Skip to content

Commit

Permalink
feat: Agent version command (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahmmcgivern authored Oct 4, 2024
1 parent 0f05d91 commit 08e9dd7
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 6 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jedib0t/go-pretty/v6 v6.4.4
github.com/joshdk/go-junit v0.0.0-20210226021600-6145f504ca0d
github.com/mitchellh/go-homedir v1.1.0
github.com/newrelic/newrelic-client-go/v2 v2.38.0
github.com/newrelic/newrelic-client-go/v2 v2.48.2
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil/v3 v3.23.9
github.com/sirupsen/logrus v1.9.3
Expand All @@ -28,6 +28,7 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.23.0
golang.org/x/term v0.18.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
mvdan.cc/sh/v3 v3.4.3
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGg
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/newrelic-forks/task/v3 v3.11.0 h1:8Gwo33tMTqQkWlekCChbCZqf0nrXI85UqVeIgXk73L0=
github.com/newrelic-forks/task/v3 v3.11.0/go.mod h1:rzfFpA7kl0CsL44ZQd2F2Hic5xOb2m03N6Y2lUjdNo4=
github.com/newrelic/newrelic-client-go/v2 v2.38.0 h1:2Q106SyrB7e0zeWP7374HBCJ2N5aWhZ8sZBkzmxwzn0=
github.com/newrelic/newrelic-client-go/v2 v2.38.0/go.mod h1:pDFY24/6iIMEbPIdowTRrRn9YYwkXc3j+B+XpTb4oF4=
github.com/newrelic/newrelic-client-go/v2 v2.48.2 h1:Pd8/ADdidF/dISMCy9B2UHvcD11lnj4Use6j++OOEf4=
github.com/newrelic/newrelic-client-go/v2 v2.48.2/go.mod h1:pDFY24/6iIMEbPIdowTRrRn9YYwkXc3j+B+XpTb4oF4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
48 changes: 47 additions & 1 deletion internal/agent/command.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package agent

import (
"github.com/spf13/cobra"
"errors"
"fmt"
"strings"

"github.com/newrelic/newrelic-cli/internal/agent/migrate"
"github.com/newrelic/newrelic-cli/internal/agent/obfuscate"
"github.com/newrelic/newrelic-cli/internal/agent/version"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/v2/pkg/agent"
"github.com/spf13/cobra"
)

var (
Expand All @@ -28,6 +34,44 @@ var Command = &cobra.Command{
Long: `Utilities for New Relic Agents`,
}

var cmdAgentVersion = &cobra.Command{
Use: "version",
Short: "Show latest agent versions.",
Long: `Show latest agent versions. Valid agent names include:
android, browser, dotnet, elixir, go, infrastructure, ios, java, nodejs, php, python, ruby, sdk"
`,
Example: "newrelic agent version <agent_name>",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("invalid number of arguments")
}

agentName := strings.ToUpper(args[0])

if !version.IsValidAgentName(agentName) {
return fmt.Errorf("invalid agent name: %s, use --help for a list of valid agent names", args[0])
}

return nil
},
PreRun: client.RequireClient,
RunE: func(cmd *cobra.Command, args []string) error {
agentName := agent.AgentReleasesFilter(strings.ToUpper(args[0]))

result, err := client.NRClient.Agent.GetCurrentAgentReleaseWithContext(utils.SignalCtx, agentName)

if err != nil {
return err
}

agentNameTitleCase := version.AgentNameTitleCase(agentName)

fmt.Printf("%s: %s\n", agentNameTitleCase, result.Version)

return nil
},
}

var cmdConfig = &cobra.Command{
Use: "config",
Short: "Configuration utilities/helpers for New Relic agents",
Expand Down Expand Up @@ -71,6 +115,8 @@ func init() {

Command.AddCommand(cmdConfig)

Command.AddCommand(cmdAgentVersion)

cmdConfig.AddCommand(cmdConfigObfuscate)

cmdConfigObfuscate.Flags().StringVarP(&encodeKey, "key", "k", "", "the key to use when obfuscating the clear-text value")
Expand Down
8 changes: 7 additions & 1 deletion internal/agent/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAgent(t *testing.T) {
func TestAgentConfig(t *testing.T) {
assert.Equal(t, "config", cmdConfig.Name())
testcobra.CheckCobraMetadata(t, cmdConfig)
testcobra.CheckCobraMetadata(t, cmdConfig)
testcobra.CheckCobraRequiredFlags(t, cmdConfigObfuscate, []string{})
}

func TestAgentConfigObfuscate(t *testing.T) {
Expand All @@ -34,3 +34,9 @@ func TestAgentConfigMigrate(t *testing.T) {
testcobra.CheckCobraMetadata(t, cmdMigrateV3toV4)
testcobra.CheckCobraRequiredFlags(t, cmdMigrateV3toV4, []string{})
}

func TestAgentAgentVersion(t *testing.T) {
assert.Equal(t, "version", cmdAgentVersion.Name())
testcobra.CheckCobraMetadata(t, cmdAgentVersion)
testcobra.CheckCobraRequiredFlags(t, cmdAgentVersion, []string{})
}
34 changes: 34 additions & 0 deletions internal/agent/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package version

import (
"reflect"

"github.com/newrelic/newrelic-client-go/v2/pkg/agent"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func AgentNameTitleCase(agentName agent.AgentReleasesFilter) string {
caser := cases.Title(language.AmericanEnglish)

switch agentName {
case agent.AgentReleasesFilterTypes.DOTNET:
return ".NET"
case agent.AgentReleasesFilterTypes.IOS:
return "iOS"
case agent.AgentReleasesFilterTypes.NODEJS:
return "Node.js"
case agent.AgentReleasesFilterTypes.PHP:
return "PHP"
case agent.AgentReleasesFilterTypes.SDK:
return "C SDK"
default:
return caser.String(string(agentName))
}
}

func IsValidAgentName(agentName string) bool {
a := reflect.ValueOf(agent.AgentReleasesFilterTypes)

return a.FieldByName(agentName).String() == agentName
}
105 changes: 105 additions & 0 deletions internal/agent/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package version_test

import (
"testing"

"github.com/newrelic/newrelic-cli/internal/agent/version"
"github.com/newrelic/newrelic-client-go/v2/pkg/agent"
"github.com/stretchr/testify/assert"
)

func TestAgentNameTitleCase(t *testing.T) {
t.Parallel()

expected := "Android"
agentName := agent.AgentReleasesFilterTypes.ANDROID
actual := version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Browser"
agentName = agent.AgentReleasesFilterTypes.BROWSER
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = ".NET"
agentName = agent.AgentReleasesFilterTypes.DOTNET
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Elixir"
agentName = agent.AgentReleasesFilterTypes.ELIXIR
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Go"
agentName = agent.AgentReleasesFilterTypes.GO
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Infrastructure"
agentName = agent.AgentReleasesFilterTypes.INFRASTRUCTURE
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "iOS"
agentName = agent.AgentReleasesFilterTypes.IOS
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Java"
agentName = agent.AgentReleasesFilterTypes.JAVA
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Node.js"
agentName = agent.AgentReleasesFilterTypes.NODEJS
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "PHP"
agentName = agent.AgentReleasesFilterTypes.PHP
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Python"
agentName = agent.AgentReleasesFilterTypes.PYTHON
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "Ruby"
agentName = agent.AgentReleasesFilterTypes.RUBY
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)

expected = "C SDK"
agentName = agent.AgentReleasesFilterTypes.SDK
actual = version.AgentNameTitleCase(agentName)

assert.Equal(t, expected, actual)
}

func TestIsValidAgentName(t *testing.T) {
t.Parallel()

agentName := "PYTHON"
actual := version.IsValidAgentName(agentName)

assert.True(t, actual)

agentName = "ASDF"
actual = version.IsValidAgentName(agentName)

assert.False(t, actual)
}

0 comments on commit 08e9dd7

Please sign in to comment.