Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: err handling + improved import cmd #175

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Contributing Guide

work in progress
work-in-progress
1 change: 1 addition & 0 deletions src/archiveClient/handler_findFilesByRules_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build exclude

package archiveClient

import (
Expand Down
1 change: 1 addition & 0 deletions src/archiveClient/handler_findGitFiles_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build exclude

package archiveClient

import (
Expand Down
33 changes: 21 additions & 12 deletions src/cmd/projectImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"path/filepath"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/entity/repository"
Expand All @@ -14,16 +15,16 @@ import (
"github.com/zeropsio/zerops-go/types/uuid"
)

const projectImportArgName = "importYamlPath"
const defaultYamlFilePattern = "*import.yml"

func projectImportCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("project-import").
Aliases("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)).
StringFlag("workingDir", "./", i18n.T(i18n.BuildWorkingDir)).
HelpFlag(i18n.T(i18n.CmdHelpProjectImport)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks
Expand All @@ -33,11 +34,18 @@ func projectImportCmd() *cmdBuilder.Cmd {
return err
}

yamlContent, err := yamlReader.ReadContent(
uxBlocks,
cmdData.Args[projectImportArgName][0],
cmdData.Params.GetString("workingDir"),
)
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
}
Expand All @@ -50,16 +58,18 @@ func projectImportCmd() *cmdBuilder.Cmd {
},
)
if err != nil {
uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed)))
return err
}

responseOutput, err := importProjectResponse.Output()
projectOutput, err := importProjectResponse.Output()
if err != nil {
uxBlocks.PrintError(styles.ErrorLine(i18n.T(i18n.ProjectImportFailed)))
nermalcat69 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

var processes []uxHelpers.Process
for _, service := range responseOutput.ServiceStacks {
for _, service := range projectOutput.ServiceStacks {
for _, process := range service.Processes {
processes = append(processes, uxHelpers.Process{
F: uxHelpers.CheckZeropsProcess(process.Id, cmdData.RestApiClient),
Expand All @@ -70,7 +80,7 @@ func projectImportCmd() *cmdBuilder.Cmd {
}
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ServiceCount, len(responseOutput.ServiceStacks))))
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)))

Expand All @@ -80,7 +90,6 @@ func projectImportCmd() *cmdBuilder.Cmd {
}

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

return nil
})
}
Expand Down
1 change: 1 addition & 0 deletions src/cmdBuilder/buildCobraCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func buildCobraCmd(
Short: cmd.short,
SilenceUsage: cmd.silenceUsage,
SilenceErrors: cmd.silenceError,
Aliases: cmd.aliases,
}

if cmd.helpTemplate != "" {
Expand Down
6 changes: 6 additions & 0 deletions src/cmdBuilder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ScopeLevel interface {

type Cmd struct {
use string
aliases []string
short string
long string
helpTemplate string
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
CmdHelpProjectServiceImport: "the project service import command.",
Expand Down Expand Up @@ -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 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",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
CmdDescProjectImport = "CmdDescProjectImport"
CmdDescProjectImportLong = "CmdDescProjectImportLong"
ProjectImported = "ProjectImported"
NoYamlFound = "NoYamlFound"
ProjectImportFailed = "ProjectImportFailed"

// project service import
CmdHelpProjectServiceImport = "CmdHelpProjectServiceImport"
Expand Down