From b734ad802a1983be35c6d78e815c48562b6f2267 Mon Sep 17 00:00:00 2001 From: Mathis Maquenne <124215603+mathismqn@users.noreply.github.com> Date: Thu, 30 Jul 2026 10:35:10 +0200 Subject: [PATCH] feat: add update command --- cmd/update.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 cmd/update.go diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 0000000..2944fb7 --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "errors" + "fmt" + "os" + + "github.com/mathismqn/godeez/internal/buildinfo" + "github.com/mathismqn/godeez/internal/updater" + "github.com/spf13/cobra" +) + +var updateOpts struct { + checkOnly bool + force bool +} + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update GoDeez to the latest version", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + err := runUpdate(cmd.Context()) + if errors.Is(err, context.Canceled) { + return nil + } + + return err + }, +} + +func runUpdate(ctx context.Context) error { + if err := updater.CheckUpdatable(); err != nil { + return err + } + + u := updater.New() + u.Out = os.Stdout + current := buildinfo.Version() + + release, err := u.Latest(ctx) + if err != nil { + return fmt.Errorf("failed to check for updates: %w", err) + } + + latest := release.Version() + fmt.Printf("Current: %s\nLatest: %s\n", current, latest) + + if !updater.IsNewer(current, latest) && !updateOpts.force { + fmt.Println("Already up to date.") + + return nil + } + + if updateOpts.checkOnly { + fmt.Printf("Run `godeez update` to install %s.\n", latest) + + return nil + } + + if err := u.Apply(ctx, release); err != nil { + return err + } + fmt.Printf("Updated to %s.\n", latest) + + return nil +} + +func init() { + RootCmd.AddCommand(updateCmd) + + updateCmd.Flags().BoolVar(&updateOpts.checkOnly, "check", false, "only report whether an update is available") + updateCmd.Flags().BoolVar(&updateOpts.force, "force", false, "reinstall even if already up to date") +}