Skip to content
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
81 changes: 42 additions & 39 deletions internal/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,19 @@ import (
"github.com/urfave/cli/v3"
)

type contextKey string

const (
gatewayContextKey contextKey = "gateway"
)

// Gateway model constants.
const (
ARCADYAN string = "ARCADYAN"
NOK5G21 string = "NOK5G21"
DefaultTimeout time.Duration = 5 * time.Second
)

func commonContext(ctx context.Context, cmd *cli.Command) (context.Context, error) {
debug := cmd.Bool(ConfigDebug)
noColor := cmd.Bool(ConfigNoColor)
quiet := cmd.Bool(ConfigQuiet)

if debug {
pterm.EnableDebugMessages()
}

if noColor {
pterm.DisableStyling()
}
//nolint:gochecknoglobals
var initGatewayFunc = initGateway

if quiet {
pterm.DisableOutput()
}
//nolint:ireturn
func initGateway(cmd *cli.Command) (pkg.Gateway, error) {
debug := cmd.Bool(ConfigDebug)

gateway, err := getGateway(cmd.Version,
cmd.String(ConfigModel),
Expand All @@ -58,16 +42,17 @@ func commonContext(ctx context.Context, cmd *cli.Command) (context.Context, erro
return nil, err
}

newCtx := context.WithValue(ctx, gatewayContextKey, gateway)

return newCtx, nil
return gateway, nil
}

// Login handles the login CLI command.
func Login(ctx context.Context, _ *cli.Command) error {
gateway, _ := ctx.Value(gatewayContextKey).(pkg.Gateway)
func Login(_ context.Context, cmd *cli.Command) error {
gateway, err := initGatewayFunc(cmd)
if err != nil {
return err
}

err := gateway.Login()
err = gateway.Login()
if err != nil {
pterm.Error.Println("could not log in:", err)

Expand All @@ -80,7 +65,12 @@ func Login(ctx context.Context, _ *cli.Command) error {
}

// Req handles the req CLI command for custom HTTP requests.
func Req(ctx context.Context, cmd *cli.Command) error {
func Req(_ context.Context, cmd *cli.Command) error {
gateway, err := initGatewayFunc(cmd)
if err != nil {
return err
}

const requiredArgsCount = 2
if cmd.NArg() != requiredArgsCount {
return cli.Exit("exactly 2 arguments required (HTTP method and path)", 1)
Expand All @@ -90,7 +80,6 @@ func Req(ctx context.Context, cmd *cli.Command) error {
path := cmd.Args().Get(1)
loginFirst := cmd.Bool("login")

gateway, _ := ctx.Value(gatewayContextKey).(pkg.Gateway)
if loginFirst {
if err := gateway.Login(); err != nil {
return fmt.Errorf("request failed: %w", err)
Expand All @@ -105,8 +94,12 @@ func Req(ctx context.Context, cmd *cli.Command) error {
}

// Info handles the info CLI command.
func Info(ctx context.Context, _ *cli.Command) error {
gateway, _ := ctx.Value(gatewayContextKey).(pkg.Gateway)
func Info(_ context.Context, cmd *cli.Command) error {
gateway, err := initGatewayFunc(cmd)
if err != nil {
return err
}

if err := gateway.Info(); err != nil {
return fmt.Errorf("info command failed: %w", err)
}
Expand All @@ -115,8 +108,12 @@ func Info(ctx context.Context, _ *cli.Command) error {
}

// Status handles the status CLI command.
func Status(ctx context.Context, _ *cli.Command) error {
gateway, _ := ctx.Value(gatewayContextKey).(pkg.Gateway)
func Status(_ context.Context, cmd *cli.Command) error {
gateway, err := initGatewayFunc(cmd)
if err != nil {
return err
}

if err := gateway.Status(); err != nil {
pterm.Error.Println("status check failed:", err)

Expand All @@ -127,8 +124,12 @@ func Status(ctx context.Context, _ *cli.Command) error {
}

// Signal handles the signal CLI command.
func Signal(ctx context.Context, _ *cli.Command) error {
gateway, _ := ctx.Value(gatewayContextKey).(pkg.Gateway)
func Signal(_ context.Context, cmd *cli.Command) error {
gateway, err := initGatewayFunc(cmd)
if err != nil {
return err
}

if err := gateway.Signal(); err != nil {
return fmt.Errorf("signal command failed: %w", err)
}
Expand All @@ -137,8 +138,11 @@ func Signal(ctx context.Context, _ *cli.Command) error {
}

// Reboot handles the reboot CLI command.
func Reboot(ctx context.Context, cmd *cli.Command) error {
gateway, _ := ctx.Value(gatewayContextKey).(pkg.Gateway)
func Reboot(_ context.Context, cmd *cli.Command) error {
gateway, err := initGatewayFunc(cmd)
if err != nil {
return err
}

dryRun := cmd.Bool(ConfigDryRun)
if !cmd.Bool(ConfigAutoConfirm) {
Expand All @@ -152,7 +156,7 @@ func Reboot(ctx context.Context, cmd *cli.Command) error {
}
}

err := gateway.Reboot(dryRun)
err = gateway.Reboot(dryRun)
if err != nil {
pterm.Error.Println("could not reboot gateway:", err)

Expand Down Expand Up @@ -183,7 +187,6 @@ func Cmd(version string) {
Version: version,
Flags: cmdFlags(&configFile, configSource),
Commands: cmdCommands(),
Before: commonContext,
}

if err := app.Run(context.Background(), os.Args); err != nil {
Expand Down
41 changes: 33 additions & 8 deletions internal/cmd_builder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package internal

import (
"context"
"fmt"

"github.com/pterm/pterm"
altsrc "github.com/urfave/cli-altsrc/v3"
toml "github.com/urfave/cli-altsrc/v3/toml"
"github.com/urfave/cli/v3"
Expand Down Expand Up @@ -34,8 +36,16 @@ func cmdCommands() []*cli.Command {
Action: Login,
},
{
Name: "reboot",
Usage: "Reboot the router",
Name: "reboot",
Usage: "Reboot the router",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: ConfigAutoConfirm,
Aliases: []string{"y"},
Value: false,
Usage: "skip confirmation prompts",
},
},
Action: Reboot,
},
{
Expand Down Expand Up @@ -85,23 +95,38 @@ func cmdFlags(configFile *string, configSource altsrc.Sourcer) []cli.Flag { //no
Aliases: []string{"d"},
Value: false,
Usage: "display debugging output in the console",
Action: func(_ context.Context, _ *cli.Command, v bool) error {
if v {
pterm.EnableDebugMessages()
}

return nil
},
},
&cli.BoolFlag{
Name: ConfigNoColor,
Value: false,
Usage: "disable colored output",
Action: func(_ context.Context, _ *cli.Command, v bool) error {
if v {
pterm.DisableStyling()
}

return nil
},
},
&cli.BoolFlag{
Name: ConfigQuiet,
Aliases: []string{"q"},
Value: false,
Usage: "quiet mode, suppresses output",
},
&cli.BoolFlag{
Name: ConfigAutoConfirm,
Aliases: []string{"y"},
Value: false,
Usage: "skip confirmation prompts",
Action: func(_ context.Context, _ *cli.Command, v bool) error {
if v {
pterm.DisableOutput()
}

return nil
},
},
&cli.BoolFlag{
Name: ConfigDryRun,
Expand Down
Loading
Loading