Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #413 #424

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions sztp-agent/cmd/cli.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 8 additions & 5 deletions sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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()
},
Expand Down
12 changes: 5 additions & 7 deletions sztp-agent/cmd/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ 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",
RunE: func(c *cobra.Command, _ []string) error {
err := c.Help()
cobra.CheckErr(err)
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
},
},
}
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)
}
})
}
Expand Down
13 changes: 8 additions & 5 deletions sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
},
Expand Down
12 changes: 5 additions & 7 deletions sztp-agent/cmd/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ 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",
RunE: func(c *cobra.Command, _ []string) error {
err := c.Help()
cobra.CheckErr(err)
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
},
},
}
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)
}
})
}
Expand Down
13 changes: 8 additions & 5 deletions sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
},
Expand Down
12 changes: 5 additions & 7 deletions sztp-agent/cmd/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ 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",
RunE: func(c *cobra.Command, _ []string) error {
err := c.Help()
cobra.CheckErr(err)
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
},
},
}
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)
}
})
}
Expand Down
13 changes: 8 additions & 5 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,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
Expand All @@ -32,7 +37,7 @@ func NewRunCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Exec the run 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")
Expand All @@ -54,8 +59,6 @@ func NewRunCommand() *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.RunCommand()
},
Expand Down
12 changes: 5 additions & 7 deletions sztp-agent/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ 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",
RunE: func(c *cobra.Command, _ []string) error {
err := c.Help()
cobra.CheckErr(err)
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
},
},
}
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)
}
})
}
Expand Down
13 changes: 8 additions & 5 deletions sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
},
Expand Down
12 changes: 5 additions & 7 deletions sztp-agent/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ 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",
RunE: func(c *cobra.Command, _ []string) error {
err := c.Help()
cobra.CheckErr(err)
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
},
},
}
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)
}
})
}
Expand Down
28 changes: 1 addition & 27 deletions sztp-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +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.NewRunCommand())
c.AddCommand(cmd.NewStatusCommand())
c.AddCommand(cmd.NewEnableCommand())
c.AddCommand(cmd.NewDisableCommand())

return c
}
Loading