From 165bf1f37fd7f7939a20b62534c26c3b709f548b Mon Sep 17 00:00:00 2001 From: Aavash Shrestha Date: Fri, 15 Sep 2023 17:43:17 +0200 Subject: [PATCH] cmd: update error messages --- cmd/builder.go | 13 ++++---- cmd/dev.go | 14 +++----- cmd/dev_proxy.go | 6 ++-- cmd/dev_trigger.go | 14 +++----- cmd/dev_up.go | 11 ++----- cmd/exec.go | 8 ++--- cmd/link.go | 9 ++---- cmd/login.go | 15 +++------ cmd/new.go | 12 +++---- cmd/open.go | 7 ++-- cmd/push.go | 64 +++++++++++-------------------------- cmd/release.go | 74 +++++++++++++++---------------------------- cmd/root.go | 18 +++++------ cmd/trigger.go | 5 --- cmd/validate.go | 18 ++++------- cmd/version_ugrade.go | 15 ++++----- 16 files changed, 103 insertions(+), 200 deletions(-) diff --git a/cmd/builder.go b/cmd/builder.go index e53293c..f865067 100644 --- a/cmd/builder.go +++ b/cmd/builder.go @@ -38,8 +38,7 @@ func newCmdEnv() *cobra.Command { var err error projectID, err = runtime.GetProjectID(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to get project id: %w", err) } } @@ -89,7 +88,7 @@ func cmdEnvGetFn(microName string, file string, projectID string) error { err = godotenv.Write(envMap, file) if err != nil { - return fmt.Errorf("Failed to write to `%s` env file: %s", file, err) + return fmt.Errorf("failed to write to `%s` env file: %w", file, err) } utils.Logger.Printf("%s Wrote %d environment variables from the micro `%s` to the file `%s`", @@ -111,12 +110,12 @@ func cmdEnvSetFn(microName string, file string, projectID string) error { data, err := os.ReadFile(file) if err != nil { - return fmt.Errorf("Failed to read `%s` env file: %s", file, err) + return fmt.Errorf("failed to read `%s` env file: %w", file, err) } envMap, err := godotenv.UnmarshalBytes(data) if err != nil { - return fmt.Errorf("Failed to parse `%s` env file: %s", file, err) + return fmt.Errorf("failed to parse `%s` env file: %w", file, err) } // update the values in-place @@ -165,8 +164,8 @@ func cmdEnvGetMicro(microName string, devInstance *api.AppInstance) (*api.AppIns } if microName == "" { - return nil, fmt.Errorf("Please provide a valid micro name with the `--micro` flag") + return nil, fmt.Errorf("please provide a valid micro name with the `--micro` flag") } else { - return nil, fmt.Errorf("Micro '%s' not found in this project", microName) + return nil, fmt.Errorf("micro '%s' not found in this project", microName) } } diff --git a/cmd/dev.go b/cmd/dev.go index 4760eed..2a9933b 100644 --- a/cmd/dev.go +++ b/cmd/dev.go @@ -67,8 +67,7 @@ The cli will start one process for each of your micros, then expose a single enp if !cmd.Flags().Changed("id") { projectID, err = runtime.GetProjectID(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to get proejct id: %w", err) } } @@ -126,17 +125,14 @@ func dev(projectDir string, projectID string, host string, port int, open bool) routeDir := filepath.Join(projectDir, ".space", "micros") spacefile, err := spacefile.LoadSpacefile(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to parse Spacefile: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to parse Spacefile: %w", err) } projectKey, err := utils.GenerateDataKeyIfNotExists(projectID) - - addr := fmt.Sprintf("%s:%d", host, port) if err != nil { - utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation) - return err + return fmt.Errorf("failed to generate project key: %w", err) } + addr := fmt.Sprintf("%s:%d", host, port) utils.Logger.Printf("\n%s Checking for running micros...", emoji.Eyes) var stoppedMicros []*types.Micro @@ -220,7 +216,7 @@ func dev(projectDir string, projectID string, host string, port int, open bool) defer wg.Done() err := server.ListenAndServe() if err != nil && err != http.ErrServerClosed { - utils.Logger.Println("proxy error", err) + utils.StdErrLogger.Println("proxy error", err) } }() diff --git a/cmd/dev_proxy.go b/cmd/dev_proxy.go index f40b09a..41f1bf7 100644 --- a/cmd/dev_proxy.go +++ b/cmd/dev_proxy.go @@ -40,8 +40,7 @@ The micros will be automatically discovered and proxied to.`, if !cmd.Flags().Changed("port") { port, err = GetFreePort(utils.DevPort) if err != nil { - utils.Logger.Printf("%s Failed to get free port: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to get free port: %w", err) } } @@ -80,8 +79,7 @@ func devProxy(projectDir string, host string, port int, open bool) error { projectKey, err := utils.GenerateDataKeyIfNotExists(meta.ID) if err != nil { - utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation) - return err + return fmt.Errorf("failed to generate project key: %w", err) } reverseProxy := proxy.NewReverseProxy(projectKey, meta.ID, meta.Name, meta.Alias) diff --git a/cmd/dev_trigger.go b/cmd/dev_trigger.go index 05a8e44..26ba7b7 100644 --- a/cmd/dev_trigger.go +++ b/cmd/dev_trigger.go @@ -143,7 +143,7 @@ Make sure that the corresponding micro is running before triggering the action.` func triggerScheduledAction(projectDir string, actionID string) (err error) { spacefile, err := spacefile.LoadSpacefile(projectDir) if err != nil { - utils.Logger.Printf("%s failed to parse Spacefile: %s", emoji.X, err.Error()) + return fmt.Errorf("failed to parse Spacefile: %w", err) } routeDir := filepath.Join(projectDir, ".space", "micros") @@ -157,7 +157,7 @@ func triggerScheduledAction(projectDir string, actionID string) (err error) { port, err := getMicroPort(micro, routeDir) if err != nil { upCommand := fmt.Sprintf("space dev up %s", micro.Name) - utils.Logger.Printf("%smicro %s is not running, to start it run:", emoji.X, styles.Green(micro.Name)) + utils.Logger.Printf("%s Micro %s is not running, to start it run:", emoji.X, styles.Green(micro.Name)) utils.Logger.Printf("L %s", styles.Blue(upCommand)) return err } @@ -180,8 +180,7 @@ func triggerScheduledAction(projectDir string, actionID string) (err error) { res, err := http.Post(actionEndpoint, "application/json", bytes.NewReader(body)) if err != nil { - utils.Logger.Printf("\n%s failed to trigger action: %s", emoji.X, err.Error()) - return err + return fmt.Errorf("failed to trigger action: %w", err) } defer res.Body.Close() @@ -193,14 +192,11 @@ func triggerScheduledAction(projectDir string, actionID string) (err error) { io.Copy(os.Stdout, res.Body) if res.StatusCode >= 400 { - utils.Logger.Printf("\n\nL %s", styles.Error("failed to trigger action")) - return err + return fmt.Errorf("\n\nL failed to trigger action") } utils.Logger.Printf("\n\nL Action triggered successfully!") return nil } } - - utils.Logger.Printf("\n%saction `%s` not found", emoji.X, actionID) - return fmt.Errorf("\n%saction `%s` not found", emoji.X, actionID) + return fmt.Errorf("\n%s action `%s` not found", emoji.X, actionID) } diff --git a/cmd/dev_up.go b/cmd/dev_up.go index 6e49967..eb826af 100644 --- a/cmd/dev_up.go +++ b/cmd/dev_up.go @@ -37,14 +37,12 @@ func newCmdDevUp() *cobra.Command { if err != nil { return err } - } if !cmd.Flags().Changed("port") { port, err = GetFreePort(utils.DevPort + 1) if err != nil { - utils.Logger.Printf("%s Failed to get free port: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to get free port: %w", err) } } @@ -68,13 +66,12 @@ func devUp(projectDir string, projectId string, port int, microName string, open spacefile, err := spacefile.LoadSpacefile(projectDir) if err != nil { - return err + return fmt.Errorf("failed to parse Spacefile: %w", err) } projectKey, err := utils.GenerateDataKeyIfNotExists(projectId) if err != nil { - utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation) - return err + return fmt.Errorf("failed to generate project key: %w", err) } for _, micro := range spacefile.Micros { @@ -128,7 +125,5 @@ func devUp(projectDir string, projectId string, port int, microName string, open command.Wait() return nil } - - utils.Logger.Printf("micro %s not found", microName) return fmt.Errorf("micro %s not found", microName) } diff --git a/cmd/exec.go b/cmd/exec.go index b2c5704..b3f7a2b 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -1,12 +1,12 @@ package cmd import ( + "fmt" "os" "os/exec" "github.com/deta/space/cmd/utils" "github.com/deta/space/internal/runtime" - "github.com/deta/space/pkg/components/emoji" "github.com/spf13/cobra" ) @@ -26,8 +26,7 @@ The data key will be automatically injected into the command's environment.`, cwd, _ := os.Getwd() projectID, err = runtime.GetProjectID(cwd) if err != nil { - utils.Logger.Printf("project id not provided and could not be inferred from current working directory") - return err + return fmt.Errorf("project id not provided and could not be inferred from current working directory") } } @@ -49,8 +48,7 @@ func execRun(projectID string, args []string) error { projectKey, err := utils.GenerateDataKeyIfNotExists(projectID) if err != nil { - utils.Logger.Printf("%sError generating data key: %s\n", emoji.ErrorExclamation, err.Error()) - return err + return fmt.Errorf("failed to generate data key: %w", err) } name := args[0] diff --git a/cmd/link.go b/cmd/link.go index 2a7b736..261280b 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -73,18 +73,15 @@ func link(projectDir string, projectID string) error { return err } if errors.Is(err, api.ErrProjectNotFound) { - utils.Logger.Println(styles.Errorf("%s No project found. Please provide a valid Project ID.", emoji.ErrorExclamation)) - return err + return fmt.Errorf("no project found, please provide a valid project id") } - utils.Logger.Println(styles.Errorf("%s Failed to link project, %s", emoji.ErrorExclamation, err.Error())) - return err + return fmt.Errorf("failed to get project details, %w", err) } err = runtime.StoreProjectMeta(projectDir, &runtime.ProjectMeta{ID: projectRes.ID, Name: projectRes.Name, Alias: projectRes.Alias}) if err != nil { - utils.Logger.Printf("failed to link project: %s", err) - return err + return fmt.Errorf("failed to store project metadata locally, %w", err) } utils.Logger.Println(styles.Greenf("%s Project", emoji.Link), styles.Pink(projectRes.Name), styles.Green("was linked!")) diff --git a/cmd/login.go b/cmd/login.go index 038988a..3de0061 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -10,7 +10,6 @@ import ( "github.com/deta/space/cmd/utils" "github.com/deta/space/internal/api" "github.com/deta/space/internal/auth" - "github.com/deta/space/pkg/components/emoji" "github.com/deta/space/pkg/components/styles" "github.com/deta/space/pkg/components/text" "github.com/spf13/cobra" @@ -30,8 +29,7 @@ func newCmdLogin() *cobra.Command { if withToken { input, err := io.ReadAll(os.Stdin) if err != nil { - utils.Logger.Println("failed to read access token from standard input") - return err + return fmt.Errorf("failed to read access token from standard input, %w", err) } accessToken = strings.TrimSpace(string(input)) @@ -44,8 +42,7 @@ func newCmdLogin() *cobra.Command { } if err := login(accessToken); err != nil { - utils.Logger.Printf(styles.Errorf("%s Failed to login: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to login, %w", err) } return nil @@ -84,16 +81,14 @@ func login(accessToken string) (err error) { if err != nil { if errors.Is(err, auth.ErrInvalidAccessToken) { - utils.Logger.Printf(styles.Errorf("%s Invalid access token. Please generate a valid token from your Space settings.", emoji.ErrorExclamation)) - return fmt.Errorf("invalid access token") + return fmt.Errorf("invalid access token, please generate a valid token from your Space settings") } - utils.Logger.Printf(styles.Errorf("%s Failed to validate access token: %v", emoji.ErrorExclamation, err)) - return fmt.Errorf("failed to validate access token: %w", err) + return fmt.Errorf("failed to validate access token, %w", err) } err = auth.StoreAccessToken(accessToken) if err != nil { - return fmt.Errorf("failed to store access token: %w", err) + return fmt.Errorf("failed to store access token, %w", err) } utils.Logger.Println(styles.Green("👍 Login Successful!")) diff --git a/cmd/new.go b/cmd/new.go index 5a261ba..a5325da 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -31,8 +31,7 @@ func newCmdNew() *cobra.Command { if !cmd.Flags().Changed("name") { abs, err := filepath.Abs(projectDir) if err != nil { - utils.Logger.Printf("%sError getting absolute path of project directory: %s", styles.ErrorExclamation, err.Error()) - return err + return fmt.Errorf("failed to get absolute path of project directory: %w", err) } name := filepath.Base(abs) @@ -149,8 +148,7 @@ func newProject(projectDir, projectName string, blankProject bool) error { if _, err := os.Stat(spaceFilePath); errors.Is(err, os.ErrNotExist) { err := createSpacefile(projectDir, projectName, blankProject) if err != nil { - utils.Logger.Printf("failed to create spacefile: %s", err) - return err + return fmt.Errorf("failed to create Spacefile, %w", err) } } @@ -161,13 +159,11 @@ func newProject(projectDir, projectName string, blankProject bool) error { utils.Logger.Println(utils.LoginInfo()) return err } - utils.Logger.Printf("failed to create project: %s", err) - return err + return fmt.Errorf("failed to create a project, %w", err) } if err := runtime.StoreProjectMeta(projectDir, meta); err != nil { - utils.Logger.Printf("failed to save project meta, %s", err) - return err + return fmt.Errorf("failed to save project metadata locally, %w", err) } utils.Logger.Println(styles.Greenf("\nProject %s created successfully!", projectName)) diff --git a/cmd/open.go b/cmd/open.go index 5815654..47b2e37 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -5,7 +5,6 @@ import ( "github.com/deta/space/cmd/utils" "github.com/deta/space/internal/runtime" - "github.com/deta/space/pkg/components/emoji" "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -35,15 +34,13 @@ func open(cmd *cobra.Command, args []string) error { var err error projectID, err = runtime.GetProjectID(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to get the project id, %w", err) } } utils.Logger.Printf("Opening project in default browser...\n") if err := browser.OpenURL(fmt.Sprintf("%s/%s", utils.BuilderUrl, projectID)); err != nil { - utils.Logger.Printf("%s Failed to open browser window %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to open a browser window, %w", err) } return nil diff --git a/cmd/push.go b/cmd/push.go index 7ae4eee..0781005 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -44,8 +44,7 @@ Tip: Use the .spaceignore file to exclude certain files and directories from bei var err error projectID, err = runtime.GetProjectID(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to get the project id, %w", err) } } @@ -75,8 +74,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi s, err := spacefile.LoadSpacefile(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to parse Spacefile: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to parse your Spacefile, %w", err) } utils.Logger.Printf(styles.Green("\nYour Spacefile looks good, proceeding with your push!")) @@ -84,22 +82,19 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi // push code & run build steps zippedCode, nbFiles, err := runtime.ZipDir(projectDir) if err != nil { - utils.Logger.Printf("%s Failed to zip project: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to zip your project, %w", err) } build, err := utils.Client.CreateBuild(&api.CreateBuildRequest{AppID: projectID, Tag: pushTag, Experimental: experimental}) if err != nil { - utils.Logger.Printf("%s Failed to push project: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to start a build, %w", err) } utils.Logger.Printf("\n%s Successfully started your build!", emoji.Check) // push spacefile raw, err := os.ReadFile(filepath.Join(projectDir, "Spacefile")) if err != nil { - utils.Logger.Printf("%s Failed to read Spacefile: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to read Spacefile, %w", err) } _, err = utils.Client.PushSpacefile(&api.PushSpacefileRequest{ @@ -107,8 +102,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi BuildID: build.ID, }) if err != nil { - utils.Logger.Println(styles.Errorf("\n%s Failed to push Spacefile, %v", emoji.ErrorExclamation, err)) - return fmt.Errorf("failed to push Spacefile: %w", err) + return fmt.Errorf("failed to push Spacefile, %w", err) } utils.Logger.Printf("%s Successfully pushed your Spacefile!", emoji.Check) @@ -119,8 +113,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi ContentType: icon.IconMeta.ContentType, BuildID: build.ID, }); err != nil { - utils.Logger.Println(styles.Errorf("\n%s Failed to push icon, %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to push the icon, %w", err) } } @@ -131,8 +124,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi utils.Logger.Println(utils.LoginInfo()) return err } - utils.Logger.Printf("%s Failed to push code: %s", emoji.ErrorExclamation, err) - return err + return fmt.Errorf("failed to push your code, %w", err) } utils.Logger.Printf("\n%s Pushing your code (%d files) & running build process...\n\n", emoji.Package, nbFiles) @@ -140,8 +132,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi if skipLogs { b, err := utils.Client.GetBuild(&api.GetBuildRequest{BuildID: build.ID}) if err != nil { - utils.Logger.Printf(styles.Errorf("\n%s Failed to check if build was started. Please check %s for the build status.", emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return fmt.Errorf("failed to check if build was started: %w", err) + return fmt.Errorf("failed to check if the build was started, please check %s for the build status", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } var url = fmt.Sprintf("%s/%s?event=bld-%s", utils.BuilderUrl, projectID, b.Tag) @@ -153,8 +144,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi err = browser.OpenURL(url) if err != nil { - utils.Logger.Printf("%s Failed to open browser window", emoji.ErrorExclamation) - return err + return fmt.Errorf("failed to open a browser window, %w", err) } } @@ -166,7 +156,6 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi BuildID: build.ID, }) if err != nil { - utils.Logger.Printf("%s Error: %v\n", emoji.ErrorExclamation, err) return err } defer readCloser.Close() @@ -179,19 +168,16 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi buildLogger.Println(line) } if err := scanner.Err(); err != nil { - utils.Logger.Printf("%s Error: %v\n", emoji.ErrorExclamation, err) return err } // check build status b, err := utils.Client.GetBuild(&api.GetBuildRequest{BuildID: build.ID}) if err != nil { - utils.Logger.Printf(styles.Errorf("\n%s Failed to check if push succeded. Please check %s if a new revision was created successfully.", emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return err + return fmt.Errorf("%s failed to check if push succeded, please check %s if a new revision was created successfully", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } if b.Status != api.Complete { - utils.Logger.Println(styles.Errorf("\n%s Failed to push code and create a revision. Please try again!", emoji.ErrorExclamation)) - return fmt.Errorf("build isn't complete") + return fmt.Errorf("failed to push code and create a revision, please try again") } // get promotion via build id (build id == revision id) @@ -205,8 +191,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi } if err != nil { - utils.Logger.Printf(styles.Errorf("\n%s Failed to get promotion. Please check %s if a new revision was created successfully.", emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return err + return fmt.Errorf("failed to check if a new revision was created, please check %s manually", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } } @@ -216,7 +201,6 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi ID: p.ID, }) if err != nil { - utils.Logger.Println(styles.Errorf("%s Error: %v", emoji.ErrorExclamation, err)) return err } @@ -226,34 +210,28 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi // we don't want to print the logs to the terminal } if err := scannerPromotion.Err(); err != nil { - utils.Logger.Printf("%s Error: %v\n", emoji.ErrorExclamation, err) return err } // check promotion status p, err = utils.Client.GetReleasePromotion(&api.GetReleasePromotionRequest{PromotionID: p.ID}) if err != nil { - utils.Logger.Printf(styles.Errorf("\n%s Failed to check if Builder instance was updated. Please check %s", emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return err + return fmt.Errorf("failed to check if your Builder instance was updated, please check %s manually", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } if p.Status != api.Complete { - utils.Logger.Println(styles.Errorf("\n%s Failed to update Builder instance. Please try again!", emoji.ErrorExclamation)) - return err + return fmt.Errorf("failed to update your Builder instance, please try again") } // get installation via promotion id (promotion id == release id) i, err := utils.Client.GetInstallationByRelease(&api.GetInstallationByReleaseRequest{ReleaseID: p.ID}) if err != nil { - utils.Logger.Println(styles.Errorf("%s Error: %v", emoji.ErrorExclamation, err)) - utils.Logger.Printf(styles.Errorf("\n%s Failed to get installation. Please check %s if your Builder instance is being updated.", emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return err + return fmt.Errorf("failed to check if your Builder instance is being updated, please check %s manually", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } readCloserInstallation, err := utils.Client.GetInstallationLogs(&api.GetInstallationLogsRequest{ ID: i.ID, }) if err != nil { - utils.Logger.Println(styles.Errorf("%s Error: %v", emoji.ErrorExclamation, err)) return err } @@ -273,19 +251,16 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi } } if err := scannerInstallation.Err(); err != nil { - utils.Logger.Printf("%s Error: %v\n", emoji.ErrorExclamation, err) return err } // check installation status i, err = utils.Client.GetInstallation(&api.GetInstallationRequest{ID: i.ID}) if err != nil { - utils.Logger.Printf(styles.Errorf("\n%s Failed to check if Builder instance was updated. Please check %s", emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return err + return fmt.Errorf("failed to check if your Builder instance was updated, please check %s manually", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } if i.Status != api.Complete { - utils.Logger.Println(styles.Errorf("\n%s Failed to update Builder instance. Please try again!", emoji.ErrorExclamation)) - return err + return fmt.Errorf("failed to update your Builder instance, please try again") } utils.Logger.Println(styles.Greenf("\n%s Successfully pushed your code and updated your Builder instance!", emoji.PartyPopper)) @@ -297,8 +272,7 @@ func push(projectID, projectDir, pushTag string, openInBrowser, skipLogs, experi err = browser.OpenURL(instanceUrl) if err != nil { - utils.Logger.Printf("%s Failed to open browser window", emoji.ErrorExclamation) - return err + return fmt.Errorf("failed to open a browser window, %w", err) } } } diff --git a/cmd/release.go b/cmd/release.go index 70d161e..b4a8714 100644 --- a/cmd/release.go +++ b/cmd/release.go @@ -69,8 +69,7 @@ func newCmdRelease() *cobra.Command { latestRelease, err := utils.Client.GetLatestReleaseByApp(projectID) if err != nil { if !errors.Is(err, api.ErrReleaseNotFound) { - utils.Logger.Println(styles.Errorf("%s Failed to fetch releases: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to the latest release, %w", err) } } @@ -81,18 +80,16 @@ func newCmdRelease() *cobra.Command { if err != nil { return err } - if !continueReleasing { - utils.Logger.Println("Aborted releasing this app.") - return err + utils.Logger.Println("Aborted.") + return nil } } } discoveryData, err := getDiscoveryData(projectDir) if err != nil { - utils.Logger.Printf("Failed to get discovery data: %v", err) - return err + return fmt.Errorf("failed to get Discovery data, %w", err) } if latestRelease != nil { @@ -130,8 +127,7 @@ func newCmdRelease() *cobra.Command { latestListedRelease, err := utils.Client.GetLatestListedReleaseByApp(projectID) if err != nil { if !errors.Is(err, api.ErrReleaseNotFound) { - utils.Logger.Println(styles.Errorf("%s Failed to fetch latest listed release: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to fetch the latest listed release for this app, %w", err) } } if latestListedRelease != nil { @@ -144,7 +140,7 @@ func newCmdRelease() *cobra.Command { utils.Logger.Printf(getCreatingReleaseMsg(listedRelease, useLatestRevision)) - err = release(projectDir, projectID, revisionID, releaseVersion, + err = release(projectID, revisionID, releaseVersion, listedRelease, releaseNotes, discoveryData) if err != nil { return err @@ -254,14 +250,12 @@ func compareDiscoveryData(discoveryData *shared.DiscoveryData, latestRelease *ap p := filepath.Join(projectDir, discovery.DiscoveryFilename) modTime, err := fs.GetFileLastChanged(p) if err != nil { - utils.Logger.Println(styles.Errorf("%s Failed to check if local Discovery data is outdated: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to check if your local Discovery data is outdated, %w", err) } parsedTime, err := time.Parse(time.RFC3339, latestRelease.ReleasedAt) if err != nil { - utils.Logger.Println(styles.Errorf("%s Failed to check if local Discovery data is outdated: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to check if your local Discovery data is outdated, %w", err) } if modTime.Before(parsedTime) { @@ -269,7 +263,7 @@ func compareDiscoveryData(discoveryData *shared.DiscoveryData, latestRelease *ap updateLocalDiscovery, err := confirm.Run("Do you want to update your local Discovery.md file with the data from the latest release?") if err != nil { - utils.Logger.Println("Aborted releasing this app.") + utils.Logger.Println("Aborted.") return err } @@ -278,19 +272,18 @@ func compareDiscoveryData(discoveryData *shared.DiscoveryData, latestRelease *ap discoveryPath := filepath.Join(projectDir, discovery.DiscoveryFilename) err := discovery.CreateDiscoveryFile(discoveryPath, *discoveryData) if err != nil { - utils.Logger.Println(styles.Errorf("%s Failed to update local Discovery.md file: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to update local Discovery.md file, %w", err) } utils.Logger.Printf("\n%s Updated your local Discovery.md file with the latest data!\n\n", emoji.Check) } else { continueReleasing, err := confirm.Run("Are you sure you want to continue releasing the app with the local Discovery data?") if err != nil { - utils.Logger.Println("Aborted releasing this app.") + utils.Logger.Println("Aborted.") return err } else if !continueReleasing { - utils.Logger.Println("Aborted releasing this app.") - return fmt.Errorf("aborted releasing this app") + utils.Logger.Println("Aborted.") + return nil } } } @@ -307,12 +300,11 @@ func getDiscoveryData(projectDir string) (*shared.DiscoveryData, error) { } discoveryData, err := promptForDiscoveryData() if err != nil { - utils.Logger.Printf("%s Error: %v", emoji.ErrorExclamation, err) + return nil, err } err = discovery.CreateDiscoveryFile(discoveryPath, *discoveryData) if err != nil { - utils.Logger.Printf("%s Failed to create Discovery.md file, %v", emoji.ErrorExclamation, err) - return nil, err + return nil, fmt.Errorf("failed to create Discovery.md file, %w", err) } utils.Logger.Printf("\n%s Created a new Discovery.md file that stores this data!\n\n", emoji.Check) @@ -330,8 +322,7 @@ func getDiscoveryData(projectDir string) (*shared.DiscoveryData, error) { discoveryData := &shared.DiscoveryData{} rest, err := frontmatter.Parse(bytes.NewReader(df), &discoveryData) if err != nil { - utils.Logger.Println(styles.Errorf("\n%s Failed to parse Discovery file, %v", emoji.ErrorExclamation, err)) - return nil, err + return nil, fmt.Errorf("failed to parse the Discovery file, %w", err) } discoveryData.ContentRaw = string(rest) @@ -357,8 +348,7 @@ func selectRevisionByTagName(projectID string, tagName string) (*api.Revision, e utils.Logger.Println(utils.LoginInfo()) return nil, err } else { - utils.Logger.Println(styles.Errorf("%s Failed to get revision: %v", emoji.ErrorExclamation, err)) - return nil, err + return nil, fmt.Errorf("failed to fetch the revision from the tag: %w", err) } } @@ -372,8 +362,7 @@ func selectRevision(projectID string, useLatestRevision bool) (*api.Revision, er utils.Logger.Println(utils.LoginInfo()) return nil, err } else { - utils.Logger.Println(styles.Errorf("%s Failed to get revisions: %v", emoji.ErrorExclamation, err)) - return nil, err + return nil, fmt.Errorf("failed to fetch the revisions: %w", err) } } revisions := r.Revisions @@ -413,7 +402,7 @@ func selectRevision(projectID string, useLatestRevision bool) (*api.Revision, er } // release xx -func release(projectDir string, projectID string, +func release(projectID string, revisionID string, releaseVersion string, listedRelease bool, releaseNotes string, discoveryData *shared.DiscoveryData) (err error) { @@ -431,20 +420,17 @@ func release(projectDir string, projectID string, utils.Logger.Println(utils.LoginInfo()) return nil } - utils.Logger.Println(styles.Errorf("%s Failed to create release: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to create a new release, %w", err) } err = utils.Client.StoreDiscoveryData(cr.ID, discoveryData) if err != nil { - utils.Logger.Println(styles.Errorf("%s Error: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to store Discovery data, %w", err) } screenshots, err := discovery.ParseScreenshot(discoveryData.Media) if err != nil { - utils.Logger.Println(styles.Errorf("%s Failed to create release: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to process Discovery data media, %w", err) } var wg sync.WaitGroup @@ -468,8 +454,7 @@ func release(projectDir string, projectID string, close(errChan) for err := range errChan { if err != nil { - utils.Logger.Println(styles.Errorf("\n%s Failed to push screenshot, %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to push Discovery data media, %w", err) } } @@ -477,8 +462,7 @@ func release(projectDir string, projectID string, ID: cr.ID, }) if err != nil { - utils.Logger.Println(styles.Errorf("%s Error: %v", emoji.ErrorExclamation, err)) - return err + return fmt.Errorf("failed to get release logs, %w", err) } defer readCloser.Close() @@ -493,17 +477,12 @@ func release(projectDir string, projectID string, } } if err := scanner.Err(); err != nil { - utils.Logger.Printf("%s Error: %v\n", emoji.ErrorExclamation, err) return err } r, err := utils.Client.GetReleasePromotion(&api.GetReleasePromotionRequest{PromotionID: cr.ID}) if err != nil { - utils.Logger.Printf( - styles.Errorf( - "\n%s Failed to check if release succeeded. Please check %s if a new release was created successfully.", - emoji.ErrorExclamation, styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID))) - return err + return fmt.Errorf("failed to check if the release succeeded, please check %s manually", styles.Codef("%s/%s/develop", utils.BuilderUrl, projectID)) } if r.Status == api.Complete { @@ -519,8 +498,7 @@ func release(projectDir string, projectID string, utils.Logger.Printf("\nRelease: %s", styles.Code(releaseUrl)) } } else { - utils.Logger.Println(styles.Errorf("\n%s Failed to create release. Please try again!", emoji.ErrorExclamation)) - return fmt.Errorf("release failed: %s", r.Status) + return fmt.Errorf("failed to create the release, please try again") } return nil diff --git a/cmd/root.go b/cmd/root.go index 9dc95d3..5aae85b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,7 +18,14 @@ Complete documentation available at %s`, utils.DocsUrl), cmd.Usage() }, DisableAutoGenTag: true, - Version: utils.SpaceVersion, + // This will prevent the usage from being displayed when an error occurs + // while calling the Execute function in the main.go file. + SilenceUsage: true, + // This will prevent the error message from being displayed when an error + // We will handle printing the error message ourselves. + // Each subcommand must use RunE instead of Run. + SilenceErrors: true, + Version: utils.SpaceVersion, } cmd.AddCommand(newCmdLogin()) @@ -36,14 +43,5 @@ Complete documentation available at %s`, utils.DocsUrl), cmd.AddCommand(newCmdTrigger()) cmd.AddCommand(newCmdBuilder()) - // XXX: This will prevent the usage from being displayed when an error occurs - // while calling the Execute function in the main.go file. - cmd.SilenceUsage = true - - // This will prevent the error message from being displayed when an error - // We will handle printing the error message ourselves. - // Each subcommand must use RunE instead of Run. - cmd.SilenceErrors = true - return cmd } diff --git a/cmd/trigger.go b/cmd/trigger.go index 479990a..14b8244 100644 --- a/cmd/trigger.go +++ b/cmd/trigger.go @@ -35,11 +35,6 @@ type ActionInput []struct { type InputType string -var ( - InputTypeString InputType = "string" - InputTypeBool InputType = "bool" -) - type ActionOutput struct { Type string `json:"type"` Data any `json:"data"` diff --git a/cmd/validate.go b/cmd/validate.go index 8bcff60..51ab523 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -32,35 +32,29 @@ func newCmdValidate() *cobra.Command { } func validate(projectDir string) error { - utils.Logger.Printf("\n%s Validating Spacefile...", emoji.Package) + utils.Logger.Printf("\n%s Validating your Spacefile...\n", emoji.Package) s, err := spacefile.LoadSpacefile(projectDir) if err != nil { - utils.Logger.Println(styles.Errorf("\n%s Detected some issues with your Spacefile. Please fix them before pushing your code.", emoji.ErrorExclamation)) - utils.Logger.Println() - utils.Logger.Println(err.Error()) - return err + return fmt.Errorf("failed to parse Spacefile, %w", err) } if s.Icon == "" { utils.Logger.Printf("\n%s No app icon specified.", styles.Blue("i")) } else { if err := spacefile.ValidateIcon(s.Icon); err != nil { - utils.Logger.Println(styles.Errorf("\nDetected some issues with your icon. Please fix them before pushing your code.")) switch { case errors.Is(spacefile.ErrInvalidIconType, err): - utils.Logger.Println(styles.Error("L Invalid icon type. Please use a 512x512 sized PNG or WebP icon")) + return fmt.Errorf("invalid icon type, please use a 512x512 sized PNG or WebP icon") case errors.Is(spacefile.ErrInvalidIconSize, err): - utils.Logger.Println(styles.Error("L Icon size is not valid. Please use a 512x512 sized PNG or WebP icon")) + return fmt.Errorf("icon size is not valid, please use a 512x512 sized PNG or WebP icon") case errors.Is(spacefile.ErrInvalidIconPath, err): - utils.Logger.Println(styles.Error("L Cannot find icon path. Please provide a valid icon path or leave it empty to auto-generate project icon.")) + return fmt.Errorf("cannot find the icon in provided path, please provide a valid icon path or leave it empty to auto-generate one") default: - utils.Logger.Println(styles.Error(fmt.Sprintf("%s Validation Error: %v", emoji.X, err))) + return err } - return err } } - utils.Logger.Println(styles.Greenf("\n%s Spacefile looks good!", emoji.Sparkles)) return nil } diff --git a/cmd/version_ugrade.go b/cmd/version_ugrade.go index e3eb60b..45dd049 100644 --- a/cmd/version_ugrade.go +++ b/cmd/version_ugrade.go @@ -9,7 +9,6 @@ import ( "github.com/deta/space/cmd/utils" "github.com/deta/space/internal/api" detaruntime "github.com/deta/space/internal/runtime" - "github.com/deta/space/pkg/components/emoji" "github.com/deta/space/pkg/components/styles" "github.com/spf13/cobra" ) @@ -24,8 +23,7 @@ func newCmdVersionUpgrade(currentVersion string) *cobra.Command { if !cmd.Flags().Changed("version") { latestVersion, err := api.GetLatestCliVersion() if err != nil { - utils.Logger.Println(styles.Errorf("%s Failed to get latest version. Please try again.", emoji.X)) - return err + return fmt.Errorf("failed to get the latest version, %w, please try again", err) } targetVersion = latestVersion } @@ -39,18 +37,17 @@ func newCmdVersionUpgrade(currentVersion string) *cobra.Command { case "linux", "darwin": err := upgradeUnix(targetVersion) if err != nil { - utils.Logger.Println(styles.Errorf("%s Upgrade failed. Please try again.", emoji.X)) - return err + return fmt.Errorf("failed to upgrade, %w, please try again", err) } case "windows": err := upgradeWin(targetVersion) if err != nil { - utils.Logger.Println(styles.Errorf("%s Upgrade failed. Please try again.", emoji.X)) - return err + if err != nil { + return fmt.Errorf("failed to upgrade, %w, please try again", err) + } } default: - utils.Logger.Println(styles.Errorf("%s Upgrade not supported for %s", emoji.X, runtime.GOOS)) - return fmt.Errorf("%s Upgrade not supported for %s", emoji.X, runtime.GOOS) + return fmt.Errorf("unsupported OS, %s", runtime.GOOS) } detaruntime.CacheLatestVersion(targetVersion)