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

fix(err handling): projectServiceImport.go #190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/cmd/projectServiceImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"context"

"github.com/pkg/errors"

"github.com/zeropsio/zcli/src/cmd/scope"
"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/i18n"
Expand All @@ -19,28 +21,20 @@ func projectServiceImportCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("service-import").
Short(i18n.T(i18n.CmdDescProjectServiceImport)).
Long(i18n.T(i18n.CmdDescProjectServiceImportLong)).
ScopeLevel(scope.Project).
Arg(serviceImportArgName).
HelpFlag(i18n.T(i18n.CmdHelpProjectServiceImport)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

var err error
var yamlContent []byte
if cmdData.Args[serviceImportArgName][0] == "-" {
yamlContent, err = yamlReader.ReadContentFromStdin(uxBlocks)
if err != nil {
return err
}
} else {
yamlContent, err = yamlReader.ReadContent(
uxBlocks,
cmdData.Args[serviceImportArgName][0],
"./",
)
if err != nil {
return err
}
if len(cmdData.Args[serviceImportArgName]) == 0 {
return errors.New(i18n.T(i18n.ServiceImportYamlPathMissing))
}

yamlContent, err := yamlReader.ReadContent(uxBlocks, cmdData.Args[serviceImportArgName][0], "./")
if err != nil {
return errors.Wrap(err, i18n.T(i18n.ServiceImportYamlReadFailed))
}

importServiceResponse, err := cmdData.RestApiClient.PostServiceStackImport(
Expand All @@ -51,12 +45,12 @@ func projectServiceImportCmd() *cmdBuilder.Cmd {
},
)
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ServiceImportFailed))
}

responseOutput, err := importServiceResponse.Output()
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ServiceImportResponseParseFailed))
}

var processes []uxHelpers.Process
Expand All @@ -76,10 +70,10 @@ func projectServiceImportCmd() *cmdBuilder.Cmd {

err = uxHelpers.ProcessCheckWithSpinner(ctx, cmdData.UxBlocks, processes)
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ServiceImportProcessCheckFailed))
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.ServiceImported)))
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.ServiceImported)))

return nil
})
Expand Down
22 changes: 22 additions & 0 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,26 @@ more info: https://docs.zerops.io/references/cli/`,
ErrorInvalidScopedProjectId: "Invalid ID of the scoped project [%s], select a different project using `zcli scope project` command.",
ErrorInvalidServiceId: "Invalid service ID [%s], %s", // values: serviceId, message
ErrorServiceNotFound: "Service [%s] not found",

// Project service import
CmdDescProjectServiceImportLong: "Creates one or more Zerops services in an existing project according to the definition in the import YAML file.\n\n" +
"The YAML file should contain the service configurations including:\n" +
"- Service type and version\n" +
"- Resource allocations\n" +
"- Environment variables\n" +
"- Dependencies\n\n" +
"Example usage: zcli service-import services.yml",
ServiceImportYamlPathMissing: "YAML file path is missing. Usage: zcli service-import <path_to_yaml_file>",
ServiceImportYamlReadFailed: "Failed to read the YAML file. Please check if the file exists and has correct permissions",
ServiceImportFailed: "Failed to import services. Please check your YAML configuration",
ServiceImportResponseParseFailed: "Failed to parse the import response from server",
ServiceImportProcessCheckFailed: "Failed to check import processes",
ServiceImportYamlValidationFailed: "YAML validation failed. Please check your service configuration",
ServiceImportAvailableCommands: `Available commands after service import:
zcli service list - List all services in the project
zcli service start <service_name> - Start a service
zcli service stop <service_name> - Stop a service
zcli service log <service_name> - View service logs
zcli service deploy <service_name> - Deploy to service
For more information, use 'zcli --help' or visit our documentation.`,
}
10 changes: 10 additions & 0 deletions src/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,14 @@ const (
ErrorInvalidScopedProjectId = "ErrorInvalidScopedProjectId"
ErrorInvalidServiceId = "ErrorInvalidServiceId"
ErrorServiceNotFound = "ErrorServiceNotFound"

// Project service import
CmdDescProjectServiceImportLong = "CmdDescProjectServiceImportLong"
ServiceImportYamlPathMissing = "ServiceImportYamlPathMissing"
ServiceImportYamlReadFailed = "ServiceImportYamlReadFailed"
ServiceImportFailed = "ServiceImportFailed"
ServiceImportResponseParseFailed = "ServiceImportResponseParseFailed"
ServiceImportProcessCheckFailed = "ServiceImportProcessCheckFailed"
ServiceImportAvailableCommands = "ServiceImportAvailableCommands"
ServiceImportYamlValidationFailed = "ServiceImportYamlValidationFailed"
)