Skip to content

Commit

Permalink
improvement: add cmd text to i18n package & stdout handling & error h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
l-hellmann committed Jun 6, 2024
1 parent 030bdf3 commit ee5b78e
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 200 deletions.
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
52 changes: 35 additions & 17 deletions src/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,54 @@ package cmd

import (
"context"
"fmt"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/i18n"
"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"
)

var env string

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 {

fmt.Println(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))

fmt.Println(styles.CobraSectionColor().SetString(`
Curently used variables:`).String())

body := &uxBlock.TableBody{}
guestInfoPart(body)
cmdData.UxBlocks.Table(body)
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)
}
49 changes: 22 additions & 27 deletions src/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,34 @@ import (
"context"

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

var logout string

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.Update(func(data cliStorage.Data) cliStorage.Data {
return cliStorage.Data{}
})
if err != nil {
return err
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.LogoutVpnDisconnecting)))
disconnectVpn(ctx, cmdData.UxBlocks) // TODO: ask - there is no need for any declaration - i can just call this function from anywhere?
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.LogoutSuccess)))

return nil

})
}
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
})
}
61 changes: 16 additions & 45 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 @@ -34,29 +33,19 @@ func rootCmd() *cmdBuilder.Cmd {
AddChildrenCmd(statusShowDebugLogsCmd()).
AddChildrenCmd(servicePushCmd()).
AddChildrenCmd(envCmd()).
AddChildrenCmd(supportCmd()).
AddChildrenCmd(supportCmd()).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
fmt.Println(`Welcome to zCli by Zerops!
To unlock the full potential of zCLI, you need to log in using your Zerops account.
Logging in enables you to access various features and interact with Zerops services seamlessly.
To log in, simply use the following command: zcli login <your_token>
Replace <your_token> with the authentication token generated from your Zerops account.
Once logged in, you'll be able to manage projects, deploy applications, configure VPN,
and much more directly from the command line interface.
If you encounter any issues during the login process or have any questions,
feel free to find out how to contact our support team by running 'zcli support'.
`)
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 {

var loggedUser string
if info, err := cmdData.RestApiClient.GetUserInfo(ctx); err != nil {
loggedUser = err.Error()
Expand All @@ -68,8 +57,7 @@ feel free to find out how to contact our support team by running 'zcli support'.
}
}


// TODO: krls - check whole block
// TODO: krls - check whole block
if cmdData.CliStorage.Data().ScopeProjectId.Filled() {
// project scope is set
projectId, _ := cmdData.CliStorage.Data().ScopeProjectId.Get()
Expand All @@ -81,21 +69,24 @@ feel free to find out how to contact our support team by running 'zcli support'.
return err
}
} else {
fmt.Print(i18n.T(i18n.ScopedProject), err.Error())
cmdData.Stderr.PrintLines(i18n.T(i18n.ScopedProject), err.Error())
}
} else {
fmt.Print(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
var vpnStatusText string
if isVpnUp(ctx, cmdData.UxBlocks, 1) {
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsActive)
} else {
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsNotActive)
}

fmt.Printf("Welcome in Zerops!\nYou are loged as %s \nand your %s.\n\n", loggedUser, vpnStatusText)
cmdData.Stdout.PrintLines(
i18n.T(i18n.LoggedWelcome, loggedUser, vpnStatusText),
printer.EmptyLine,
)

// print the default command help
cmdData.PrintHelp()
Expand All @@ -104,26 +95,6 @@ feel free to find out how to contact our support team by running 'zcli support'.
})
}

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}}
Expand Down
24 changes: 9 additions & 15 deletions src/cmd/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ package cmd

import (
"context"
"fmt"

"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"
)

var support string

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 {


fmt.Println("You can contact Zerops support via:")
fmt.Println("- E-mail: [email protected]")
fmt.Println("- Discord: https://discord.com/invite/WDvCZ54")
fmt.Println(`
Additionally, you can explore our documentation
at https://docs.zerops.io/references/cli for further details.
`)
return nil

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: 0 additions & 2 deletions src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,13 @@ func vpnUpCmd() *cmdBuilder.Cmd {
return err
}


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


return nil
})
}
Expand Down
Loading

0 comments on commit ee5b78e

Please sign in to comment.