Skip to content

Commit

Permalink
fix: errors and tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-hajek committed Mar 29, 2024
1 parent 6ce7b12 commit 770f168
Show file tree
Hide file tree
Showing 34 changed files with 866 additions and 511 deletions.
4 changes: 2 additions & 2 deletions src/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
func loginCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("login").
Short(i18n.T(i18n.CmdLogin)).
Short(i18n.T(i18n.CmdDescLogin)).
StringFlag("regionUrl", constants.DefaultRegionUrl, i18n.T(i18n.RegionUrlFlag), cmdBuilder.HiddenFlag()).
StringFlag("region", "", i18n.T(i18n.RegionFlag), cmdBuilder.HiddenFlag()).
HelpFlag(i18n.T(i18n.LoginHelp)).
HelpFlag(i18n.T(i18n.CmdHelpLogin)).
Arg("token").
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
uxBlocks := cmdData.UxBlocks
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
func projectCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("project").
Short(i18n.T(i18n.CmdProject)).
HelpFlag(i18n.T(i18n.ProjectHelp)).
Short(i18n.T(i18n.CmdDescProject)).
HelpFlag(i18n.T(i18n.CmdHelpProject)).
AddChildrenCmd(projectListCmd()).
AddChildrenCmd(projectDeleteCmd()).
AddChildrenCmd(projectServiceImportCmd()).
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/projectDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
func projectDeleteCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("delete").
Short(i18n.T(i18n.CmdProjectDelete)).
Short(i18n.T(i18n.CmdDescProjectDelete)).
ScopeLevel(scope.Project).
Arg(scope.ProjectArgName, cmdBuilder.OptionalArg()).
BoolFlag("confirm", false, i18n.T(i18n.ConfirmFlag)).
HelpFlag(i18n.T(i18n.ProjectDeleteHelp)).
HelpFlag(i18n.T(i18n.CmdHelpProjectDelete)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
if !cmdData.Params.GetBool("confirm") {
confirmed, err := uxHelpers.YesNoPrompt(
Expand Down Expand Up @@ -58,7 +58,7 @@ func projectDeleteCmd() *cmdBuilder.Cmd {
[]uxHelpers.Process{{
F: uxHelpers.CheckZeropsProcess(processId, cmdData.RestApiClient),
RunningMessage: i18n.T(i18n.ProjectDeleting),
ErrorMessageMessage: i18n.T(i18n.ProjectDeleting),
ErrorMessageMessage: i18n.T(i18n.ProjectDeleteFailed),
SuccessMessage: i18n.T(i18n.ProjectDeleted),
}},
)
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/projectImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const projectImportArgName = "importYamlPath"
func projectImportCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("project-import").
Short(i18n.T(i18n.CmdProjectImport)).
Long(i18n.T(i18n.CmdProjectImportLong)).
Short(i18n.T(i18n.CmdDescProjectImport)).
Long(i18n.T(i18n.CmdDescProjectImportLong)).
Arg(projectImportArgName).
StringFlag("orgId", "", i18n.T(i18n.OrgIdFlag)).
StringFlag("workingDie", "./", i18n.T(i18n.BuildWorkingDir)).
HelpFlag(i18n.T(i18n.ProjectImportHelp)).
HelpFlag(i18n.T(i18n.CmdHelpProjectImport)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/projectList.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
func projectListCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("list").
Short(i18n.T(i18n.CmdProjectList)).
HelpFlag(i18n.T(i18n.ProjectListHelp)).
Short(i18n.T(i18n.CmdDescProjectList)).
HelpFlag(i18n.T(i18n.CmdHelpProjectList)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
err := uxHelpers.PrintProjectList(ctx, cmdData.UxBlocks, cmdData.RestApiClient)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/projectServiceImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const serviceImportArgName = "importYamlPath"
func projectServiceImportCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("service-import").
Short(i18n.T(i18n.CmdServiceImport)).
Short(i18n.T(i18n.CmdDescProjectServiceImport)).
ScopeLevel(scope.Project).
Arg(serviceImportArgName).
HelpFlag(i18n.T(i18n.ProjectServiceImportHelp)).
HelpFlag(i18n.T(i18n.CmdHelpProjectServiceImport)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand Down
13 changes: 4 additions & 9 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package cmd
import (
"context"
"fmt"
"time"

"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/nettools"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zerops-go/errorCode"
Expand Down Expand Up @@ -71,7 +69,7 @@ func rootCmd() *cmdBuilder.Cmd {
projectId, _ := cmdData.CliStorage.Data().ScopeProjectId.Get()
project, err := repository.GetProjectById(ctx, cmdData.RestApiClient, projectId)
if err != nil {
if errorsx.Check(err, errorsx.CheckErrorCode(errorCode.ProjectNotFound)) {
if errorsx.Is(err, errorsx.ErrorCode(errorCode.ProjectNotFound)) {
err := scope.ProjectScopeReset(cmdData)
if err != nil {
return err
Expand All @@ -84,13 +82,10 @@ func rootCmd() *cmdBuilder.Cmd {
}
}

pingCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

if err := nettools.Ping(pingCtx, vpnCheckAddress); err != nil {
body.AddStringsRow(i18n.T(i18n.StatusInfoVpnStatus), i18n.T(i18n.VpnCheckingConnectionIsNotActive))
} else {
if isVpnUp(ctx) {
body.AddStringsRow(i18n.T(i18n.StatusInfoVpnStatus), i18n.T(i18n.VpnCheckingConnectionIsActive))
} else {
body.AddStringsRow(i18n.T(i18n.StatusInfoVpnStatus), i18n.T(i18n.VpnCheckingConnectionIsNotActive))
}

cmdData.UxBlocks.Table(body)
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
func scopeCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("scope").
Short(i18n.T(i18n.CmdScope)).
HelpFlag(i18n.T(i18n.ScopeHelp)).
Short(i18n.T(i18n.CmdDescScope)).
HelpFlag(i18n.T(i18n.CmdHelpScope)).
AddChildrenCmd(scopeProjectCmd()).
AddChildrenCmd(scopeResetCmd())
}
21 changes: 18 additions & 3 deletions src/cmd/scope/scopeProject.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/uxHelpers"
"github.com/zeropsio/zerops-go/apiError"
"github.com/zeropsio/zerops-go/errorCode"
"github.com/zeropsio/zerops-go/types/uuid"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func (p *project) LoadSelectedScope(ctx context.Context, cmd *cmdBuilder.Cmd, cm

project, err = repository.GetProjectById(ctx, cmdData.RestApiClient, projectId)
if err != nil {
if errorsx.Check(err, errorsx.CheckErrorCode(errorCode.ProjectNotFound)) {
if errorsx.Is(err, errorsx.ErrorCode(errorCode.ProjectNotFound)) {
err := ProjectScopeReset(cmdData)
if err != nil {
return err
Expand All @@ -57,7 +58,14 @@ func (p *project) LoadSelectedScope(ctx context.Context, cmd *cmdBuilder.Cmd, cm
if err != nil {
return errorsx.Convert(
err,
errorsx.ConvertInvalidUserInput("id", i18n.T(i18n.ErrorInvalidProjectId)),
errorsx.InvalidUserInput(
"id",
errorsx.InvalidUserInputErrorMessage(
func(_ apiError.Error, metaItemTyped map[string]interface{}) string {
return i18n.T(i18n.ErrorInvalidProjectId, projectId, metaItemTyped["message"])
},
),
),
)
}

Expand All @@ -70,7 +78,14 @@ func (p *project) LoadSelectedScope(ctx context.Context, cmd *cmdBuilder.Cmd, cm
if err != nil {
return errorsx.Convert(
err,
errorsx.ConvertInvalidUserInput("id", i18n.T(i18n.ErrorInvalidProjectId)),
errorsx.InvalidUserInput(
"id",
errorsx.InvalidUserInputErrorMessage(
func(_ apiError.Error, metaItemTyped map[string]interface{}) string {
return i18n.T(i18n.ErrorInvalidProjectId, projectId, metaItemTyped["message"])
},
),
),
)
}

Expand Down
10 changes: 9 additions & 1 deletion src/cmd/scope/scopeService.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/uxHelpers"
"github.com/zeropsio/zerops-go/apiError"
"github.com/zeropsio/zerops-go/types/uuid"
)

Expand Down Expand Up @@ -50,7 +51,14 @@ func (s *service) LoadSelectedScope(ctx context.Context, _ *cmdBuilder.Cmd, cmdD
if err != nil {
return errorsx.Convert(
err,
errorsx.ConvertInvalidUserInput("id", i18n.T(i18n.ErrorInvalidServiceId)),
errorsx.InvalidUserInput(
"id",
errorsx.InvalidUserInputErrorMessage(
func(_ apiError.Error, metaItemTyped map[string]interface{}) string {
return i18n.T(i18n.ErrorInvalidServiceId, serviceId, metaItemTyped["message"])
},
),
),
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/scopeProject.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
func scopeProjectCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("project").
Short(i18n.T(i18n.CmdScopeProject)).
Short(i18n.T(i18n.CmdDescScopeProject)).
Arg(scope.ProjectArgName, cmdBuilder.OptionalArg()).
HelpFlag(i18n.T(i18n.ScopeProjectHelp)).
HelpFlag(i18n.T(i18n.CmdHelpScopeProject)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
projectId, projectSet := cmdData.CliStorage.Data().ScopeProjectId.Get()
if projectSet {
project, err := repository.GetProjectById(ctx, cmdData.RestApiClient, projectId)
if err != nil {
if errorsx.Check(err, errorsx.CheckErrorCode(errorCode.ProjectNotFound)) {
if errorsx.Is(err, errorsx.ErrorCode(errorCode.ProjectNotFound)) {
cmdData.UxBlocks.PrintWarning(styles.WarningLine(err.Error()))
} else {
return err
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/scopeReset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
func scopeResetCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("reset").
Short(i18n.T(i18n.CmdScopeReset)).
HelpFlag(i18n.T(i18n.ScopeResetHelp)).
Short(i18n.T(i18n.CmdDescScopeReset)).
HelpFlag(i18n.T(i18n.CmdHelpScopeReset)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
err := scope.ProjectScopeReset(cmdData)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
func serviceCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("service").
Short(i18n.T(i18n.CmdService)).
HelpFlag(i18n.T(i18n.ServiceHelp)).
Short(i18n.T(i18n.CmdDescService)).
HelpFlag(i18n.T(i18n.CmdHelpService)).
AddChildrenCmd(serviceDeleteCmd()).
AddChildrenCmd(serviceListCmd()).
AddChildrenCmd(serviceLogCmd()).
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/serviceDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
func serviceDeleteCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("delete").
Short(i18n.T(i18n.CmdServiceDelete)).
Short(i18n.T(i18n.CmdDescServiceDelete)).
ScopeLevel(scope.Service).
Arg(scope.ServiceArgName, cmdBuilder.OptionalArg()).
BoolFlag("confirm", false, i18n.T(i18n.ConfirmFlag)).
HelpFlag(i18n.T(i18n.ServiceDeleteHelp)).
HelpFlag(i18n.T(i18n.CmdHelpServiceDelete)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
if !cmdData.Params.GetBool("confirm") {
confirmed, err := uxHelpers.YesNoPrompt(
Expand Down Expand Up @@ -58,7 +58,7 @@ func serviceDeleteCmd() *cmdBuilder.Cmd {
[]uxHelpers.Process{{
F: uxHelpers.CheckZeropsProcess(processId, cmdData.RestApiClient),
RunningMessage: i18n.T(i18n.ServiceDeleting),
ErrorMessageMessage: i18n.T(i18n.ServiceDeleting),
ErrorMessageMessage: i18n.T(i18n.ServiceDeleteFailed),
SuccessMessage: i18n.T(i18n.ServiceDeleted),
}},
)
Expand Down
22 changes: 14 additions & 8 deletions src/cmd/serviceDeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
func serviceDeployCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("deploy").
Short(i18n.T(i18n.CmdDeployDesc)).
Long(i18n.T(i18n.CmdDeployDesc)+"\n\n"+i18n.T(i18n.DeployDescLong)+"\n\n"+i18n.T(i18n.DeployHintPush)).
Short(i18n.T(i18n.CmdDescDeploy)).
Long(i18n.T(i18n.CmdDescDeployLong)).
ScopeLevel(scope.Service).
Arg("pathToFileOrDir", cmdBuilder.ArrayArg()).
StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)).
StringFlag("archiveFilePath", "", i18n.T(i18n.BuildArchiveFilePath)).
StringFlag("versionName", "", i18n.T(i18n.BuildVersionName)).
StringFlag("zeropsYamlPath", "", i18n.T(i18n.ZeropsYamlLocation)).
BoolFlag("deployGitFolder", false, i18n.T(i18n.ZeropsYamlLocation)).
HelpFlag(i18n.T(i18n.ServiceDeployHelp)).
HelpFlag(i18n.T(i18n.CmdHelpServiceDeploy)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

Expand All @@ -53,7 +53,7 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
return err
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.BuildDeployCreatingPackageStart)))
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.PushDeployCreatingPackageStart)))

appVersion, err := createAppVersion(
ctx,
Expand Down Expand Up @@ -132,16 +132,22 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
}
return nil
},
RunningMessage: i18n.T(i18n.BuildDeployUploadingPackageStart),
ErrorMessageMessage: i18n.T(i18n.BuildDeployUploadPackageFailed),
SuccessMessage: i18n.T(i18n.BuildDeployUploadingPackageDone),
RunningMessage: i18n.T(i18n.PushDeployUploadingPackageStart),
ErrorMessageMessage: i18n.T(i18n.PushDeployUploadPackageFailed),
SuccessMessage: i18n.T(i18n.PushDeployUploadingPackageDone),
}},
)
if err != nil {
return err
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.BuildDeployDeployingStart)))
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.PushDeployCreatingPackageDone)))

if cmdData.Params.GetString("archiveFilePath") != "" {
uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.PushDeployPackageSavedInto, cmdData.Params.GetString("archiveFilePath"))))
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.PushDeployDeployingStart)))

deployResponse, err := cmdData.RestApiClient.PutAppVersionDeploy(
ctx,
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/serviceEnableSubdomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
func serviceEnableSubdomainCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("enable-subdomain").
Short(i18n.T(i18n.CmdServiceEnableSubdomain)).
Short(i18n.T(i18n.CmdDescServiceEnableSubdomain)).
ScopeLevel(scope.Service).
Arg(scope.ServiceArgName, cmdBuilder.OptionalArg()).
HelpFlag(i18n.T(i18n.ServiceEnableSubdomainHelp)).
HelpFlag(i18n.T(i18n.CmdHelpServiceEnableSubdomain)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
enableSubdomainResponse, err := cmdData.RestApiClient.PutServiceStackEnableSubdomainAccess(
ctx,
Expand All @@ -42,7 +42,7 @@ func serviceEnableSubdomainCmd() *cmdBuilder.Cmd {
[]uxHelpers.Process{{
F: uxHelpers.CheckZeropsProcess(processId, cmdData.RestApiClient),
RunningMessage: i18n.T(i18n.ServiceEnablingSubdomain),
ErrorMessageMessage: i18n.T(i18n.ServiceEnablingSubdomain),
ErrorMessageMessage: i18n.T(i18n.ServiceEnableSubdomainFailed),
SuccessMessage: i18n.T(i18n.ServiceEnabledSubdomain),
}},
)
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/serviceList.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
func serviceListCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("list").
Short(i18n.T(i18n.CmdServiceList)).
Short(i18n.T(i18n.CmdDescServiceList)).
ScopeLevel(scope.Project).
Arg(scope.ProjectArgName, cmdBuilder.OptionalArg()).
HelpFlag(i18n.T(i18n.ServiceListHelp)).
HelpFlag(i18n.T(i18n.CmdHelpServiceList)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
err := uxHelpers.PrintServiceList(ctx, cmdData.UxBlocks, cmdData.RestApiClient, *cmdData.Project)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/serviceLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func serviceLogCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("log").
Short(i18n.T(i18n.CmdServiceLog)).
Long(i18n.T(i18n.CmdServiceLogLong)+i18n.T(i18n.ServiceLogAdditional)).
Short(i18n.T(i18n.CmdDescServiceLog)).
Long(i18n.T(i18n.CmdDescServiceLogLong)).
ScopeLevel(scope.Service).
IntFlag("limit", 100, i18n.T(i18n.LogLimitFlag)).
StringFlag("minimumSeverity", "", i18n.T(i18n.LogMinSeverityFlag)).
Expand All @@ -26,7 +26,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)).
HelpFlag(i18n.T(i18n.ServiceLogHelp)).
HelpFlag(i18n.T(i18n.CmdHelpServiceLog)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
handler := serviceLogs.New(
serviceLogs.Config{},
Expand Down
Loading

0 comments on commit 770f168

Please sign in to comment.