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

change colors and add logout, support info, env section, update texts… #145

Merged
merged 2 commits into from
Jun 6, 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
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
## help show this help
.PHONY: help

define helpMessage
possible values:
test
lint
build-for-windows-amd
build-for-linux-amd
build-for-darwin-arm
endef
export helpMessage

help:
@printf "possible values: test, lint"
@echo "$$helpMessage"

test:
go test -v ./cmd/... ./src/...
Expand Down
6 changes: 1 addition & 5 deletions cmd/zcli/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package main

import (
"os"

"github.com/zeropsio/zcli/src/cmd"
)

func main() {
if cmd.ExecuteCmd() != nil {
os.Exit(1)
}
cmd.ExecuteCmd()
}
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
Expand Down Expand Up @@ -144,6 +146,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
55 changes: 55 additions & 0 deletions src/cmd/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"context"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/constants"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/printer"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
)

func envCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("env").
Short(i18n.T(i18n.CmdDescEnv)).
HelpFlag(i18n.T(i18n.CmdHelpEnv)).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
cmdData.Stdout.PrintLines(
printer.Style(styles.CobraSectionColor(), i18n.T(i18n.GlobalEnvVariables)),
printer.Style(styles.CobraItemNameColor(), constants.CliLogFilePathEnvVar)+"\t"+i18n.T(i18n.CliLogFilePathEnvVar),
printer.Style(styles.CobraItemNameColor(), constants.CliDataFilePathEnvVar)+"\t"+i18n.T(i18n.CliDataFilePathEnvVar),
printer.Style(styles.CobraItemNameColor(), constants.CliTerminalMode)+"\t"+i18n.T(i18n.CliTerminalModeEnvVar),
printer.EmptyLine,
printer.Style(styles.CobraSectionColor(), i18n.T(i18n.CurrentlyUsedEnvVariables)),
)

body := uxBlock.NewTableBody()
guestInfoPart(body)
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)
}
37 changes: 37 additions & 0 deletions src/cmd/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"context"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock/styles"
)

func logoutCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("logout").
Short(i18n.T(i18n.CmdDescLogout)).
HelpFlag(i18n.T(i18n.CmdHelpLogout)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

_, err := cmdData.RestApiClient.PostAuthLogout(ctx)
if err != nil {
return err
}

_, err = cmdData.CliStorage.Clear()
if err != nil {
return err
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.LogoutVpnDisconnecting)))
if isVpnUp(ctx, uxBlocks, 1) {
_ = disconnectVpn(ctx, uxBlocks)
}
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.LogoutSuccess)))

return nil
})
}
81 changes: 27 additions & 54 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (

"github.com/zeropsio/zcli/src/cmd/scope"
"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/printer"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zerops-go/errorCode"
)

func ExecuteCmd() error {
return cmdBuilder.ExecuteRootCmd(rootCmd())
func ExecuteCmd() {
cmdBuilder.ExecuteRootCmd(rootCmd())
}

func rootCmd() *cmdBuilder.Cmd {
Expand All @@ -25,30 +24,28 @@ func rootCmd() *cmdBuilder.Cmd {
SetHelpTemplate(getRootTemplate()).
SilenceError(true).
AddChildrenCmd(loginCmd()).
AddChildrenCmd(logoutCmd()).
AddChildrenCmd(versionCmd()).
AddChildrenCmd(scopeCmd()).
AddChildrenCmd(projectCmd()).
AddChildrenCmd(serviceCmd()).
AddChildrenCmd(vpnCmd()).
AddChildrenCmd(statusShowDebugLogsCmd()).
AddChildrenCmd(servicePushCmd()).
AddChildrenCmd(envCmd()).
AddChildrenCmd(supportCmd()).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
body := &uxBlock.TableBody{}

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

guestInfoPart(body)

cmdData.UxBlocks.Table(body)
cmdData.Stdout.PrintLines(
i18n.T(i18n.GuestWelcome),
printer.EmptyLine,
)

// print the default command help
cmdData.PrintHelp()

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()
Expand All @@ -60,10 +57,7 @@ func rootCmd() *cmdBuilder.Cmd {
}
}

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

guestInfoPart(body)

// TODO: krls - check whole block
if cmdData.CliStorage.Data().ScopeProjectId.Filled() {
// project scope is set
projectId, _ := cmdData.CliStorage.Data().ScopeProjectId.Get()
Expand All @@ -75,20 +69,24 @@ func rootCmd() *cmdBuilder.Cmd {
return err
}
} else {
body.AddStringsRow(i18n.T(i18n.ScopedProject), err.Error())
cmdData.Stderr.PrintLines(i18n.T(i18n.ScopedProject), err.Error())
}
} else {
body.AddStringsRow(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native()))
cmdData.Stdout.PrintLines(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native()))
}
}

var vpnStatusText string
if isVpnUp(ctx, cmdData.UxBlocks, 1) {
body.AddStringsRow(i18n.T(i18n.StatusInfoVpnStatus), i18n.T(i18n.VpnCheckingConnectionIsActive))
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsActive)
} else {
body.AddStringsRow(i18n.T(i18n.StatusInfoVpnStatus), i18n.T(i18n.VpnCheckingConnectionIsNotActive))
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsNotActive)
}

cmdData.UxBlocks.Table(body)
cmdData.Stdout.PrintLines(
i18n.T(i18n.LoggedWelcome, loggedUser, vpnStatusText),
printer.EmptyLine,
)

// print the default command help
cmdData.PrintHelp()
Expand All @@ -97,45 +95,25 @@ func rootCmd() *cmdBuilder.Cmd {
})
}

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}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

` + styles.CobraSectionColor().SetString("Aliases:").String() + `
{{.NameAndAliases}}{{end}}{{if .HasExample}}
{{.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}}
` + 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.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.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}}
Expand All @@ -144,12 +122,7 @@ func getRootTemplate() 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}}

` + 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) + `
` + styles.CobraItemNameColor().SetString("{{rpad .CommandPath .CommandPathPadding}}").String() + ` {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
Expand Down
27 changes: 27 additions & 0 deletions src/cmd/support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"context"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/printer"
"github.com/zeropsio/zcli/src/uxBlock/styles"
)

func supportCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("support").
Short(i18n.T(i18n.CmdDescSupport)).
HelpFlag(i18n.T(i18n.CmdHelpSupport)).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
cmdData.Stdout.PrintLines(
printer.Style(styles.CobraSectionColor(), i18n.T(i18n.Contact)),
printer.Style(styles.CobraItemNameColor(), "- E-mail")+": [email protected]",
printer.Style(styles.CobraItemNameColor(), "- Discord")+": https://discord.com/invite/WDvCZ54",
printer.EmptyLine,
i18n.T(i18n.Documentation),
)
return nil
})
}
3 changes: 1 addition & 2 deletions src/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"
"runtime"

"github.com/zeropsio/zcli/src/cmdBuilder"
Expand All @@ -17,7 +16,7 @@ func versionCmd() *cmdBuilder.Cmd {
Short(i18n.T(i18n.CmdDescVersion)).
HelpFlag(i18n.T(i18n.CmdHelpVersion)).
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)
cmdData.Stdout.Printf("zcli version %s (%s) %s/%s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)

return nil
})
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/vpnDown.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func disconnectVpn(ctx context.Context, uxBlocks uxBlock.UxBlocks) error {
return err
}

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

return nil
}
2 changes: 1 addition & 1 deletion src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func vpnUpCmd() *cmdBuilder.Cmd {

// wait for the vpn to be up
if isVpnUp(ctx, uxBlocks, 6) {
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.VpnUp)))
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.VpnUp)))
} else {
uxBlocks.PrintWarning(styles.WarningLine(i18n.T(i18n.VpnPingFailed)))
}
Expand Down
Loading
Loading