Skip to content

Commit

Permalink
fix helps
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-hajek committed Feb 21, 2024
1 parent cd12a0e commit 04722ac
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 200 deletions.
1 change: 1 addition & 0 deletions src/cmd/scopeProject.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func scopeProjectCmd() *cmdBuilder.Cmd {
Use("project").
Short(i18n.T(i18n.CmdScopeProject)).
Arg(cmdBuilder.ProjectArgName, cmdBuilder.OptionalArg()).
HelpFlag(i18n.T(i18n.ScopeProjectHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
projectId, projectSet := cmdData.CliStorage.Data().ScopeProjectId.Get()
if projectSet {
Expand Down
1 change: 1 addition & 0 deletions src/cmd/scopeReset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func scopeResetCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("reset").
Short(i18n.T(i18n.CmdScopeReset)).
HelpFlag(i18n.T(i18n.ScopeResetHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
_, err := cmdData.CliStorage.Update(func(data cliStorage.Data) cliStorage.Data {
data.ScopeProjectId = uuid.ProjectIdNull{}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func serviceCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("service").
Short(i18n.T(i18n.CmdService)).
Short(i18n.T(i18n.ServiceHelp)).
HelpFlag(i18n.T(i18n.ServiceHelp)).
AddChildrenCmd(serviceDeleteCmd()).
AddChildrenCmd(serviceListCmd()).
AddChildrenCmd(serviceLogCmd()).
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/serviceDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func serviceDeleteCmd() *cmdBuilder.Cmd {
ScopeLevel(cmdBuilder.Service).
Arg(cmdBuilder.ServiceArgName, cmdBuilder.OptionalArg()).
BoolFlag("confirm", false, i18n.T(i18n.ConfirmFlag)).
Short(i18n.T(i18n.ServiceDeleteHelp)).
HelpFlag(i18n.T(i18n.ServiceDeleteHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
if !cmdData.Params.GetBool("confirm") {
err := uxHelpers.YesNoPromptDestructive(ctx, cmdData.UxBlocks, i18n.T(i18n.ServiceDeleteConfirm, cmdData.Service.Name))
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/serviceDeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
StringFlag("versionName", "", i18n.T(i18n.BuildVersionName)).
StringFlag("zeropsYamlPath", "", i18n.T(i18n.ZeropsYamlLocation)).
BoolFlag("deployGitFolder", false, i18n.T(i18n.ZeropsYamlLocation)).
Short(i18n.T(i18n.ServiceDeployHelp)).
HelpFlag(i18n.T(i18n.ServiceDeployHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/serviceList.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func serviceListCmd() *cmdBuilder.Cmd {
Short(i18n.T(i18n.CmdProjectList)).
ScopeLevel(cmdBuilder.Project).
Arg(cmdBuilder.ProjectArgName, cmdBuilder.OptionalArg()).
Short(i18n.T(i18n.ServiceListHelp)).
HelpFlag(i18n.T(i18n.ServiceListHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
err := uxHelpers.PrintServiceList(ctx, cmdData.UxBlocks, cmdData.RestApiClient, *cmdData.Project)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/serviceLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func serviceLogCmd() *cmdBuilder.Cmd {
StringFlag("formatTemplate", "", i18n.T(i18n.LogFormatTemplateFlag)).
BoolFlag("follow", false, i18n.T(i18n.LogFollowFlag)).
BoolFlag("showBuildLogs", false, i18n.T(i18n.LogShowBuildFlag)).
Short(i18n.T(i18n.ServiceLogHelp)).
HelpFlag(i18n.T(i18n.ServiceLogHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
handler := serviceLogs.New(
serviceLogs.Config{},
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/servicePush.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func servicePushCmd() *cmdBuilder.Cmd {
StringFlag("source", "", i18n.T(i18n.SourceName)).
StringFlag("zeropsYamlPath", "", i18n.T(i18n.ZeropsYamlLocation)).
BoolFlag("deployGitFolder", false, i18n.T(i18n.UploadGitFolder)).
Short(i18n.T(i18n.ServicePushHelp)).
HelpFlag(i18n.T(i18n.ServicePushHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/serviceStart.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func serviceStartCmd() *cmdBuilder.Cmd {
Short(i18n.T(i18n.CmdServiceStart)).
ScopeLevel(cmdBuilder.Service).
Arg(cmdBuilder.ServiceArgName, cmdBuilder.OptionalArg(), cmdBuilder.OptionalArgLabel("{serviceName | serviceId}")).
Short(i18n.T(i18n.ServiceStartHelp)).
HelpFlag(i18n.T(i18n.ServiceStartHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
startServiceResponse, err := cmdData.RestApiClient.PutServiceStackStart(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/serviceStop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func serviceStopCmd() *cmdBuilder.Cmd {
Short(i18n.T(i18n.CmdServiceStop)).
ScopeLevel(cmdBuilder.Service).
Arg(cmdBuilder.ServiceArgName, cmdBuilder.OptionalArg()).
Short(i18n.T(i18n.ServiceStopHelp)).
HelpFlag(i18n.T(i18n.ServiceStopHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
stopServiceResponse, err := cmdData.RestApiClient.PutServiceStackStop(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func statusCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("status").
Short(i18n.T(i18n.CmdStatus)).
Short(i18n.T(i18n.StatusHelp)).
HelpFlag(i18n.T(i18n.StatusHelp)).
AddChildrenCmd(statusShowDebugLogsCmd()).
AddChildrenCmd(statusInfoCmd())
}
2 changes: 1 addition & 1 deletion src/cmd/statusInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func statusInfoCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("info").
Short(i18n.T(i18n.CmdStatusInfo)).
Short(i18n.T(i18n.StatusInfoHelp)).
HelpFlag(i18n.T(i18n.StatusInfoHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
body := &uxBlock.TableBody{}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/statusShowDebugLogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func statusShowDebugLogsCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("show-debug-logs").
Short(i18n.T(i18n.CmdStatusShowDebugLogs)).
Short(i18n.T(i18n.StatusShowDebugLogsHelp)).
HelpFlag(i18n.T(i18n.StatusShowDebugLogsHelp)).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
logFilePath, err := constants.LogFilePath()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func versionCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("version").
Short(i18n.T(i18n.CmdVersion)).
Short(i18n.T(i18n.VersionHelp)).
HelpFlag(i18n.T(i18n.VersionHelp)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
fmt.Printf("zcli version %s (%s) %s/%s\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)

Expand Down
13 changes: 10 additions & 3 deletions src/cmdBuilder/cmdBuilderBuildCobraCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/zeropsio/zcli/src/cliStorage"
"github.com/zeropsio/zcli/src/params"
"github.com/zeropsio/zcli/src/uxBlock"
)

func (b *CmdBuilder) buildCobraCmd(cmd *Cmd, params *params.Handler) (*cobra.Command, error) {
func (b *CmdBuilder) buildCobraCmd(
cmd *Cmd,
params *params.Handler,
uxBlocks uxBlock.UxBlocks,
cliStorage *cliStorage.Handler,
) (*cobra.Command, error) {
cobraCmd := &cobra.Command{
Short: cmd.short,
SilenceUsage: cmd.silenceUsage,
Expand Down Expand Up @@ -52,11 +59,11 @@ func (b *CmdBuilder) buildCobraCmd(cmd *Cmd, params *params.Handler) (*cobra.Com
}

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

for _, childrenCmd := range cmd.childrenCmds {
cobraChildrenCmd, err := b.buildCobraCmd(childrenCmd, params)
cobraChildrenCmd, err := b.buildCobraCmd(childrenCmd, params, uxBlocks, cliStorage)
if err != nil {
return nil, err
}
Expand Down
131 changes: 7 additions & 124 deletions src/cmdBuilder/cmdBuilderCreateRunFunc.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
package cmdBuilder

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/zeropsio/zcli/src/cliStorage"
"github.com/zeropsio/zcli/src/constants"
"github.com/zeropsio/zcli/src/entity"
"github.com/zeropsio/zcli/src/errorsx"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/logger"
"github.com/zeropsio/zcli/src/params"
"github.com/zeropsio/zcli/src/storage"
"github.com/zeropsio/zcli/src/support"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/zeropsRestApiClient"
"github.com/zeropsio/zerops-go/apiError"
"golang.org/x/term"
"gopkg.in/yaml.v3"
)

type ParamsReader interface {
Expand Down Expand Up @@ -73,43 +59,19 @@ type LoggedUserCmdData struct {
Service *entity.Service
}

func (b *CmdBuilder) createCmdRunFunc(cmd *Cmd, params *params.Handler) func(*cobra.Command, []string) error {
func (b *CmdBuilder) createCmdRunFunc(
cmd *Cmd,
params *params.Handler,
uxBlocks uxBlock.UxBlocks,
cliStorage *cliStorage.Handler,
) func(*cobra.Command, []string) error {
return func(cobraCmd *cobra.Command, args []string) (err error) {
ctx, cancel := context.WithCancel(context.Background())
regSignals(cancel)
ctx = support.Context(ctx)

isTerminal, err := isTerminal()
if err != nil {
return err
}

width, _, err := term.GetSize(0)
if err != nil {
width = 100
}

outputLogger, debugFileLogger := createLoggers(isTerminal)

uxBlocks := uxBlock.NewBlock(outputLogger, debugFileLogger, isTerminal, width, cancel)
ctx := cobraCmd.Context()

uxBlocks.LogDebug(fmt.Sprintf("Command: %s", cobraCmd.CommandPath()))

defer func() {
if err != nil {
printError(err, uxBlocks)
err = errSkipErrorReporting
}
}()

// TODO - janhajek move somewhere else?
params.InitViper()

cliStorage, err := createCliStorage()
if err != nil {
return err
}

argsMap, err := convertArgs(cmd, args)
if err != nil {
return err
Expand Down Expand Up @@ -187,82 +149,3 @@ func convertArgs(cmd *Cmd, args []string) (map[string][]string, error) {

return argsMap, nil
}

func printError(err error, uxBlocks uxBlock.UxBlocks) {
uxBlocks.LogDebug(fmt.Sprintf("error: %+v", err))

if userErr := errorsx.AsUserError(err); userErr != nil {
uxBlocks.PrintError(styles.ErrorLine(err.Error()))
return
}

var apiErr apiError.Error
if errors.As(err, &apiErr) {
uxBlocks.PrintError(styles.ErrorLine(apiErr.GetMessage()))
if apiErr.GetMeta() != nil {
meta, err := yaml.Marshal(apiErr.GetMeta())
if err != nil {
uxBlocks.PrintError(styles.ErrorLine(fmt.Sprintf("couldn't parse meta of error: %s", apiErr.GetMessage())))
}
uxBlocks.PrintError(styles.ErrorLine(string(meta)))
}

return
}

uxBlocks.PrintError(styles.ErrorLine(err.Error()))
}

func isTerminal() (bool, error) {
switch TerminalMode(TerminalFlag) {
case TerminalModeAuto:
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()), nil
case TerminalModeDisabled:
return false, nil
case TerminalModeEnabled:
return true, nil
default:
return false, errors.New(i18n.T(i18n.UnknownTerminalMode, TerminalFlag))
}
}

func createLoggers(isTerminal bool) (*logger.Handler, *logger.Handler) {
outputLogger := logger.NewOutputLogger(logger.OutputConfig{
IsTerminal: isTerminal,
})

loggerFilePath, err := constants.LogFilePath()
if err != nil {
outputLogger.Warning(styles.WarningLine(err.Error()))
}

debugFileLogger := logger.NewDebugFileLogger(logger.DebugFileConfig{
FilePath: loggerFilePath,
})

return outputLogger, debugFileLogger
}

func regSignals(contextCancel func()) {
sigs := make(chan os.Signal, 1)

signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

go func() {
<-sigs
contextCancel()
}()
}

func createCliStorage() (*cliStorage.Handler, error) {
filePath, err := constants.CliDataFilePath()
if err != nil {
return nil, err
}
s, err := storage.New[cliStorage.Data](
storage.Config{
FilePath: filePath,
},
)
return &cliStorage.Handler{Handler: s}, err
}
Loading

0 comments on commit 04722ac

Please sign in to comment.