diff --git a/.idea/sztp.iml b/.idea/sztp.iml new file mode 100644 index 00000000..7ee078df --- /dev/null +++ b/.idea/sztp.iml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..9addc0d6 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + { + "associatedIndex": 8 +} + + + + { + "keyToString": { + "DefaultGoTemplateProperty": "Go File", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.go.formatter.settings.were.checked": "true", + "RunOnceActivity.go.migrated.go.modules.settings": "true", + "RunOnceActivity.go.modules.automatic.dependencies.download": "true", + "RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true", + "git-widget-placeholder": "cli-fixes", + "go.import.settings.migrated": "true", + "go.sdk.automatically.set": "true", + "last_opened_file_path": "/Users/jordan/Documents/GitHub/sztp", + "node.js.detected.package.eslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "nodejs_package_manager_path": "npm" + } +} + + + + + + + + + + + true + + \ No newline at end of file diff --git a/sztp-agent/cmd/cli.go b/sztp-agent/cmd/cli.go new file mode 100644 index 00000000..2bc71abc --- /dev/null +++ b/sztp-agent/cmd/cli.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "log" + "os" + + "github.com/TwiN/go-color" + "github.com/spf13/cobra" +) + +// commands hold a slice of all cobra commands for cli tool +var commands []*cobra.Command + +// RootCmd is the main entrypoint for the cli +func RootCmd() *cobra.Command { + c := &cobra.Command{ + Use: "opi-sztp-agent", + Short: "opi-sztp-agent is the agent command line interface to work with the sztp workflow", + Run: func(cmd *cobra.Command, _ []string) { + err := cmd.Help() + if err != nil { + log.Fatalf(color.InRed("[ERROR]")+"%s", err.Error()) + } + os.Exit(1) + }, + } + + for _, cmd := range commands { + c.AddCommand(cmd) + } + + return c +} diff --git a/sztp-agent/cmd/daemon.go b/sztp-agent/cmd/daemon.go index 32250f2e..d309d9a6 100644 --- a/sztp-agent/cmd/daemon.go +++ b/sztp-agent/cmd/daemon.go @@ -17,8 +17,13 @@ import ( "github.com/spf13/cobra" ) -// NewDaemonCommand returns the daemon command -func NewDaemonCommand() *cobra.Command { +//nolint:gochecknoinits +func init() { + commands = append(commands, Daemon()) +} + +// Daemon returns the daemon command +func Daemon() *cobra.Command { var ( bootstrapURL string serialNumber string @@ -32,7 +37,7 @@ func NewDaemonCommand() *cobra.Command { cmd := &cobra.Command{ Use: "daemon", Short: "Run the daemon command", - RunE: func(c *cobra.Command, _ []string) error { + RunE: func(_ *cobra.Command, _ []string) error { arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert} if bootstrapURL != "" && dhcpLeaseFile != "" { return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive") @@ -54,8 +59,6 @@ func NewDaemonCommand() *cobra.Command { return fmt.Errorf("must not be folder: %q", filePath) } } - err := c.Help() - cobra.CheckErr(err) a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandDaemon() }, diff --git a/sztp-agent/cmd/daemon_test.go b/sztp-agent/cmd/daemon_test.go index 9d75c1b7..8f8cf263 100644 --- a/sztp-agent/cmd/daemon_test.go +++ b/sztp-agent/cmd/daemon_test.go @@ -11,13 +11,13 @@ import ( "github.com/spf13/cobra" ) -func TestNewDaemonCommand(t *testing.T) { +func TestDaemonCommand(t *testing.T) { tests := []struct { name string want *cobra.Command }{ { - name: "TestNewDaemonCommand", + name: "TestDaemonCommand", want: &cobra.Command{ Use: "daemon", Short: "Run the daemon command", @@ -31,8 +31,8 @@ func TestNewDaemonCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewDaemonCommand(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { - t.Errorf("NewDaemonCommand() = %v, want %v", got, tt.want) + if got := Daemon(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { + t.Errorf("Daemon() = %v, want %v", got, tt.want) } }) } diff --git a/sztp-agent/cmd/disable.go b/sztp-agent/cmd/disable.go index 3fe22f74..3ce70a4d 100644 --- a/sztp-agent/cmd/disable.go +++ b/sztp-agent/cmd/disable.go @@ -13,8 +13,13 @@ import ( "github.com/spf13/cobra" ) -// NewDisableCommand returns the disable command -func NewDisableCommand() *cobra.Command { +//nolint:gochecknoinits +func init() { + commands = append(commands, Disable()) +} + +// Disable returns the disable command +func Disable() *cobra.Command { var ( bootstrapURL string serialNumber string @@ -28,9 +33,7 @@ func NewDisableCommand() *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Run the disable command", - RunE: func(c *cobra.Command, _ []string) error { - err := c.Help() - cobra.CheckErr(err) + RunE: func(_ *cobra.Command, _ []string) error { a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandDisable() }, diff --git a/sztp-agent/cmd/disable_test.go b/sztp-agent/cmd/disable_test.go index 73d11926..1d92a22d 100644 --- a/sztp-agent/cmd/disable_test.go +++ b/sztp-agent/cmd/disable_test.go @@ -11,13 +11,13 @@ import ( "github.com/spf13/cobra" ) -func TestNewDisableCommand(t *testing.T) { +func TestDisableCommand(t *testing.T) { tests := []struct { name string want *cobra.Command }{ { - name: "TestNewDisableCommand", + name: "TestDisableCommand", want: &cobra.Command{ Use: "disable", Short: "Run the disable command", @@ -31,8 +31,8 @@ func TestNewDisableCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewDisableCommand(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { - t.Errorf("NewDisableCommand() = %v, want %v", got, tt.want) + if got := Disable(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { + t.Errorf("Disable() = %v, want %v", got, tt.want) } }) } diff --git a/sztp-agent/cmd/enable.go b/sztp-agent/cmd/enable.go index dbab9542..745bd795 100644 --- a/sztp-agent/cmd/enable.go +++ b/sztp-agent/cmd/enable.go @@ -13,8 +13,13 @@ import ( "github.com/spf13/cobra" ) -// NewEnableCommand returns the enable command -func NewEnableCommand() *cobra.Command { +//nolint:gochecknoinits +func init() { + commands = append(commands, Enable()) +} + +// Enable returns the enable command +func Enable() *cobra.Command { var ( bootstrapURL string serialNumber string @@ -28,9 +33,7 @@ func NewEnableCommand() *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Run the enable command", - RunE: func(c *cobra.Command, _ []string) error { - err := c.Help() - cobra.CheckErr(err) + RunE: func(_ *cobra.Command, _ []string) error { a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandEnable() }, diff --git a/sztp-agent/cmd/enable_test.go b/sztp-agent/cmd/enable_test.go index 4b20f975..6d11ff5a 100644 --- a/sztp-agent/cmd/enable_test.go +++ b/sztp-agent/cmd/enable_test.go @@ -11,13 +11,13 @@ import ( "github.com/spf13/cobra" ) -func TestNewEnableCommand(t *testing.T) { +func TestEnableCommand(t *testing.T) { tests := []struct { name string want *cobra.Command }{ { - name: "TestNewEnableCommand", + name: "TestEnableCommand", want: &cobra.Command{ Use: "enable", Short: "Run the enable command", @@ -31,8 +31,8 @@ func TestNewEnableCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewEnableCommand(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { - t.Errorf("NewEnableCommand() = %v, want %v", got, tt.want) + if got := Enable(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { + t.Errorf("Enable() = %v, want %v", got, tt.want) } }) } diff --git a/sztp-agent/cmd/run.go b/sztp-agent/cmd/run.go index dfe75d00..8d2792ca 100644 --- a/sztp-agent/cmd/run.go +++ b/sztp-agent/cmd/run.go @@ -13,8 +13,13 @@ import ( "github.com/spf13/cobra" ) -// NewRunCommand returns the run command -func NewRunCommand() *cobra.Command { +//nolint:gochecknoinits +func init() { + commands = append(commands, Run()) +} + +// Run returns the run command +func Run() *cobra.Command { var ( bootstrapURL string serialNumber string @@ -28,9 +33,7 @@ func NewRunCommand() *cobra.Command { cmd := &cobra.Command{ Use: "run", Short: "Exec the run command", - RunE: func(c *cobra.Command, _ []string) error { - err := c.Help() - cobra.CheckErr(err) + RunE: func(_ *cobra.Command, _ []string) error { a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommand() }, diff --git a/sztp-agent/cmd/run_test.go b/sztp-agent/cmd/run_test.go index ef58ccaf..9885793a 100644 --- a/sztp-agent/cmd/run_test.go +++ b/sztp-agent/cmd/run_test.go @@ -11,13 +11,13 @@ import ( "github.com/spf13/cobra" ) -func TestNewRunCommand(t *testing.T) { +func TestRunCommand(t *testing.T) { tests := []struct { name string want *cobra.Command }{ { - name: "TestNewRunCommand", + name: "TestRunCommand", want: &cobra.Command{ Use: "run", Short: "Exec the run command", @@ -31,8 +31,8 @@ func TestNewRunCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewRunCommand(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { - t.Errorf("NewRunCommand() = %v, want %v", got, tt.want) + if got := Run(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { + t.Errorf("Run() = %v, want %v", got, tt.want) } }) } diff --git a/sztp-agent/cmd/status.go b/sztp-agent/cmd/status.go index 0e5e1d6f..cf5043a7 100644 --- a/sztp-agent/cmd/status.go +++ b/sztp-agent/cmd/status.go @@ -13,8 +13,13 @@ import ( "github.com/spf13/cobra" ) -// NewStatusCommand returns the status command -func NewStatusCommand() *cobra.Command { +//nolint:gochecknoinits +func init() { + commands = append(commands, Status()) +} + +// Status returns the status command +func Status() *cobra.Command { var ( bootstrapURL string serialNumber string @@ -28,9 +33,7 @@ func NewStatusCommand() *cobra.Command { cmd := &cobra.Command{ Use: "status", Short: "Run the status command", - RunE: func(c *cobra.Command, _ []string) error { - err := c.Help() - cobra.CheckErr(err) + RunE: func(_ *cobra.Command, _ []string) error { a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandStatus() }, diff --git a/sztp-agent/cmd/status_test.go b/sztp-agent/cmd/status_test.go index 4f9b3629..75717bee 100644 --- a/sztp-agent/cmd/status_test.go +++ b/sztp-agent/cmd/status_test.go @@ -11,13 +11,13 @@ import ( "github.com/spf13/cobra" ) -func TestNewStatusCommand(t *testing.T) { +func TestStatusCommand(t *testing.T) { tests := []struct { name string want *cobra.Command }{ { - name: "TestNewStatusCommand", + name: "TestStatusCommand", want: &cobra.Command{ Use: "status", Short: "Run the status command", @@ -31,8 +31,8 @@ func TestNewStatusCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewStatusCommand(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { - t.Errorf("NewStatusCommand() = %v, want %v", got, tt.want) + if got := Status(); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) { + t.Errorf("Status() = %v, want %v", got, tt.want) } }) } diff --git a/sztp-agent/main.go b/sztp-agent/main.go index 89758b52..6fad6045 100644 --- a/sztp-agent/main.go +++ b/sztp-agent/main.go @@ -13,35 +13,10 @@ import ( "github.com/opiproject/sztp/sztp-agent/cmd" "log" - "os" - - "github.com/spf13/cobra" ) func main() { - command := newCommand() - if err := command.Execute(); err != nil { + if err := cmd.RootCmd().Execute(); err != nil { log.Fatalf(color.InRed("[ERROR]")+"%s", err.Error()) } } - -func newCommand() *cobra.Command { - c := &cobra.Command{ - Use: "opi-sztp-agent", - Short: "opi-sztp-agent is the agent command line interface to work with the sztp workflow", - Run: func(cmd *cobra.Command, _ []string) { - err := cmd.Help() - if err != nil { - log.Fatalf(color.InRed("[ERROR]")+"%s", err.Error()) - } - os.Exit(1) - }, - } - - c.AddCommand(cmd.NewDaemonCommand()) - c.AddCommand(cmd.NewStatusCommand()) - c.AddCommand(cmd.NewEnableCommand()) - c.AddCommand(cmd.NewDisableCommand()) - - return c -} diff --git a/sztp-agent/sztp-agent b/sztp-agent/sztp-agent new file mode 100755 index 00000000..c0d62b92 Binary files /dev/null and b/sztp-agent/sztp-agent differ