Skip to content

Commit ee5b78e

Browse files
Lukáš Hellmannl-hellmann
authored andcommitted
improvement: add cmd text to i18n package & stdout handling & error handling
1 parent 030bdf3 commit ee5b78e

File tree

20 files changed

+301
-200
lines changed

20 files changed

+301
-200
lines changed

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
## help show this help
22
.PHONY: help
33

4+
define helpMessage
5+
possible values:
6+
test
7+
lint
8+
build-for-windows-amd
9+
build-for-linux-amd
10+
build-for-darwin-arm
11+
endef
12+
export helpMessage
13+
414
help:
5-
@printf "possible values: test, lint"
15+
@echo "$$helpMessage"
616

717
test:
818
go test -v ./cmd/... ./src/...

cmd/zcli/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package main
22

33
import (
4-
"os"
5-
64
"github.com/zeropsio/zcli/src/cmd"
75
)
86

97
func main() {
10-
if cmd.ExecuteCmd() != nil {
11-
os.Exit(1)
12-
}
8+
cmd.ExecuteCmd()
139
}

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf
115115
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE=
116116
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
117117
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
118+
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
119+
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
118120
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
119121
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
120122
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
@@ -144,6 +146,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
144146
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
145147
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
146148
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
149+
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
150+
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
147151
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
148152
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
149153
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

src/cmd/env.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,54 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/zeropsio/zcli/src/cmdBuilder"
8-
"github.com/zeropsio/zcli/src/i18n"
97
"github.com/zeropsio/zcli/src/constants"
8+
"github.com/zeropsio/zcli/src/i18n"
9+
"github.com/zeropsio/zcli/src/printer"
1010
"github.com/zeropsio/zcli/src/uxBlock"
1111
"github.com/zeropsio/zcli/src/uxBlock/styles"
1212
)
1313

14-
var env string
15-
1614
func envCmd() *cmdBuilder.Cmd {
1715
return cmdBuilder.NewCmd().
1816
Use("env").
1917
Short(i18n.T(i18n.CmdDescEnv)).
2018
HelpFlag(i18n.T(i18n.CmdHelpEnv)).
2119
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
22-
23-
fmt.Println(styles.CobraSectionColor().SetString("Global Env Variables:").String() + `
24-
` + styles.CobraItemNameColor().SetString(constants.CliLogFilePathEnvVar).String() + ` ` + i18n.T(i18n.CliLogFilePathEnvVar) + `
25-
` + styles.CobraItemNameColor().SetString(constants.CliDataFilePathEnvVar).String() + ` ` + i18n.T(i18n.CliDataFilePathEnvVar) + `
26-
` + styles.CobraItemNameColor().SetString(constants.CliTerminalMode).String() + ` ` + i18n.T(i18n.CliTerminalModeEnvVar))
27-
28-
fmt.Println(styles.CobraSectionColor().SetString(`
29-
Curently used variables:`).String())
30-
31-
body := &uxBlock.TableBody{}
32-
guestInfoPart(body)
33-
cmdData.UxBlocks.Table(body)
20+
cmdData.Stdout.PrintLines(
21+
printer.Style(styles.CobraSectionColor(), i18n.T(i18n.GlobalEnvVariables)),
22+
printer.Style(styles.CobraItemNameColor(), constants.CliLogFilePathEnvVar)+"\t"+i18n.T(i18n.CliLogFilePathEnvVar),
23+
printer.Style(styles.CobraItemNameColor(), constants.CliDataFilePathEnvVar)+"\t"+i18n.T(i18n.CliDataFilePathEnvVar),
24+
printer.Style(styles.CobraItemNameColor(), constants.CliTerminalMode)+"\t"+i18n.T(i18n.CliTerminalModeEnvVar),
25+
printer.EmptyLine,
26+
printer.Style(styles.CobraSectionColor(), i18n.T(i18n.CurrentlyUsedEnvVariables)),
27+
)
28+
29+
body := uxBlock.NewTableBody()
30+
guestInfoPart(body)
31+
cmdData.UxBlocks.Table(body)
3432

3533
return nil
3634
})
37-
}
35+
}
36+
37+
func guestInfoPart(tableBody *uxBlock.TableBody) {
38+
cliDataFilePath, _, err := constants.CliDataFilePath()
39+
if err != nil {
40+
cliDataFilePath = err.Error()
41+
}
42+
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoCliDataFilePath), cliDataFilePath)
43+
44+
logFilePath, _, err := constants.LogFilePath()
45+
if err != nil {
46+
logFilePath = err.Error()
47+
}
48+
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoLogFilePath), logFilePath)
49+
50+
wgConfigFilePath, _, err := constants.WgConfigFilePath()
51+
if err != nil {
52+
wgConfigFilePath = err.Error()
53+
}
54+
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoWgConfigFilePath), wgConfigFilePath)
55+
}

src/cmd/logout.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,34 @@ import (
44
"context"
55

66
"github.com/zeropsio/zcli/src/cmdBuilder"
7-
"github.com/zeropsio/zcli/src/cliStorage"
87
"github.com/zeropsio/zcli/src/i18n"
9-
"github.com/zeropsio/zcli/src/uxBlock/styles"
8+
"github.com/zeropsio/zcli/src/uxBlock/styles"
109
)
1110

12-
var logout string
13-
1411
func logoutCmd() *cmdBuilder.Cmd {
1512
return cmdBuilder.NewCmd().
1613
Use("logout").
1714
Short(i18n.T(i18n.CmdDescLogout)).
1815
HelpFlag(i18n.T(i18n.CmdHelpLogout)).
1916
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
20-
21-
uxBlocks := cmdData.UxBlocks
22-
23-
_, err := cmdData.RestApiClient.PostAuthLogout(ctx)
24-
if err != nil {
25-
return err
26-
}
27-
28-
_, err = cmdData.CliStorage.Update(func(data cliStorage.Data) cliStorage.Data {
29-
return cliStorage.Data{}
30-
})
31-
if err != nil {
32-
return err
33-
}
34-
35-
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.LogoutVpnDisconnecting)))
36-
disconnectVpn(ctx, cmdData.UxBlocks) // TODO: ask - there is no need for any declaration - i can just call this function from anywhere?
37-
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.LogoutSuccess)))
38-
39-
return nil
40-
41-
})
42-
}
17+
uxBlocks := cmdData.UxBlocks
18+
19+
_, err := cmdData.RestApiClient.PostAuthLogout(ctx)
20+
if err != nil {
21+
return err
22+
}
23+
24+
_, err = cmdData.CliStorage.Clear()
25+
if err != nil {
26+
return err
27+
}
28+
29+
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.LogoutVpnDisconnecting)))
30+
if isVpnUp(ctx, uxBlocks, 1) {
31+
_ = disconnectVpn(ctx, uxBlocks)
32+
}
33+
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.LogoutSuccess)))
34+
35+
return nil
36+
})
37+
}

src/cmd/root.go

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ import (
66

77
"github.com/zeropsio/zcli/src/cmd/scope"
88
"github.com/zeropsio/zcli/src/cmdBuilder"
9-
"github.com/zeropsio/zcli/src/constants"
109
"github.com/zeropsio/zcli/src/entity/repository"
1110
"github.com/zeropsio/zcli/src/errorsx"
1211
"github.com/zeropsio/zcli/src/i18n"
13-
"github.com/zeropsio/zcli/src/uxBlock"
12+
"github.com/zeropsio/zcli/src/printer"
1413
"github.com/zeropsio/zcli/src/uxBlock/styles"
1514
"github.com/zeropsio/zerops-go/errorCode"
1615
)
1716

18-
func ExecuteCmd() error {
19-
return cmdBuilder.ExecuteRootCmd(rootCmd())
17+
func ExecuteCmd() {
18+
cmdBuilder.ExecuteRootCmd(rootCmd())
2019
}
2120

2221
func rootCmd() *cmdBuilder.Cmd {
@@ -34,29 +33,19 @@ func rootCmd() *cmdBuilder.Cmd {
3433
AddChildrenCmd(statusShowDebugLogsCmd()).
3534
AddChildrenCmd(servicePushCmd()).
3635
AddChildrenCmd(envCmd()).
37-
AddChildrenCmd(supportCmd()).
36+
AddChildrenCmd(supportCmd()).
3837
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
39-
fmt.Println(`Welcome to zCli by Zerops!
40-
41-
To unlock the full potential of zCLI, you need to log in using your Zerops account.
42-
Logging in enables you to access various features and interact with Zerops services seamlessly.
43-
44-
To log in, simply use the following command: zcli login <your_token>
45-
Replace <your_token> with the authentication token generated from your Zerops account.
46-
Once logged in, you'll be able to manage projects, deploy applications, configure VPN,
47-
and much more directly from the command line interface.
48-
49-
If you encounter any issues during the login process or have any questions,
50-
feel free to find out how to contact our support team by running 'zcli support'.
51-
`)
38+
cmdData.Stdout.PrintLines(
39+
i18n.T(i18n.GuestWelcome),
40+
printer.EmptyLine,
41+
)
5242

5343
// print the default command help
5444
cmdData.PrintHelp()
5545

5646
return nil
5747
}).
5848
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
59-
6049
var loggedUser string
6150
if info, err := cmdData.RestApiClient.GetUserInfo(ctx); err != nil {
6251
loggedUser = err.Error()
@@ -68,8 +57,7 @@ feel free to find out how to contact our support team by running 'zcli support'.
6857
}
6958
}
7059

71-
72-
// TODO: krls - check whole block
60+
// TODO: krls - check whole block
7361
if cmdData.CliStorage.Data().ScopeProjectId.Filled() {
7462
// project scope is set
7563
projectId, _ := cmdData.CliStorage.Data().ScopeProjectId.Get()
@@ -81,21 +69,24 @@ feel free to find out how to contact our support team by running 'zcli support'.
8169
return err
8270
}
8371
} else {
84-
fmt.Print(i18n.T(i18n.ScopedProject), err.Error())
72+
cmdData.Stderr.PrintLines(i18n.T(i18n.ScopedProject), err.Error())
8573
}
8674
} else {
87-
fmt.Print(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native()))
75+
cmdData.Stdout.PrintLines(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native()))
8876
}
8977
}
9078

91-
var vpnStatusText string
79+
var vpnStatusText string
9280
if isVpnUp(ctx, cmdData.UxBlocks, 1) {
9381
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsActive)
9482
} else {
9583
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsNotActive)
9684
}
9785

98-
fmt.Printf("Welcome in Zerops!\nYou are loged as %s \nand your %s.\n\n", loggedUser, vpnStatusText)
86+
cmdData.Stdout.PrintLines(
87+
i18n.T(i18n.LoggedWelcome, loggedUser, vpnStatusText),
88+
printer.EmptyLine,
89+
)
9990

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

107-
func guestInfoPart(tableBody *uxBlock.TableBody) {
108-
cliDataFilePath, _, err := constants.CliDataFilePath()
109-
if err != nil {
110-
cliDataFilePath = err.Error()
111-
}
112-
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoCliDataFilePath), cliDataFilePath)
113-
114-
logFilePath, _, err := constants.LogFilePath()
115-
if err != nil {
116-
logFilePath = err.Error()
117-
}
118-
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoLogFilePath), logFilePath)
119-
120-
wgConfigFilePath, _, err := constants.WgConfigFilePath()
121-
if err != nil {
122-
wgConfigFilePath = err.Error()
123-
}
124-
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoWgConfigFilePath), wgConfigFilePath)
125-
}
126-
12798
func getRootTemplate() string {
12899
return styles.CobraSectionColor().SetString("Usage:").String() + `{{if .Runnable}}
129100
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}

src/cmd/support.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/zeropsio/zcli/src/cmdBuilder"
87
"github.com/zeropsio/zcli/src/i18n"
8+
"github.com/zeropsio/zcli/src/printer"
9+
"github.com/zeropsio/zcli/src/uxBlock/styles"
910
)
1011

11-
var support string
12-
1312
func supportCmd() *cmdBuilder.Cmd {
1413
return cmdBuilder.NewCmd().
1514
Use("support").
1615
Short(i18n.T(i18n.CmdDescSupport)).
1716
HelpFlag(i18n.T(i18n.CmdHelpSupport)).
1817
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
19-
20-
21-
fmt.Println("You can contact Zerops support via:")
22-
fmt.Println("- E-mail: [email protected]")
23-
fmt.Println("- Discord: https://discord.com/invite/WDvCZ54")
24-
fmt.Println(`
25-
Additionally, you can explore our documentation
26-
at https://docs.zerops.io/references/cli for further details.
27-
`)
28-
return nil
29-
18+
cmdData.Stdout.PrintLines(
19+
printer.Style(styles.CobraSectionColor(), i18n.T(i18n.Contact)),
20+
printer.Style(styles.CobraItemNameColor(), "- E-mail")+": [email protected]",
21+
printer.Style(styles.CobraItemNameColor(), "- Discord")+": https://discord.com/invite/WDvCZ54",
22+
printer.EmptyLine,
23+
i18n.T(i18n.Documentation),
24+
)
3025
return nil
3126
})
3227
}
33-

src/cmd/version.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"runtime"
76

87
"github.com/zeropsio/zcli/src/cmdBuilder"
@@ -17,7 +16,7 @@ func versionCmd() *cmdBuilder.Cmd {
1716
Short(i18n.T(i18n.CmdDescVersion)).
1817
HelpFlag(i18n.T(i18n.CmdHelpVersion)).
1918
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
20-
fmt.Printf("zcli version %s (%s) %s/%s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
19+
cmdData.Stdout.Printf("zcli version %s (%s) %s/%s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
2120

2221
return nil
2322
})

src/cmd/vpnUp.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ func vpnUpCmd() *cmdBuilder.Cmd {
132132
return err
133133
}
134134

135-
136135
// wait for the vpn to be up
137136
if isVpnUp(ctx, uxBlocks, 6) {
138137
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.VpnUp)))
139138
} else {
140139
uxBlocks.PrintWarning(styles.WarningLine(i18n.T(i18n.VpnPingFailed)))
141140
}
142141

143-
144142
return nil
145143
})
146144
}

0 commit comments

Comments
 (0)