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

commands refactor #125

Merged
merged 4 commits into from
Mar 9, 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
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
54 changes: 0 additions & 54 deletions src/cmd/projectStart.go

This file was deleted.

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

This file was deleted.

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"
"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{}

var loggedUser string
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 := repository.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())
}
Loading
Loading