diff --git a/cmd/root.go b/cmd/root.go index 538a322..60ae63d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,8 +3,10 @@ package cmd import ( "fmt" "os" + "strings" "github.com/GoToolSharing/htb-cli/config" + "github.com/GoToolSharing/htb-cli/lib/update" "github.com/spf13/cobra" "go.uber.org/zap" ) @@ -25,6 +27,15 @@ var rootCmd = &cobra.Command{ config.GlobalConfig.Logger.Error("", zap.Error(err)) os.Exit(1) } + message, err := update.Check(config.Version) + if err != nil { + config.GlobalConfig.Logger.Error("", zap.Error(err)) + os.Exit(1) + } + config.GlobalConfig.Logger.Debug(fmt.Sprintf("Message : %s", message)) + if strings.Contains(message, "A new update") { + fmt.Println(message) + } }, } diff --git a/cmd/update.go b/cmd/update.go index c1c5cc6..ae94f94 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "github.com/GoToolSharing/htb-cli/config" @@ -14,11 +15,12 @@ var updateCmd = &cobra.Command{ Short: "Check for updates", Run: func(cmd *cobra.Command, args []string) { config.GlobalConfig.Logger.Info("Update command executed") - err := update.Check(config.Version) + message, err := update.Check(config.Version) if err != nil { config.GlobalConfig.Logger.Error("", zap.Error(err)) os.Exit(1) } + fmt.Println(message) config.GlobalConfig.Logger.Info("Exit update command correctly") }, diff --git a/lib/update/update.go b/lib/update/update.go index fe5301f..86077ee 100644 --- a/lib/update/update.go +++ b/lib/update/update.go @@ -11,7 +11,7 @@ import ( "github.com/GoToolSharing/htb-cli/lib/webhooks" ) -func Check(newVersion string) error { +func Check(newVersion string) (string, error) { // Dev version config.GlobalConfig.Logger.Debug(fmt.Sprintf("config.Version: %s", config.Version)) if len(config.Version) == 40 { @@ -20,17 +20,17 @@ func Check(newVersion string) error { resp, err := utils.HTTPRequest(http.MethodGet, githubCommits, nil) if err != nil { - return err + return "", err } body, err := io.ReadAll(resp.Body) config.GlobalConfig.Logger.Debug(fmt.Sprintf("Body: %s", utils.TruncateString(string(body), 2000))) if err != nil { - return fmt.Errorf("error when reading the response: %v", err) + return "", fmt.Errorf("error when reading the response: %v", err) } var commits []Commit err = json.Unmarshal(body, &commits) if err != nil { - return fmt.Errorf("error when decoding JSON: %v", err) + return "", fmt.Errorf("error when decoding JSON: %v", err) } config.GlobalConfig.Logger.Debug(fmt.Sprintf("Commits : %v", commits)) @@ -49,14 +49,12 @@ func Check(newVersion string) error { message = fmt.Sprintf("You're up to date (dev) ! (%s)", commitHash) } - fmt.Println(message) - err = webhooks.SendToDiscord("update", message) if err != nil { - return err + return "", err } - return nil + return message, nil } // Main version @@ -64,11 +62,11 @@ func Check(newVersion string) error { resp, err := utils.HTTPRequest(http.MethodGet, githubVersion, nil) if err != nil { - return err + return "", err } var release GitHubRelease if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { - return fmt.Errorf("error when decoding JSON: %v", err) + return "", fmt.Errorf("error when decoding JSON: %v", err) } config.GlobalConfig.Logger.Debug(fmt.Sprintf("release.TagName : %s", release.TagName)) config.GlobalConfig.Logger.Debug(fmt.Sprintf("config.Version : %s", config.Version)) @@ -79,13 +77,11 @@ func Check(newVersion string) error { message = fmt.Sprintf("You're up to date ! (%s)", config.Version) } - fmt.Println(message) - err = webhooks.SendToDiscord("update", message) if err != nil { - return err + return "", err } - return nil + return message, nil }