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

Commit 2b19383

Browse files
authored
Merge pull request #9 from hellofresh/add-update-cmd
Add update cmd
2 parents e3febfb + 8679a86 commit 2b19383

File tree

5 files changed

+145
-2
lines changed

5 files changed

+145
-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.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@
4747
[[constraint]]
4848
name = "github.com/dghubble/sling"
4949
version = "1.1.0"
50+
51+
[[constraint]]
52+
name = "github.com/italolelis/goupdater"
53+
version = "0.1.0"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ github-cli [command] [--flags]
3535
| `github-cli repo delete [--flags]` | Deletes a github repository |
3636
| `github-cli hiring send [--flags]` | Creates a new hellofresh hiring test |
3737
| `github-cli hiring unseat [--flags]` | Removes external collaborators from repositories |
38+
| `github-cli update ` | Check for new versions of github-cli |
3839
| `github-cli version` | Prints the version information |
3940

4041
## Contributing

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

0 commit comments

Comments
 (0)