Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 094ca3f

Browse files
committed
Added update cmd
1 parent e3febfb commit 094ca3f

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

Gopkg.lock

Lines changed: 76 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewRootCmd() *cobra.Command {
4242
ctx := log.NewContext(context.Background())
4343
ctx, err := config.NewContext(ctx, opts.configFile)
4444
if err != nil {
45-
log.WithContext(ctx).WithError(err).Error("Could not load configuration file")
45+
log.WithContext(ctx).WithError(err).Fatal("Could not load configuration file")
4646
}
4747

4848
cfg := config.WithContext(ctx)
@@ -66,6 +66,7 @@ func NewRootCmd() *cobra.Command {
6666
cmd.AddCommand(NewRepoCmd(ctx))
6767
cmd.AddCommand(NewHiringCmd(ctx))
6868
cmd.AddCommand(NewVersionCmd(ctx))
69+
cmd.AddCommand(NewUpdateCmd(ctx))
6970

7071
return &cmd
7172
}

cmd/update.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/errwrap"
8+
9+
"github.com/hellofresh/github-cli/pkg/log"
10+
"github.com/italolelis/goupdater"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
const (
15+
githubOwner = "hellofresh"
16+
githubRepo = "github-cli"
17+
)
18+
19+
// UpdateOptions are the command flags
20+
type UpdateOptions struct{}
21+
22+
// NewUpdateCmd creates a new update command
23+
func NewUpdateCmd(ctx context.Context) *cobra.Command {
24+
opts := &UpdateOptions{}
25+
26+
cmd := &cobra.Command{
27+
Use: "update",
28+
Aliases: []string{"self-update"},
29+
Short: fmt.Sprintf("Check for new versions of %s", githubRepo),
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
return RunUpdate(ctx, opts)
32+
},
33+
}
34+
35+
return cmd
36+
}
37+
38+
// RunUpdate runs the update command
39+
func RunUpdate(ctx context.Context, opts *UpdateOptions) error {
40+
logger := log.WithContext(ctx)
41+
logger.Info("Checking if any new version is available...")
42+
43+
resolver, err := goupdater.NewGithubWithContext(ctx, goupdater.GithubOpts{
44+
Owner: githubOwner,
45+
Repo: githubRepo,
46+
})
47+
if err != nil {
48+
return errwrap.Wrapf("could not create the updater client: {{err}}", err)
49+
}
50+
51+
updated, err := goupdater.UpdateWithContext(ctx, resolver, version)
52+
if err != nil {
53+
return errwrap.Wrapf("could not update the binary: {{err}}", err)
54+
}
55+
56+
if updated {
57+
logger.Infof("You are now using the latest version of %s", githubRepo)
58+
} else {
59+
logger.Infof("You already have the latest version of %s", githubRepo)
60+
}
61+
62+
return nil
63+
}

0 commit comments

Comments
 (0)