Skip to content

Commit

Permalink
commands refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-hajek committed Mar 9, 2024
1 parent b8aa3d5 commit c701fd1
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 477 deletions.
2 changes: 0 additions & 2 deletions src/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ func projectCmd() *cmdBuilder.Cmd {
Short(i18n.T(i18n.CmdProject)).
HelpFlag(i18n.T(i18n.ProjectHelp)).
AddChildrenCmd(projectListCmd()).
AddChildrenCmd(projectStartCmd()).
AddChildrenCmd(projectStopCmd()).
AddChildrenCmd(projectDeleteCmd()).
AddChildrenCmd(projectServiceImportCmd()).
AddChildrenCmd(projectImportCmd())
Expand Down
137 changes: 128 additions & 9 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,138 @@
package cmd

import (
"context"
"fmt"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/constants"
repository2 "github.com/zeropsio/zcli/src/entity/repository"
"github.com/zeropsio/zcli/src/errorsx"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
)

func ExecuteCmd() error {
builder := cmdBuilder.NewCmdBuilder()
return cmdBuilder.ExecuteRootCmd(rootCmd())
}

func rootCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("zcli").
SetHelpTemplate(getRootTemplate()).
AddChildrenCmd(loginCmd()).
AddChildrenCmd(versionCmd()).
AddChildrenCmd(scopeCmd()).
AddChildrenCmd(projectCmd()).
AddChildrenCmd(serviceCmd()).
AddChildrenCmd(vpnCmd()).
AddChildrenCmd(statusShowDebugLogsCmd()).
AddChildrenCmd(servicePushCmd()).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
body := &uxBlock.TableBody{}

body.AddStringsRow(i18n.T(i18n.StatusInfoLoggedUser), "-")

guestInfoPart(body)

cmdData.UxBlocks.Table(body)

return nil
}).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
body := &uxBlock.TableBody{}

loggedUser := ""

Check failure on line 46 in src/cmd/root.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

assigned to loggedUser, but reassigned without using the value (wastedassign)

Check failure on line 46 in src/cmd/root.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

assigned to loggedUser, but reassigned without using the value (wastedassign)

Check failure on line 46 in src/cmd/root.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

assigned to loggedUser, but reassigned without using the value (wastedassign)
if info, err := cmdData.RestApiClient.GetUserInfo(ctx); err != nil {
loggedUser = err.Error()
} else {
if infoOutput, err := info.Output(); err != nil {
loggedUser = err.Error()
} else {
loggedUser = fmt.Sprintf("%s <%s>", infoOutput.FullName, infoOutput.Email)
}
}

body.AddStringsRow(i18n.T(i18n.StatusInfoLoggedUser), loggedUser)

guestInfoPart(body)

if cmdData.CliStorage.Data().ScopeProjectId.Filled() {
// project scope is set
projectId, _ := cmdData.CliStorage.Data().ScopeProjectId.Get()
project, err := repository2.GetProjectById(ctx, cmdData.RestApiClient, projectId)
if err != nil {
if errorsx.IsUserError(err) {
cmdData.UxBlocks.PrintWarning(styles.WarningLine(i18n.T(i18n.ScopedProjectNotFound)))
}

return err
}

body.AddStringsRow(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native()))
}

cmdData.UxBlocks.Table(body)

return nil
})
}

func guestInfoPart(tableBody *uxBlock.TableBody) {
cliDataFilePath, err := constants.CliDataFilePath()
if err != nil {
cliDataFilePath = err.Error()
}
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoCliDataFilePath), cliDataFilePath)

logFilePath, err := constants.LogFilePath()
if err != nil {
logFilePath = err.Error()
}
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoLogFilePath), logFilePath)

wgConfigFilePath, err := constants.WgConfigFilePath()
if err != nil {
wgConfigFilePath = err.Error()
}
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoWgConfigFilePath), wgConfigFilePath)
}

func getRootTemplate() string {
return styles.CobraSectionColor().SetString("Usage:").String() + `{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
` + styles.CobraSectionColor().SetString("Aliases:").String() + `
{{.NameAndAliases}}{{end}}{{if .HasExample}}
` + styles.CobraSectionColor().SetString("Examples:").String() + `
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
` + styles.CobraSectionColor().SetString("Available Commands:").String() + `{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
` + styles.CobraItemNameColor().SetString("{{rpad .Name .NamePadding }}").String() + ` {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
` + styles.CobraItemNameColor().SetString("{{rpad .Name .NamePadding }}").String() + ` {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
` + styles.CobraSectionColor().SetString("Additional Commands:").String() + `{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
` + styles.CobraItemNameColor().SetString("{{rpad .Name .NamePadding }}").String() + ` {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
` + styles.CobraSectionColor().SetString("Flags:").String() + `
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
` + styles.CobraSectionColor().SetString("Global Flags:").String() + `
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
` + styles.CobraSectionColor().SetString("Additional help topics:").String() + `{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
` + styles.CobraItemNameColor().SetString("{{rpad .CommandPath .CommandPathPadding}}").String() + ` {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
builder.AddCommand(loginCmd())
builder.AddCommand(versionCmd())
builder.AddCommand(scopeCmd())
builder.AddCommand(projectCmd())
builder.AddCommand(serviceCmd())
builder.AddCommand(statusCmd())
builder.AddCommand(vpnCmd())
` + styles.CobraSectionColor().SetString("Global Env Variables:").String() + `
` + styles.CobraItemNameColor().SetString(constants.CliLogFilePathEnvVar).String() + ` ` + i18n.T(i18n.CliLogFilePathEnvVar) + `
` + styles.CobraItemNameColor().SetString(constants.CliDataFilePathEnvVar).String() + ` ` + i18n.T(i18n.CliDataFilePathEnvVar) + `
` + styles.CobraItemNameColor().SetString(constants.CliTerminalMode).String() + ` ` + i18n.T(i18n.CliTerminalModeEnvVar) + `
return builder.CreateAndExecuteRootCobraCmd()
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
}
15 changes: 0 additions & 15 deletions src/cmd/status.go

This file was deleted.

55 changes: 0 additions & 55 deletions src/cmd/statusInfo.go

This file was deleted.

2 changes: 1 addition & 1 deletion src/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func versionCmd() *cmdBuilder.Cmd {
Use("version").
Short(i18n.T(i18n.CmdVersion)).
HelpFlag(i18n.T(i18n.VersionHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
fmt.Printf("zcli version %s (%s) %s/%s\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)

return nil
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func vpnCmd() *cmdBuilder.Cmd {
Use("vpn").
Short(i18n.T(i18n.CmdVpn)).
HelpFlag(i18n.T(i18n.VpnHelp)).
AddChildrenCmd(vpnConnectCmd()).
AddChildrenCmd(vpnDisconnectCmd())
AddChildrenCmd(vpnUpCmd()).
AddChildrenCmd(vpnDownCmd())
}
10 changes: 5 additions & 5 deletions src/cmd/vpnDisconnect.go → src/cmd/vpnDown.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/zeropsio/zcli/src/uxBlock/styles"
)

func vpnDisconnectCmd() *cmdBuilder.Cmd {
func vpnDownCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("disconnect").
Short(i18n.T(i18n.CmdVpnDisconnect)).
HelpFlag(i18n.T(i18n.VpnDisconnectHelp)).
Use("down").
Short(i18n.T(i18n.CmdVpnDown)).
HelpFlag(i18n.T(i18n.VpnDownHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand All @@ -28,7 +28,7 @@ func vpnDisconnectCmd() *cmdBuilder.Cmd {
return err
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.VpnDisconnected)))
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.VpnDown)))

return nil
})
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/vpnConnect.go → src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"github.com/zeropsio/zcli/src/uxBlock/styles"
)

func vpnConnectCmd() *cmdBuilder.Cmd {
func vpnUpCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("connect").
Short(i18n.T(i18n.CmdVpnConnect)).
Use("up").
Short(i18n.T(i18n.CmdVpnUp)).
ScopeLevel(scope.Project).
Arg(scope.ProjectArgName, cmdBuilder.OptionalArg()).
HelpFlag(i18n.T(i18n.VpnConnectHelp)).
HelpFlag(i18n.T(i18n.VpnUpHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand Down Expand Up @@ -115,7 +115,7 @@ func vpnConnectCmd() *cmdBuilder.Cmd {

// TODO - janhajek ping {{.Ipv4NetworkGateway}}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.VpnConnected)))
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.VpnUp)))

return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/zeropsio/zcli/src/uxBlock"
)

func (b *CmdBuilder) buildCobraCmd(
func buildCobraCmd(
cmd *Cmd,
flagParams *flagParams.Handler,
uxBlocks uxBlock.UxBlocks,
Expand All @@ -21,6 +21,10 @@ func (b *CmdBuilder) buildCobraCmd(
SilenceUsage: cmd.silenceUsage,
}

if cmd.helpTemplate != "" {
cobraCmd.SetHelpTemplate(cmd.helpTemplate)
}

argNames := make([]string, len(cmd.args))
for i, arg := range cmd.args {
argName := arg.name
Expand Down Expand Up @@ -59,11 +63,11 @@ func (b *CmdBuilder) buildCobraCmd(
}

if cmd.guestRunFunc != nil || cmd.loggedUserRunFunc != nil {
cobraCmd.RunE = b.createCmdRunFunc(cmd, flagParams, uxBlocks, cliStorage)
cobraCmd.RunE = createCmdRunFunc(cmd, flagParams, uxBlocks, cliStorage)
}

for _, childrenCmd := range cmd.childrenCmds {
cobraChildrenCmd, err := b.buildCobraCmd(childrenCmd, flagParams, uxBlocks, cliStorage)
cobraChildrenCmd, err := buildCobraCmd(childrenCmd, flagParams, uxBlocks, cliStorage)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions src/cmdBuilder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Cmd struct {
use string
short string
long string
helpTemplate string
loggedUserRunFunc loggedUserRunFunc
guestRunFunc guestRunFunc
silenceUsage bool
Expand Down Expand Up @@ -59,6 +60,11 @@ func (cmd *Cmd) Use(use string) *Cmd {
return cmd
}

func (cmd *Cmd) SetHelpTemplate(template string) *Cmd {
cmd.helpTemplate = template
return cmd
}

func (cmd *Cmd) Short(short string) *Cmd {
cmd.short = short
return cmd
Expand Down
13 changes: 0 additions & 13 deletions src/cmdBuilder/cmdBuilder.go

This file was deleted.

Loading

0 comments on commit c701fd1

Please sign in to comment.