Skip to content

Commit

Permalink
Add informer
Browse files Browse the repository at this point in the history
  • Loading branch information
qu35t-code committed Jan 6, 2024
1 parent 1ad9006 commit ece0cda
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"os"

"github.com/GoToolSharing/htb-cli/config"
Expand All @@ -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")
},
Expand Down
24 changes: 10 additions & 14 deletions lib/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))

Expand All @@ -49,26 +49,24 @@ 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
githubVersion := "https://api.github.com/repos/GoToolSharing/htb-cli/releases/latest"

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))
Expand All @@ -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

}

0 comments on commit ece0cda

Please sign in to comment.