From 495117befc9ceea6d085604bcfc69fb5d0f86c43 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 11 Sep 2024 07:19:22 +0530 Subject: [PATCH 01/13] feat: fix err handling + project import cmd --- src/cmd/projectImport.go | 161 ++++++++++++++++++++------------------- src/i18n/en.go | 8 +- src/i18n/i18n.go | 3 + 3 files changed, 89 insertions(+), 83 deletions(-) diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index 7fdcd1bd..4f39e253 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -1,43 +1,44 @@ package cmd import ( - "context" - - "github.com/zeropsio/zcli/src/cmdBuilder" - "github.com/zeropsio/zcli/src/entity/repository" - "github.com/zeropsio/zcli/src/i18n" - "github.com/zeropsio/zcli/src/uxBlock/styles" - "github.com/zeropsio/zcli/src/uxHelpers" - "github.com/zeropsio/zcli/src/yamlReader" - "github.com/zeropsio/zerops-go/dto/input/body" - "github.com/zeropsio/zerops-go/types" - "github.com/zeropsio/zerops-go/types/uuid" + "context" + "github.com/zeropsio/zcli/src/cmdBuilder" + "github.com/zeropsio/zcli/src/entity/repository" + "github.com/zeropsio/zcli/src/i18n" + "github.com/zeropsio/zcli/src/uxBlock/styles" + "github.com/zeropsio/zcli/src/uxHelpers" + "github.com/zeropsio/zcli/src/yamlReader" + "github.com/zeropsio/zerops-go/dto/input/body" + "github.com/zeropsio/zerops-go/types" + "github.com/zeropsio/zerops-go/types/uuid" + "path/filepath" ) -const projectImportArgName = "importYamlPath" +const defaultYamlFileName = "project-import.yml" func projectImportCmd() *cmdBuilder.Cmd { - return cmdBuilder.NewCmd(). - Use("project-import"). - 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.CmdHelpProjectImport)). - LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error { - uxBlocks := cmdData.UxBlocks - - orgId, err := getOrgId(ctx, cmdData) + return cmdBuilder.NewCmd(). + Use("import"). + Short(i18n.T(i18n.CmdDescProjectImport)). + Long(i18n.T(i18n.CmdDescProjectImportLong)). + StringFlag("orgId", "", i18n.T(i18n.OrgIdFlag)). + StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)). + HelpFlag(i18n.T(i18n.CmdHelpProjectImport)). + LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error { + uxBlocks := cmdData.UxBlocks + + orgId, err := getOrgId(ctx, cmdData) + if err != nil { + return err + } + + workingDir := cmdData.Params.GetString("workingDir") + yamlFilePath := filepath.Join(workingDir, defaultYamlFileName) if err != nil { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.NoYamlFound))) return err } - - yamlContent, err := yamlReader.ReadContent( - uxBlocks, - cmdData.Args[projectImportArgName][0], - cmdData.Params.GetString("workingDir"), - ) + yamlContent, err := yamlReader.ReadContent(uxBlocks, yamlFilePath, workingDir) if err != nil { return err } @@ -46,64 +47,64 @@ func projectImportCmd() *cmdBuilder.Cmd { ctx, body.ProjectImport{ ClientId: orgId, - Yaml: types.Text(yamlContent), + Yaml: types.Text(yamlContent), }, ) if err != nil { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) return err } - responseOutput, err := importProjectResponse.Output() + projectOutput, err := importProjectResponse.Output() if err != nil { - return err - } - - var processes []uxHelpers.Process - for _, service := range responseOutput.ServiceStacks { - for _, process := range service.Processes { - processes = append(processes, uxHelpers.Process{ - F: uxHelpers.CheckZeropsProcess(process.Id, cmdData.RestApiClient), - RunningMessage: service.Name.String() + ": " + process.ActionName.String(), - ErrorMessageMessage: service.Name.String() + ": " + process.ActionName.String(), - SuccessMessage: service.Name.String() + ": " + process.ActionName.String(), - }) - } - } - - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ServiceCount, len(responseOutput.ServiceStacks)))) - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.QueuedProcesses, len(processes)))) - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.CoreServices))) - - err = uxHelpers.ProcessCheckWithSpinner(ctx, cmdData.UxBlocks, processes) - if err != nil { - return err - } - - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ProjectImported))) - - return nil - }) + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) + } + + var processes []uxHelpers.Process + for _, service := range projectOutput.ServiceStacks { + for _, process := range service.Processes { + processes = append(processes, uxHelpers.Process{ + F: uxHelpers.CheckZeropsProcess(process.Id, cmdData.RestApiClient), + RunningMessage: service.Name.String() + ": " + process.ActionName.String(), + ErrorMessageMessage: service.Name.String() + ": " + process.ActionName.String(), + SuccessMessage: service.Name.String() + ": " + process.ActionName.String(), + }) + } + } + + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ServiceCount, len(projectOutput.ServiceStacks)))) + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.QueuedProcesses, len(processes)))) + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.CoreServices))) + + err = uxHelpers.ProcessCheckWithSpinner(ctx, cmdData.UxBlocks, processes) + if err != nil { + return err + } + + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ProjectImported))) + return nil + }) } func getOrgId(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) (uuid.ClientId, error) { - orgId := uuid.ClientId(cmdData.Params.GetString("orgId")) - if orgId != "" { - return orgId, nil - } - - orgs, err := repository.GetAllOrgs(ctx, cmdData.RestApiClient) - if err != nil { - return "", err - } - - if len(orgs) == 1 { - return orgs[0].ID, nil - } - - selectedOrg, err := uxHelpers.PrintOrgSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient) - if err != nil { - return "", err - } - - return selectedOrg.ID, nil + orgId := uuid.ClientId(cmdData.Params.GetString("orgId")) + if orgId != "" { + return orgId, nil + } + + orgs, err := repository.GetAllOrgs(ctx, cmdData.RestApiClient) + if err != nil { + return "", err + } + + if len(orgs) == 1 { + return orgs[0].ID, nil + } + + selectedOrg, err := uxHelpers.PrintOrgSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient) + if err != nil { + return "", err + } + + return selectedOrg.ID, nil } diff --git a/src/i18n/en.go b/src/i18n/en.go index 957cde63..66c5b3dd 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -68,9 +68,11 @@ and your %s.`, // project import CmdHelpProjectImport: "the project import command.", - CmdDescProjectImport: "Creates a new project with one or more services.", + CmdDescProjectImport: "Initializes a new project with one or more services with project-import.yaml.", CmdDescProjectImportLong: "Creates a new project with one or more services according to the definition in the import YAML file.", - ProjectImported: "project imported", + ProjectImported: "Project is successfully imported", + NoYamlFound: "No project-import.yml file detected in your directory. For more information, please visit https://docs.zerops.io/references/import/.", + ProjectImportFailed: "Failed while importing, seems like there's an issue try reaching out at https://discord.com/invite/WDvCZ54", // project service import CmdHelpProjectServiceImport: "the project service import command.", @@ -258,7 +260,7 @@ at https://docs.zerops.io/references/cli for further details.`, // import ImportYamlOk: "Yaml file was checked", - ImportYamlEmpty: "Config file import yaml is empty", + ImportYamlEmpty: "The config foo.yml file is empty. For more information, please visit https://docs.zerops.io/references/import/.", ImportYamlTooLarge: "Max. size of import yaml is 100 KB", ImportYamlFound: "Import yaml found", ImportYamlNotFound: "Import yaml not found", diff --git a/src/i18n/i18n.go b/src/i18n/i18n.go index 60e8a88a..147f4b54 100644 --- a/src/i18n/i18n.go +++ b/src/i18n/i18n.go @@ -71,6 +71,9 @@ const ( CmdDescProjectImport = "CmdDescProjectImport" CmdDescProjectImportLong = "CmdDescProjectImportLong" ProjectImported = "ProjectImported" + NoYamlFound = "NoYamlFound" + ProjectImportFailed = "ProjectImportFailed" + // project service import CmdHelpProjectServiceImport = "CmdHelpProjectServiceImport" From 43b2a129106c63b0662840ddb07029e105cae3a0 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 11 Sep 2024 08:04:46 +0530 Subject: [PATCH 02/13] update cmd --- cmd/zcli/main.go | 8 ++++ src/cmd/root.go | 1 + src/cmd/update.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/cmd/update.go diff --git a/cmd/zcli/main.go b/cmd/zcli/main.go index e59f6eaa..cc08e6d9 100644 --- a/cmd/zcli/main.go +++ b/cmd/zcli/main.go @@ -2,8 +2,16 @@ package main import ( "github.com/zeropsio/zcli/src/cmd" + "/src/cmd/update.go" ) func main() { + + go func() { + if err := updateCmd(); err != nil { + fmt.Println("Error checking for updates:", err) + } + }() + cmd.ExecuteCmd() } diff --git a/src/cmd/root.go b/src/cmd/root.go index 0b8bb536..86136150 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -34,6 +34,7 @@ func rootCmd() *cmdBuilder.Cmd { AddChildrenCmd(servicePushCmd()). AddChildrenCmd(envCmd()). AddChildrenCmd(supportCmd()). + AddChildrenCmd(updateCmd()). GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error { cmdData.Stdout.PrintLines( i18n.T(i18n.GuestWelcome), diff --git a/src/cmd/update.go b/src/cmd/update.go new file mode 100644 index 00000000..470f1374 --- /dev/null +++ b/src/cmd/update.go @@ -0,0 +1,113 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + "os/exec" + "strings" +) + +const npmPackageName = "zcli" + +func getCurrentVersion() (string, error) { + cmd := exec.Command("zcli", "version") + output, err := cmd.Output() + if err != nil { + return "", err + } + + scanner := bufio.NewScanner(strings.NewReader(string(output))) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "zcli version") { + parts := strings.Fields(line) + if len(parts) >= 3 { + return parts[2], nil + } + } + } + return "", fmt.Errorf("could not parse zcli version") +} + +func updateCmd() error { + checkForUpdates := func() error { + currentVersion, err := getCurrentVersion() + if err != nil { + return fmt.Errorf("failed to get current version: %w", err) + } + + latestVersion, err := getLatestVersion() + if err != nil { + return err + } + + if strings.Compare(currentVersion, latestVersion) < 0 { + return promptForUpdate(latestVersion) + } + return nil + } + + getLatestVersion := func() (string, error) { + url := "https://registry.npmjs.org/" + npmPackageName + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var data map[string]interface{} + if err := json.Unmarshal(body, &data); err != nil { + return "", err + } + + latestVersion := data["dist-tags"].(map[string]interface{})["latest"].(string) + return latestVersion, nil + } + + promptForUpdate := func(latestVersion string) error { + fmt.Printf("A new version (v%s) is available. Do you want to update now? (y/n): ", latestVersion) + var response string + _, err := fmt.Scanln(&response) + if err != nil { + return err + } + + if strings.ToLower(response) == "y" { + return updateCLI() + } + + fmt.Println("Update skipped.") + return nil + } + + updateCLI := func() error { + fmt.Println("Updating to the latest version...") + cmd := exec.Command("npm", "install", "-g", npmPackageName) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + return err + } + fmt.Println("Update successful.") + return nil + } + + return checkForUpdates() +} + +func main() { + if err := updateCmd(); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } +} From 123efb27d8c325a0aed826b9822286701fdec76a Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 11 Sep 2024 08:07:32 +0530 Subject: [PATCH 03/13] Update update.go --- src/cmd/update.go | 192 ++++++++++++++++++++++------------------------ 1 file changed, 91 insertions(+), 101 deletions(-) diff --git a/src/cmd/update.go b/src/cmd/update.go index 470f1374..20bc4a80 100644 --- a/src/cmd/update.go +++ b/src/cmd/update.go @@ -1,113 +1,103 @@ -package main +package cmd import ( - "bufio" - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "os" - "os/exec" - "strings" + "context" + "fmt" + "os/exec" + "bufio" + "strings" + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/zeropsio/zcli/src/cmdBuilder" + "github.com/zeropsio/zcli/src/i18n" ) const npmPackageName = "zcli" func getCurrentVersion() (string, error) { - cmd := exec.Command("zcli", "version") - output, err := cmd.Output() - if err != nil { - return "", err - } - - scanner := bufio.NewScanner(strings.NewReader(string(output))) - for scanner.Scan() { - line := scanner.Text() - if strings.HasPrefix(line, "zcli version") { - parts := strings.Fields(line) - if len(parts) >= 3 { - return parts[2], nil - } - } - } - return "", fmt.Errorf("could not parse zcli version") + cmd := exec.Command("zcli", "version") + output, err := cmd.Output() + if err != nil { + return "", err + } + + scanner := bufio.NewScanner(strings.NewReader(string(output))) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "zcli version") { + parts := strings.Fields(line) + if len(parts) >= 3 { + return parts[2], nil + } + } + } + return "", fmt.Errorf("could not parse zcli version") } -func updateCmd() error { - checkForUpdates := func() error { - currentVersion, err := getCurrentVersion() - if err != nil { - return fmt.Errorf("failed to get current version: %w", err) - } - - latestVersion, err := getLatestVersion() - if err != nil { - return err - } - - if strings.Compare(currentVersion, latestVersion) < 0 { - return promptForUpdate(latestVersion) - } - return nil - } - - getLatestVersion := func() (string, error) { - url := "https://registry.npmjs.org/" + npmPackageName - resp, err := http.Get(url) - if err != nil { - return "", err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - - var data map[string]interface{} - if err := json.Unmarshal(body, &data); err != nil { - return "", err - } - - latestVersion := data["dist-tags"].(map[string]interface{})["latest"].(string) - return latestVersion, nil - } - - promptForUpdate := func(latestVersion string) error { - fmt.Printf("A new version (v%s) is available. Do you want to update now? (y/n): ", latestVersion) - var response string - _, err := fmt.Scanln(&response) - if err != nil { - return err - } - - if strings.ToLower(response) == "y" { - return updateCLI() - } - - fmt.Println("Update skipped.") - return nil - } - - updateCLI := func() error { - fmt.Println("Updating to the latest version...") - cmd := exec.Command("npm", "install", "-g", npmPackageName) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - if err != nil { - return err - } - fmt.Println("Update successful.") - return nil - } - - return checkForUpdates() +func getLatestVersion() (string, error) { + url := "https://registry.npmjs.org/" + npmPackageName + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var data map[string]interface{} + if err := json.Unmarshal(body, &data); err != nil { + return "", err + } + + latestVersion := data["dist-tags"].(map[string]interface{})["latest"].(string) + return latestVersion, nil } -func main() { - if err := updateCmd(); err != nil { - fmt.Println("Error:", err) - os.Exit(1) - } +func updateCmd() *cmdBuilder.Cmd { + return cmdBuilder.NewCmd(). + Use("update"). + Short(i18n.T(i18n.CmdDescUpdate)). + HelpFlag(i18n.T(i18n.CmdHelpUpdate)). + GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error { + currentVersion, err := getCurrentVersion() + if err != nil { + return fmt.Errorf("failed to get current version: %w", err) + } + + latestVersion, err := getLatestVersion() + if err != nil { + return err + } + + if strings.Compare(currentVersion, latestVersion) < 0 { + cmdData.Stdout.Printf("A new version (v%s) is available. Do you want to update now? (y/n): ", latestVersion) + var response string + _, err := fmt.Scanln(&response) + if err != nil { + return err + } + + if strings.ToLower(response) == "y" { + cmd := exec.Command("npm", "install", "-g", npmPackageName) + cmd.Stdout = cmdData.Stdout + cmd.Stderr = cmdData.Stderr + err := cmd.Run() + if err != nil { + return err + } + cmdData.Stdout.Println("Update successful. Please restart the CLI.") + } else { + cmdData.Stdout.Println("Update skipped.") + } + } else { + cmdData.Stdout.Println("You are already using the latest version.") + } + + return nil + }) } From 86670676a261699bae12c6797f13cfa8eaff4596 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 11 Sep 2024 08:21:09 +0530 Subject: [PATCH 04/13] remove update cmd --- .gitignore | 1 + cmd/zcli/main.go | 8 ---- src/cmd/root.go | 1 - src/cmd/update.go | 103 ---------------------------------------------- 4 files changed, 1 insertion(+), 112 deletions(-) delete mode 100644 src/cmd/update.go diff --git a/.gitignore b/.gitignore index 37d51964..f897caea 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ tools/npm/node_modules *.DS_Store # ignore built binary in root dir /zcli +vendor diff --git a/cmd/zcli/main.go b/cmd/zcli/main.go index cc08e6d9..e59f6eaa 100644 --- a/cmd/zcli/main.go +++ b/cmd/zcli/main.go @@ -2,16 +2,8 @@ package main import ( "github.com/zeropsio/zcli/src/cmd" - "/src/cmd/update.go" ) func main() { - - go func() { - if err := updateCmd(); err != nil { - fmt.Println("Error checking for updates:", err) - } - }() - cmd.ExecuteCmd() } diff --git a/src/cmd/root.go b/src/cmd/root.go index 86136150..0b8bb536 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -34,7 +34,6 @@ func rootCmd() *cmdBuilder.Cmd { AddChildrenCmd(servicePushCmd()). AddChildrenCmd(envCmd()). AddChildrenCmd(supportCmd()). - AddChildrenCmd(updateCmd()). GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error { cmdData.Stdout.PrintLines( i18n.T(i18n.GuestWelcome), diff --git a/src/cmd/update.go b/src/cmd/update.go deleted file mode 100644 index 20bc4a80..00000000 --- a/src/cmd/update.go +++ /dev/null @@ -1,103 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "os/exec" - "bufio" - "strings" - "encoding/json" - "io/ioutil" - "net/http" - - "github.com/zeropsio/zcli/src/cmdBuilder" - "github.com/zeropsio/zcli/src/i18n" -) - -const npmPackageName = "zcli" - -func getCurrentVersion() (string, error) { - cmd := exec.Command("zcli", "version") - output, err := cmd.Output() - if err != nil { - return "", err - } - - scanner := bufio.NewScanner(strings.NewReader(string(output))) - for scanner.Scan() { - line := scanner.Text() - if strings.HasPrefix(line, "zcli version") { - parts := strings.Fields(line) - if len(parts) >= 3 { - return parts[2], nil - } - } - } - return "", fmt.Errorf("could not parse zcli version") -} - -func getLatestVersion() (string, error) { - url := "https://registry.npmjs.org/" + npmPackageName - resp, err := http.Get(url) - if err != nil { - return "", err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - - var data map[string]interface{} - if err := json.Unmarshal(body, &data); err != nil { - return "", err - } - - latestVersion := data["dist-tags"].(map[string]interface{})["latest"].(string) - return latestVersion, nil -} - -func updateCmd() *cmdBuilder.Cmd { - return cmdBuilder.NewCmd(). - Use("update"). - Short(i18n.T(i18n.CmdDescUpdate)). - HelpFlag(i18n.T(i18n.CmdHelpUpdate)). - GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error { - currentVersion, err := getCurrentVersion() - if err != nil { - return fmt.Errorf("failed to get current version: %w", err) - } - - latestVersion, err := getLatestVersion() - if err != nil { - return err - } - - if strings.Compare(currentVersion, latestVersion) < 0 { - cmdData.Stdout.Printf("A new version (v%s) is available. Do you want to update now? (y/n): ", latestVersion) - var response string - _, err := fmt.Scanln(&response) - if err != nil { - return err - } - - if strings.ToLower(response) == "y" { - cmd := exec.Command("npm", "install", "-g", npmPackageName) - cmd.Stdout = cmdData.Stdout - cmd.Stderr = cmdData.Stderr - err := cmd.Run() - if err != nil { - return err - } - cmdData.Stdout.Println("Update successful. Please restart the CLI.") - } else { - cmdData.Stdout.Println("Update skipped.") - } - } else { - cmdData.Stdout.Println("You are already using the latest version.") - } - - return nil - }) -} From d0d75a1a160d89dbb38af67354b5a632d29f9f8f Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Mon, 16 Sep 2024 06:35:57 +0530 Subject: [PATCH 05/13] chore: iterate zcli project import --- CONTRIBUTING.md | 7 ++++- src/cmd/projectImport.go | 61 ++++++++++++++++++++++------------------ src/i18n/en.go | 2 +- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1f16e573..289b5aa6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,8 @@ ## Contributing Guide -work in progress \ No newline at end of file +``` +go build cmd/zcli/main.go +``` +``` +go run cmd/zcli/main.go +``` diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index 4f39e253..4633a3a0 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -14,7 +14,7 @@ import ( "path/filepath" ) -const defaultYamlFileName = "project-import.yml" +const defaultYamlFilePattern = "*import.yml" func projectImportCmd() *cmdBuilder.Cmd { return cmdBuilder.NewCmd(). @@ -32,33 +32,38 @@ func projectImportCmd() *cmdBuilder.Cmd { return err } - workingDir := cmdData.Params.GetString("workingDir") - yamlFilePath := filepath.Join(workingDir, defaultYamlFileName) - if err != nil { - uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.NoYamlFound))) - return err - } - yamlContent, err := yamlReader.ReadContent(uxBlocks, yamlFilePath, workingDir) - if err != nil { - return err - } - - importProjectResponse, err := cmdData.RestApiClient.PostProjectImport( - ctx, - body.ProjectImport{ - ClientId: orgId, - Yaml: types.Text(yamlContent), - }, - ) - if err != nil { - uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) - return err - } - - projectOutput, err := importProjectResponse.Output() - if err != nil { - uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) - } + workingDir := cmdData.Params.GetString("workingDir") + + yamlFiles, err := filepath.Glob(filepath.Join(workingDir, defaultYamlFilePattern)) + if err != nil || len(yamlFiles) == 0 { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.NoYamlFound))) + return err + } + + yamlFilePath := yamlFiles[0] + uxBlocks.PrintInfo(styles.InfoLine("Using YAML file: " + yamlFilePath)) + + yamlContent, err := yamlReader.ReadContent(uxBlocks, yamlFilePath, workingDir) + if err != nil { + return err + } + + importProjectResponse, err := cmdData.RestApiClient.PostProjectImport( + ctx, + body.ProjectImport{ + ClientId: orgId, + Yaml: types.Text(yamlContent), + }, + ) + if err != nil { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) + return err + } + + projectOutput, err := importProjectResponse.Output() + if err != nil { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) + } var processes []uxHelpers.Process for _, service := range projectOutput.ServiceStacks { diff --git a/src/i18n/en.go b/src/i18n/en.go index 66c5b3dd..d713297f 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -71,7 +71,7 @@ and your %s.`, CmdDescProjectImport: "Initializes a new project with one or more services with project-import.yaml.", CmdDescProjectImportLong: "Creates a new project with one or more services according to the definition in the import YAML file.", ProjectImported: "Project is successfully imported", - NoYamlFound: "No project-import.yml file detected in your directory. For more information, please visit https://docs.zerops.io/references/import/.", + NoYamlFound: "No import yaml file detected in your directory. For more information, please visit https://docs.zerops.io/references/import/.", ProjectImportFailed: "Failed while importing, seems like there's an issue try reaching out at https://discord.com/invite/WDvCZ54", // project service import From 729cf9053c9ad3063ac6187dcf9fcbe8a8080292 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Mon, 16 Sep 2024 06:39:38 +0530 Subject: [PATCH 06/13] Update projectImport.go --- src/cmd/projectImport.go | 206 +++++++++++++++++++-------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index 4633a3a0..c84ff122 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -1,115 +1,115 @@ package cmd import ( - "context" - "github.com/zeropsio/zcli/src/cmdBuilder" - "github.com/zeropsio/zcli/src/entity/repository" - "github.com/zeropsio/zcli/src/i18n" - "github.com/zeropsio/zcli/src/uxBlock/styles" - "github.com/zeropsio/zcli/src/uxHelpers" - "github.com/zeropsio/zcli/src/yamlReader" - "github.com/zeropsio/zerops-go/dto/input/body" - "github.com/zeropsio/zerops-go/types" - "github.com/zeropsio/zerops-go/types/uuid" - "path/filepath" + "context" + "github.com/zeropsio/zcli/src/cmdBuilder" + "github.com/zeropsio/zcli/src/entity/repository" + "github.com/zeropsio/zcli/src/i18n" + "github.com/zeropsio/zcli/src/uxBlock/styles" + "github.com/zeropsio/zcli/src/uxHelpers" + "github.com/zeropsio/zcli/src/yamlReader" + "github.com/zeropsio/zerops-go/dto/input/body" + "github.com/zeropsio/zerops-go/types" + "github.com/zeropsio/zerops-go/types/uuid" + "path/filepath" ) const defaultYamlFilePattern = "*import.yml" func projectImportCmd() *cmdBuilder.Cmd { - return cmdBuilder.NewCmd(). - Use("import"). - Short(i18n.T(i18n.CmdDescProjectImport)). - Long(i18n.T(i18n.CmdDescProjectImportLong)). - StringFlag("orgId", "", i18n.T(i18n.OrgIdFlag)). - StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)). - HelpFlag(i18n.T(i18n.CmdHelpProjectImport)). - LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error { - uxBlocks := cmdData.UxBlocks - - orgId, err := getOrgId(ctx, cmdData) - if err != nil { - return err - } - - workingDir := cmdData.Params.GetString("workingDir") - - yamlFiles, err := filepath.Glob(filepath.Join(workingDir, defaultYamlFilePattern)) - if err != nil || len(yamlFiles) == 0 { - uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.NoYamlFound))) - return err - } - - yamlFilePath := yamlFiles[0] - uxBlocks.PrintInfo(styles.InfoLine("Using YAML file: " + yamlFilePath)) - - yamlContent, err := yamlReader.ReadContent(uxBlocks, yamlFilePath, workingDir) - if err != nil { - return err - } - - importProjectResponse, err := cmdData.RestApiClient.PostProjectImport( - ctx, - body.ProjectImport{ - ClientId: orgId, - Yaml: types.Text(yamlContent), - }, - ) - if err != nil { - uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) - return err - } - - projectOutput, err := importProjectResponse.Output() - if err != nil { - uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) - } - - var processes []uxHelpers.Process - for _, service := range projectOutput.ServiceStacks { - for _, process := range service.Processes { - processes = append(processes, uxHelpers.Process{ - F: uxHelpers.CheckZeropsProcess(process.Id, cmdData.RestApiClient), - RunningMessage: service.Name.String() + ": " + process.ActionName.String(), - ErrorMessageMessage: service.Name.String() + ": " + process.ActionName.String(), - SuccessMessage: service.Name.String() + ": " + process.ActionName.String(), - }) - } - } - - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ServiceCount, len(projectOutput.ServiceStacks)))) - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.QueuedProcesses, len(processes)))) - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.CoreServices))) - - err = uxHelpers.ProcessCheckWithSpinner(ctx, cmdData.UxBlocks, processes) - if err != nil { - return err - } - - uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ProjectImported))) - return nil - }) + return cmdBuilder.NewCmd(). + Use("import"). + Short(i18n.T(i18n.CmdDescProjectImport)). + Long(i18n.T(i18n.CmdDescProjectImportLong)). + StringFlag("orgId", "", i18n.T(i18n.OrgIdFlag)). + StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)). + HelpFlag(i18n.T(i18n.CmdHelpProjectImport)). + LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error { + uxBlocks := cmdData.UxBlocks + + orgId, err := getOrgId(ctx, cmdData) + if err != nil { + return err + } + + workingDir := cmdData.Params.GetString("workingDir") + + yamlFiles, err := filepath.Glob(filepath.Join(workingDir, defaultYamlFilePattern)) + if err != nil || len(yamlFiles) == 0 { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.NoYamlFound))) + return err + } + + yamlFilePath := yamlFiles[0] + uxBlocks.PrintInfo(styles.InfoLine("Using YAML file: " + yamlFilePath)) + + yamlContent, err := yamlReader.ReadContent(uxBlocks, yamlFilePath, workingDir) + if err != nil { + return err + } + + importProjectResponse, err := cmdData.RestApiClient.PostProjectImport( + ctx, + body.ProjectImport{ + ClientId: orgId, + Yaml: types.Text(yamlContent), + }, + ) + if err != nil { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) + return err + } + + projectOutput, err := importProjectResponse.Output() + if err != nil { + uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) + } + + var processes []uxHelpers.Process + for _, service := range projectOutput.ServiceStacks { + for _, process := range service.Processes { + processes = append(processes, uxHelpers.Process{ + F: uxHelpers.CheckZeropsProcess(process.Id, cmdData.RestApiClient), + RunningMessage: service.Name.String() + ": " + process.ActionName.String(), + ErrorMessageMessage: service.Name.String() + ": " + process.ActionName.String(), + SuccessMessage: service.Name.String() + ": " + process.ActionName.String(), + }) + } + } + + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ServiceCount, len(projectOutput.ServiceStacks)))) + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.QueuedProcesses, len(processes)))) + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.CoreServices))) + + err = uxHelpers.ProcessCheckWithSpinner(ctx, cmdData.UxBlocks, processes) + if err != nil { + return err + } + + uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ProjectImported))) + return nil + }) } func getOrgId(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) (uuid.ClientId, error) { - orgId := uuid.ClientId(cmdData.Params.GetString("orgId")) - if orgId != "" { - return orgId, nil - } - - orgs, err := repository.GetAllOrgs(ctx, cmdData.RestApiClient) - if err != nil { - return "", err - } - - if len(orgs) == 1 { - return orgs[0].ID, nil - } - - selectedOrg, err := uxHelpers.PrintOrgSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient) - if err != nil { - return "", err - } - - return selectedOrg.ID, nil + orgId := uuid.ClientId(cmdData.Params.GetString("orgId")) + if orgId != "" { + return orgId, nil + } + + orgs, err := repository.GetAllOrgs(ctx, cmdData.RestApiClient) + if err != nil { + return "", err + } + + if len(orgs) == 1 { + return orgs[0].ID, nil + } + + selectedOrg, err := uxHelpers.PrintOrgSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient) + if err != nil { + return "", err + } + + return selectedOrg.ID, nil } From e8c5623c9597986aec8148b08608072d90d67dfc Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Mon, 16 Sep 2024 06:41:00 +0530 Subject: [PATCH 07/13] gofmt --- CONTRIBUTING.md | 4 ++++ src/archiveClient/handler_findFilesByRules_test.go | 1 + src/archiveClient/handler_findGitFiles_test.go | 1 + src/i18n/en.go | 2 +- src/i18n/i18n.go | 5 ++--- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 289b5aa6..4636f864 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,3 +6,7 @@ go build cmd/zcli/main.go ``` go run cmd/zcli/main.go ``` + +``` +gofmt -s -w ./ +``` diff --git a/src/archiveClient/handler_findFilesByRules_test.go b/src/archiveClient/handler_findFilesByRules_test.go index f66c29e9..b4e35915 100644 --- a/src/archiveClient/handler_findFilesByRules_test.go +++ b/src/archiveClient/handler_findFilesByRules_test.go @@ -1,4 +1,5 @@ //go:build exclude + package archiveClient import ( diff --git a/src/archiveClient/handler_findGitFiles_test.go b/src/archiveClient/handler_findGitFiles_test.go index eba2eca0..0ed05974 100644 --- a/src/archiveClient/handler_findGitFiles_test.go +++ b/src/archiveClient/handler_findGitFiles_test.go @@ -1,4 +1,5 @@ //go:build exclude + package archiveClient import ( diff --git a/src/i18n/en.go b/src/i18n/en.go index d713297f..debf73eb 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -72,7 +72,7 @@ and your %s.`, CmdDescProjectImportLong: "Creates a new project with one or more services according to the definition in the import YAML file.", ProjectImported: "Project is successfully imported", NoYamlFound: "No import yaml file detected in your directory. For more information, please visit https://docs.zerops.io/references/import/.", - ProjectImportFailed: "Failed while importing, seems like there's an issue try reaching out at https://discord.com/invite/WDvCZ54", + ProjectImportFailed: "Failed while importing, seems like there's an issue try reaching out at https://discord.com/invite/WDvCZ54", // project service import CmdHelpProjectServiceImport: "the project service import command.", diff --git a/src/i18n/i18n.go b/src/i18n/i18n.go index 147f4b54..4c366fd7 100644 --- a/src/i18n/i18n.go +++ b/src/i18n/i18n.go @@ -71,9 +71,8 @@ const ( CmdDescProjectImport = "CmdDescProjectImport" CmdDescProjectImportLong = "CmdDescProjectImportLong" ProjectImported = "ProjectImported" - NoYamlFound = "NoYamlFound" - ProjectImportFailed = "ProjectImportFailed" - + NoYamlFound = "NoYamlFound" + ProjectImportFailed = "ProjectImportFailed" // project service import CmdHelpProjectServiceImport = "CmdHelpProjectServiceImport" From 40ef169cfb164331afc606c42ac317a713e825a2 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Mon, 16 Sep 2024 06:45:13 +0530 Subject: [PATCH 08/13] goimports -w ./ --- CONTRIBUTING.md | 4 ++++ src/cmd/projectImport.go | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4636f864..2dfa92fa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,3 +10,7 @@ go run cmd/zcli/main.go ``` gofmt -s -w ./ ``` + +``` +goimports -w ./ +``` diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index c84ff122..fd8d121e 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -2,6 +2,8 @@ package cmd import ( "context" + "path/filepath" + "github.com/zeropsio/zcli/src/cmdBuilder" "github.com/zeropsio/zcli/src/entity/repository" "github.com/zeropsio/zcli/src/i18n" @@ -11,7 +13,6 @@ import ( "github.com/zeropsio/zerops-go/dto/input/body" "github.com/zeropsio/zerops-go/types" "github.com/zeropsio/zerops-go/types/uuid" - "path/filepath" ) const defaultYamlFilePattern = "*import.yml" From 72ac24276864c61fb86891e738246f287010c84f Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 25 Sep 2024 17:14:54 +0530 Subject: [PATCH 09/13] chore: remove vendor from gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index f897caea..37d51964 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ tools/npm/node_modules *.DS_Store # ignore built binary in root dir /zcli -vendor From 390d1d0a9118497ffd53a60117b19da5e42cd337 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Thu, 26 Sep 2024 17:25:43 +0530 Subject: [PATCH 10/13] feat: alias for import-project --- src/cmd/projectImport.go | 3 ++- src/cmdBuilder/buildCobraCmd.go | 1 + src/cmdBuilder/cmd.go | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index fd8d121e..386a3b42 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -19,7 +19,8 @@ const defaultYamlFilePattern = "*import.yml" func projectImportCmd() *cmdBuilder.Cmd { return cmdBuilder.NewCmd(). - Use("import"). + Use("import-project"). + Aliases("import"). Short(i18n.T(i18n.CmdDescProjectImport)). Long(i18n.T(i18n.CmdDescProjectImportLong)). StringFlag("orgId", "", i18n.T(i18n.OrgIdFlag)). diff --git a/src/cmdBuilder/buildCobraCmd.go b/src/cmdBuilder/buildCobraCmd.go index 99be8208..4dedee22 100644 --- a/src/cmdBuilder/buildCobraCmd.go +++ b/src/cmdBuilder/buildCobraCmd.go @@ -20,6 +20,7 @@ func buildCobraCmd( Short: cmd.short, SilenceUsage: cmd.silenceUsage, SilenceErrors: cmd.silenceError, + Aliases: cmd.aliases, } if cmd.helpTemplate != "" { diff --git a/src/cmdBuilder/cmd.go b/src/cmdBuilder/cmd.go index 4d4a7f60..008a0fa0 100644 --- a/src/cmdBuilder/cmd.go +++ b/src/cmdBuilder/cmd.go @@ -14,6 +14,7 @@ type ScopeLevel interface { type Cmd struct { use string + aliases []string short string long string helpTemplate string @@ -60,6 +61,11 @@ func (cmd *Cmd) Use(use string) *Cmd { return cmd } +func (cmd *Cmd) Aliases(aliases ...string) *Cmd { + cmd.aliases = aliases + return cmd +} + func (cmd *Cmd) SetHelpTemplate(template string) *Cmd { cmd.helpTemplate = template return cmd From d142a51b98f45abca399fe887a164833213a2b5c Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Thu, 26 Sep 2024 17:26:38 +0530 Subject: [PATCH 11/13] Update CONTRIBUTING.md --- CONTRIBUTING.md | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2dfa92fa..958bf844 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,16 +1,3 @@ ## Contributing Guide -``` -go build cmd/zcli/main.go -``` -``` -go run cmd/zcli/main.go -``` - -``` -gofmt -s -w ./ -``` - -``` -goimports -w ./ -``` +work-in-progress From 4252a56f8c2072ef68b06d895cb6638b3fea473c Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 9 Oct 2024 22:11:13 +0530 Subject: [PATCH 12/13] chore: revert command name --- src/cmd/projectImport.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index 386a3b42..b26d1a9d 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -19,7 +19,7 @@ const defaultYamlFilePattern = "*import.yml" func projectImportCmd() *cmdBuilder.Cmd { return cmdBuilder.NewCmd(). - Use("import-project"). + Use("project-import"). Aliases("import"). Short(i18n.T(i18n.CmdDescProjectImport)). Long(i18n.T(i18n.CmdDescProjectImportLong)). From 17b91c38a3a37c989c168ea5a10bfa6a3b64edc6 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Wed, 9 Oct 2024 22:13:30 +0530 Subject: [PATCH 13/13] chore: fix return err + err handling statement --- src/cmd/projectImport.go | 1 + src/i18n/en.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cmd/projectImport.go b/src/cmd/projectImport.go index b26d1a9d..caadde8b 100644 --- a/src/cmd/projectImport.go +++ b/src/cmd/projectImport.go @@ -65,6 +65,7 @@ func projectImportCmd() *cmdBuilder.Cmd { projectOutput, err := importProjectResponse.Output() if err != nil { uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed))) + return err } var processes []uxHelpers.Process diff --git a/src/i18n/en.go b/src/i18n/en.go index debf73eb..a2cfb9aa 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -260,7 +260,7 @@ at https://docs.zerops.io/references/cli for further details.`, // import ImportYamlOk: "Yaml file was checked", - ImportYamlEmpty: "The config foo.yml file is empty. For more information, please visit https://docs.zerops.io/references/import/.", + ImportYamlEmpty: "The config import yaml file is empty. For more information, please visit https://docs.zerops.io/references/import/.", ImportYamlTooLarge: "Max. size of import yaml is 100 KB", ImportYamlFound: "Import yaml found", ImportYamlNotFound: "Import yaml not found",