Skip to content

Commit

Permalink
fix: cobra commands automatically populate
Browse files Browse the repository at this point in the history
Signed-off-by: Xeckt <[email protected]>
  • Loading branch information
Xeckt committed Jun 26, 2024
1 parent 33b5b5c commit d2ae079
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .idea/sztp.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
8 changes: 4 additions & 4 deletions sztp-agent/cmd/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
})
}
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
8 changes: 4 additions & 4 deletions sztp-agent/cmd/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
})
}
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
8 changes: 4 additions & 4 deletions sztp-agent/cmd/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
})
}
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 @@ -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
Expand All @@ -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()
},
Expand Down
8 changes: 4 additions & 4 deletions sztp-agent/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
})
}
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
8 changes: 4 additions & 4 deletions sztp-agent/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
})
}
Expand Down
Loading

0 comments on commit d2ae079

Please sign in to comment.