From 705d994e8bb563fb1d8e96ea0b932e646055d3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=A1jek?= Date: Sat, 16 Mar 2024 15:33:09 +0100 Subject: [PATCH] fix: version, push source, scope reset (#132) Co-authored-by: jan.hajek@zerops.io --- .github/workflows/tag.yml | 10 +++++----- src/cmd/root.go | 17 +++++++++++------ src/cmd/scope/scopeProject.go | 30 +++++++++++++++++++++++------- src/cmd/scopeReset.go | 8 ++------ src/cmd/servicePush.go | 8 +------- src/cmd/version.go | 4 ++-- src/i18n/en.go | 1 - src/i18n/i18n.go | 1 - 8 files changed, 44 insertions(+), 35 deletions(-) diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index c13c81f8..6d434b29 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -13,7 +13,7 @@ jobs: include: - name: linux amd64 os: ubuntu-latest - buildCmd: env GOOS=linux GOARCH=amd64 go build -o builds/zcli-linux-amd64 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd/zcli.Version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go + buildCmd: env GOOS=linux GOARCH=amd64 go build -o builds/zcli-linux-amd64 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd.version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go file: zcli-linux-amd64 compress: true strip: true @@ -21,7 +21,7 @@ jobs: runTests: true - name: linux 386 os: ubuntu-latest - buildCmd: env GOOS=linux GOARCH=386 go build -o builds/zcli-linux-i386 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd/zcli.Version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go + buildCmd: env GOOS=linux GOARCH=386 go build -o builds/zcli-linux-i386 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd.version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go file: zcli-linux-i386 compress: true strip: true @@ -29,7 +29,7 @@ jobs: runTests: true - name: darwin amd64 os: macos-latest - buildCmd: env GOOS=darwin GOARCH=amd64 go build -o builds/zcli-darwin-amd64 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd/zcli.Version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go + buildCmd: env GOOS=darwin GOARCH=amd64 go build -o builds/zcli-darwin-amd64 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd.version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go file: zcli-darwin-amd64 compress: false strip: false @@ -37,7 +37,7 @@ jobs: runTests: true - name: darwin arm64 os: macos-latest - buildCmd: env GOOS=darwin GOARCH=arm64 go build -o builds/zcli-darwin-arm64 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd/zcli.Version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go + buildCmd: env GOOS=darwin GOARCH=arm64 go build -o builds/zcli-darwin-arm64 -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd.version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go file: zcli-darwin-arm64 compress: false strip: false @@ -45,7 +45,7 @@ jobs: runTests: false - name: windows amd64 os: ubuntu-latest - buildCmd: env GOOS=windows GOARCH=amd64 go build -o builds/zcli-win-x64.exe -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd/zcli.Version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go + buildCmd: env GOOS=windows GOARCH=amd64 go build -o builds/zcli-win-x64.exe -ldflags "-s -w -X github.com/zeropsio/zcli/src/cmd.version=${{ github.event.release.tag_name }}" ./cmd/zcli/main.go file: zcli-win-x64.exe compress: false strip: false diff --git a/src/cmd/root.go b/src/cmd/root.go index 05a0a297..6581dbae 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -5,6 +5,7 @@ import ( "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" @@ -13,6 +14,7 @@ import ( "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" ) func ExecuteCmd() error { @@ -66,14 +68,17 @@ func rootCmd() *cmdBuilder.Cmd { projectId, _ := cmdData.CliStorage.Data().ScopeProjectId.Get() project, err := repository.GetProjectById(ctx, cmdData.RestApiClient, projectId) if err != nil { - if errorsx.IsUserError(err) { - cmdData.UxBlocks.PrintWarning(styles.WarningLine(i18n.T(i18n.ScopedProjectNotFound))) + if errorsx.Check(err, errorsx.CheckErrorCode(errorCode.ProjectNotFound)) { + err := scope.ProjectScopeReset(cmdData) + if err != nil { + return err + } + } else { + body.AddStringsRow(i18n.T(i18n.ScopedProject), err.Error()) } - - return err + } else { + body.AddStringsRow(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native())) } - - body.AddStringsRow(i18n.T(i18n.ScopedProject), fmt.Sprintf("%s [%s]", project.Name.String(), project.ID.Native())) } ctx, cancel := context.WithTimeout(ctx, time.Second*5) diff --git a/src/cmd/scope/scopeProject.go b/src/cmd/scope/scopeProject.go index 80e7eb5f..372312a2 100644 --- a/src/cmd/scope/scopeProject.go +++ b/src/cmd/scope/scopeProject.go @@ -3,6 +3,7 @@ package scope import ( "context" + "github.com/zeropsio/zcli/src/cliStorage" "github.com/zeropsio/zcli/src/cmdBuilder" "github.com/zeropsio/zcli/src/entity" "github.com/zeropsio/zcli/src/entity/repository" @@ -38,14 +39,17 @@ func (p *project) LoadSelectedScope(ctx context.Context, cmd *cmdBuilder.Cmd, cm project, err = repository.GetProjectById(ctx, cmdData.RestApiClient, projectId) if err != nil { - return errorsx.Convert( - err, - errorsx.ConvertInvalidUserInput("id", i18n.T(i18n.ErrorInvalidScopedProjectId)), - errorsx.ConvertErrorCode(errorCode.ProjectNotFound, i18n.T(i18n.ScopedProjectNotFound)), - ) + if errorsx.Check(err, errorsx.CheckErrorCode(errorCode.ProjectNotFound)) { + err := ProjectScopeReset(cmdData) + if err != nil { + return err + } + } else { + return err + } + } else { + infoText = i18n.ScopedProject } - - infoText = i18n.ScopedProject } if projectId, exists := cmdData.Args[ProjectArgName]; exists { @@ -86,3 +90,15 @@ func (p *project) LoadSelectedScope(ctx context.Context, cmd *cmdBuilder.Cmd, cm return nil } + +func ProjectScopeReset(cmdData *cmdBuilder.LoggedUserCmdData) error { + _, err := cmdData.CliStorage.Update(func(data cliStorage.Data) cliStorage.Data { + data.ScopeProjectId = uuid.ProjectIdNull{} + return data + }) + if err != nil { + return err + } + + return nil +} diff --git a/src/cmd/scopeReset.go b/src/cmd/scopeReset.go index e32291e5..d4799558 100644 --- a/src/cmd/scopeReset.go +++ b/src/cmd/scopeReset.go @@ -3,11 +3,10 @@ package cmd import ( "context" - "github.com/zeropsio/zcli/src/cliStorage" + "github.com/zeropsio/zcli/src/cmd/scope" "github.com/zeropsio/zcli/src/cmdBuilder" "github.com/zeropsio/zcli/src/i18n" "github.com/zeropsio/zcli/src/uxBlock/styles" - "github.com/zeropsio/zerops-go/types/uuid" ) func scopeResetCmd() *cmdBuilder.Cmd { @@ -16,10 +15,7 @@ func scopeResetCmd() *cmdBuilder.Cmd { 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{} - return data - }) + err := scope.ProjectScopeReset(cmdData) if err != nil { return err } diff --git a/src/cmd/servicePush.go b/src/cmd/servicePush.go index da43e14c..06c810a2 100644 --- a/src/cmd/servicePush.go +++ b/src/cmd/servicePush.go @@ -28,7 +28,6 @@ func servicePushCmd() *cmdBuilder.Cmd { StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)). StringFlag("archiveFilePath", "", i18n.T(i18n.BuildArchiveFilePath)). StringFlag("versionName", "", i18n.T(i18n.BuildVersionName)). - StringFlag("source", "", i18n.T(i18n.SourceName)). StringFlag("zeropsYamlPath", "", i18n.T(i18n.ZeropsYamlLocation)). BoolFlag("deployGitFolder", false, i18n.T(i18n.UploadGitFolder)). HelpFlag(i18n.T(i18n.ServicePushHelp)). @@ -144,18 +143,13 @@ func servicePushCmd() *cmdBuilder.Cmd { uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.BuildDeployDeployingStart))) - sourceName := cmdData.Params.GetString("source") - if sourceName == "" { - sourceName = cmdData.Service.Name.String() - } - deployResponse, err := cmdData.RestApiClient.PutAppVersionBuildAndDeploy(ctx, dtoPath.AppVersionId{ Id: appVersion.Id, }, body.PutAppVersionBuildAndDeploy{ ZeropsYaml: types.MediumText(configContent), - Source: types.NewStringNull(sourceName), + Source: types.NewStringNull("CLI"), }, ) if err != nil { diff --git a/src/cmd/version.go b/src/cmd/version.go index 96f44b5e..3b3ade57 100644 --- a/src/cmd/version.go +++ b/src/cmd/version.go @@ -9,7 +9,7 @@ import ( "github.com/zeropsio/zcli/src/i18n" ) -var Version string +var version string func versionCmd() *cmdBuilder.Cmd { return cmdBuilder.NewCmd(). @@ -17,7 +17,7 @@ func versionCmd() *cmdBuilder.Cmd { Short(i18n.T(i18n.CmdVersion)). HelpFlag(i18n.T(i18n.VersionHelp)). 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) + fmt.Printf("zcli version %s (%s) %s/%s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH) return nil }) diff --git a/src/i18n/en.go b/src/i18n/en.go index dc698478..49e9d126 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -62,7 +62,6 @@ var en = map[string]string{ RegionFlag: "Choose one of Zerops regions. Use the \"zcli region list\" command to list all Zerops regions.", RegionUrlFlag: "Zerops region file url.", BuildVersionName: "Adds a custom version name. Automatically filled if the VERSIONNAME environment variable exists.", - SourceName: "Override zerops.yml service name.", BuildWorkingDir: "Sets a custom working directory. Default working directory is the current directory.", BuildArchiveFilePath: "If set, zCLI creates a tar.gz archive with the application code in the required path relative\nto the working directory. By default, no archive is created.", ZeropsYamlLocation: "Sets a custom path to the zerops.yml file relative to the working directory. By default zCLI\nlooks for zerops.yml in the working directory.", diff --git a/src/i18n/i18n.go b/src/i18n/i18n.go index 02a58e47..381a2f03 100644 --- a/src/i18n/i18n.go +++ b/src/i18n/i18n.go @@ -75,7 +75,6 @@ const ( RegionFlag = "RegionFlag" RegionUrlFlag = "RegionUrlFlag" BuildVersionName = "BuildVersionName" - SourceName = "SourceName" BuildWorkingDir = "BuildWorkingDir" BuildArchiveFilePath = "BuildArchiveFilePath" ZeropsYamlLocation = "ZeropsYamlLocation"