Skip to content

Commit

Permalink
Update dependencies (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmdrs authored Sep 21, 2021
1 parent 3da14dd commit e246e46
Show file tree
Hide file tree
Showing 3 changed files with 699 additions and 177 deletions.
33 changes: 20 additions & 13 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package cmd

import (
"context"
"fmt"
"os"
"runtime"
"strings"
"time"

"github.com/hellofresh/updater-go"
"github.com/hellofresh/updater-go/v3"
"github.com/palantir/stacktrace"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
githubOwner = "hellofresh"
githubRepo = "klepto"
githubOwner = "hellofresh"
githubRepo = "klepto"
defaultConnectionTimeout = time.Duration(5 * time.Second)
)

// UpdateOptions are the command flags
type UpdateOptions struct {
token string
version string
dryRun bool
timeout time.Duration
}

// NewUpdateCmd creates a new update command
Expand All @@ -33,19 +37,20 @@ func NewUpdateCmd() *cobra.Command {
Aliases: []string{"self-update"},
Short: "Check for new versions of kepto",
RunE: func(cmd *cobra.Command, args []string) error {
return RunUpdate(opts)
return RunUpdate(cmd.Context(), opts)
},
}

cmd.PersistentFlags().StringVar(&opts.token, "token", "", "Github token that will be used to check for Klepto! versions. If not set GITHUB_TOKEN environment variable value is used.")
cmd.PersistentFlags().StringVar(&opts.version, "version", "", "Update to specific version instead of the latest stable.")
cmd.PersistentFlags().BoolVar(&opts.dryRun, "dry-run", false, "check the version available but do not run actual update")
cmd.PersistentFlags().BoolVar(&opts.dryRun, "dry-run", false, "Check the version available but do not run actual update")
cmd.PersistentFlags().DurationVar(&opts.timeout, "timeout", defaultConnectionTimeout, "Connection timeout")

return cmd
}

// RunUpdate runs the update command
func RunUpdate(opts *UpdateOptions) error {
func RunUpdate(ctx context.Context, opts *UpdateOptions) error {
log.Info("Checking for new versions of Klepto!")

if opts.token == "" {
Expand All @@ -62,10 +67,10 @@ func RunUpdate(opts *UpdateOptions) error {
}

// Create release locator
locator := newReleaseLocator(opts.token, versionFilter)
locator := newReleaseLocator(ctx, opts.token, versionFilter, opts.timeout)

// Find the release
updateTo, err := locateRelease(locator, updateToVersion)
updateTo, err := locateRelease(ctx, locator, updateToVersion)
if rootErr := stacktrace.RootCause(err); rootErr == updater.ErrNoRepository {
// fatal exits with code 1
log.Fatal("Unable to access the Klepto! repository.")
Expand All @@ -77,7 +82,7 @@ func RunUpdate(opts *UpdateOptions) error {
if updateTo.Name != version {
// Fetch the release and update
if !opts.dryRun {
if err := updater.SelfUpdate(updateTo); err != nil {
if err := updater.SelfUpdate(ctx, updateTo); err != nil {
return wErrors.Wrapf(err, "failed to update to version %s", updateTo.Name)
}
}
Expand All @@ -90,26 +95,28 @@ func RunUpdate(opts *UpdateOptions) error {
return nil
}

func newReleaseLocator(token string, filter updater.ReleaseFilter) updater.ReleaseLocator {
func newReleaseLocator(ctx context.Context, token string, filter updater.ReleaseFilter, timeout time.Duration) updater.ReleaseLocator {
return updater.NewGithubClient(
ctx,
githubOwner,
githubRepo,
token,
filter,
func(asset string) bool {
return strings.Contains(asset, fmt.Sprintf("_%s_%s", runtime.GOOS, runtime.GOARCH))
},
timeout,
)
}
func locateRelease(locator updater.ReleaseLocator, version string) (updater.Release, error) {
func locateRelease(ctx context.Context, locator updater.ReleaseLocator, version string) (updater.Release, error) {
// No specific version use the latest
if version == "" {
return updater.LatestRelease(locator)
return updater.LatestRelease(ctx, locator)
}

// Find a specific release
var release updater.Release
updates, err := locator.ListReleases(1)
updates, err := locator.ListReleases(ctx, 1)
if err != nil {
return release, err
}
Expand Down
65 changes: 40 additions & 25 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
module github.com/hellofresh/klepto

go 1.14
go 1.17

require (
github.com/BurntSushi/toml v0.3.1
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/squirrel v1.2.0
github.com/corpix/uarand v0.1.1 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.3.3 // indirect
github.com/hellofresh/updater-go v1.0.1
github.com/BurntSushi/toml v0.4.1
github.com/Masterminds/squirrel v1.5.0
github.com/go-sql-driver/mysql v1.6.0
github.com/hellofresh/updater-go/v3 v3.0.0
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/lib/pq v1.3.0
github.com/lib/pq v1.10.3
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/pkg/errors v0.9.1
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0 // indirect
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.5.0
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v0.0.5
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
)

require (
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/corpix/uarand v0.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa // indirect
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.1
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
google.golang.org/appengine v1.6.5 // indirect
gopkg.in/ini.v1 v1.51.1 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit e246e46

Please sign in to comment.