From df22851e8feb7b43ee508e767c1852a06c16fb4d Mon Sep 17 00:00:00 2001 From: Efertone Date: Tue, 3 Oct 2023 16:23:26 +0200 Subject: [PATCH] refactor: remove update command There are a lot of ways to update totp-cli. The update command always had some weird issues, it's easier to just let other well known methods to update the binary (`go install` for example). Invalidates these issues: - Closes #4 - Closes #9 - Closes #33 Signed-off-by: Efertone --- README.md | 7 --- app.go | 1 - go.mod | 2 - go.sum | 10 ---- internal/cmd/const.go | 2 - internal/cmd/update.go | 129 ----------------------------------------- 6 files changed, 151 deletions(-) delete mode 100644 internal/cmd/update.go diff --git a/README.md b/README.md index 4921cdc..83c046a 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,6 @@ or [Homebrew](https://brew.sh/): brew install totp-cli ``` -### Update - -```shell -totp-cli update -``` - #### Upgrading from totp-cli v1.2.7 or below Starting with totp-cli v1.2.8 a [more secure storage @@ -76,7 +70,6 @@ COMMANDS: instant Generate an OTP from TOTP_TOKEN or stdin without the Storage backend. list List all available namespaces or accounts under a namespace. set-prefix Set prefix for a token. - update Check and update totp-cli itself help, h Shows a list of commands or help for one command GLOBAL OPTIONS: diff --git a/app.go b/app.go index 1141f9a..71c6415 100644 --- a/app.go +++ b/app.go @@ -28,7 +28,6 @@ func newApplication() *cli.App { cmd.InstantCommand(), cmd.ListCommand(), cmd.SetPrefixCommand(), - cmd.UpdateCommand(), cmd.SetLengthCommand(), cmd.RenameCommand(), }, diff --git a/go.mod b/go.mod index 74b01bd..e370bcb 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,8 @@ go 1.21 require ( filippo.io/age v1.1.1 - github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 github.com/stretchr/testify v1.7.1 github.com/urfave/cli/v2 v2.25.7 - github.com/yitsushi/github-release-check v1.0.2 golang.org/x/term v0.11.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 08f2bbe..a7b24fa 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= -github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -18,18 +16,10 @@ github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= -github.com/yitsushi/github-release-check v1.0.2 h1:UywTeCISNTnl5gu2eHQgIS9kQboSCYF/39W4dapDzUY= -github.com/yitsushi/github-release-check v1.0.2/go.mod h1:jt68RrcS1VcNoZaoCXh+iqODmNHVtVyV4r3RoPA0jhI= -golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8= -golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI= -golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/internal/cmd/const.go b/internal/cmd/const.go index 2869745..36b8220 100644 --- a/internal/cmd/const.go +++ b/internal/cmd/const.go @@ -11,7 +11,5 @@ const ( argSetLengthPositionAccount = 1 argSetLengthPositionPrefix = 2 - binaryChmodValue = 0o755 - strictDumpFilePerms = 0o600 ) diff --git a/internal/cmd/update.go b/internal/cmd/update.go deleted file mode 100644 index 141aff0..0000000 --- a/internal/cmd/update.go +++ /dev/null @@ -1,129 +0,0 @@ -package cmd - -import ( - "archive/tar" - "compress/gzip" - "context" - "fmt" - "io" - "net/http" - "os" - "path" - "runtime" - - "github.com/kardianos/osext" - "github.com/urfave/cli/v2" - grc "github.com/yitsushi/github-release-check" - - "github.com/yitsushi/totp-cli/internal/info" -) - -// UpdateCommand is the update subcommand. -func UpdateCommand() *cli.Command { - return &cli.Command{ - Name: "update", - Usage: fmt.Sprintf("Check and update %s itself", info.AppName), - ArgsUsage: " ", - Description: `Check for updates. -If there is a newer version of this application for this OS and ARCH, -then download it and replace this application with the newer one.`, - Action: func(_ *cli.Context) error { - hasUpdate, release, _ := grc.Check(info.AppRepoOwner, info.AppName, info.AppVersion) - - if !hasUpdate { - fmt.Printf("Your %s is up-to-date. \\o/\n", info.AppName) - - return nil - } - - var ( - assetToDownload grc.Asset - found bool - ) - - for _, asset := range release.Assets { - buildFilename := fmt.Sprintf("%s-%s-%s-%s.tar.gz", info.AppName, release.TagName, runtime.GOOS, runtime.GOARCH) - if asset.Name == buildFilename { - assetToDownload = asset - found = true - - break - } - } - - if !found { - fmt.Printf("Your %s is up-to-date. \\o/\n", info.AppName) - - return nil - } - - fmt.Printf("Target: %s\n", assetToDownload.Name) - - err := downloadBinary(assetToDownload.BrowserDownloadURL) - if err != nil { - return err - } - - fmt.Printf("Now you have a fresh new %s \\o/\n", info.AppName) - - return nil - }, - } -} - -func downloadBinary(uri string) error { - fmt.Println(" -> Download...") - - client := http.Client{} - - request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, uri, nil) - if err != nil { - return DownloadError{Message: err.Error()} - } - - response, err := client.Do(request) - if err != nil { - return DownloadError{Message: err.Error()} - } - - defer response.Body.Close() - - gzipReader, _ := gzip.NewReader(response.Body) - defer gzipReader.Close() - - fmt.Println(" -> Extract...") - - tarReader := tar.NewReader(gzipReader) - - _, err = tarReader.Next() - if err != nil { - return DownloadError{Message: err.Error()} - } - - currentExecutable, _ := osext.Executable() - originalPath := path.Dir(currentExecutable) - - file, err := os.CreateTemp(originalPath, info.AppName) - if err != nil { - return DownloadError{Message: err.Error()} - } - - defer file.Close() - - _, err = io.Copy(file, tarReader) //nolint:gosec // I don't have better option right now. - if err != nil { - return DownloadError{Message: err.Error()} - } - - err = file.Chmod(binaryChmodValue) - if err != nil { - return DownloadError{Message: err.Error()} - } - - err = os.Rename(file.Name(), currentExecutable) - if err != nil { - return DownloadError{Message: err.Error()} - } - - return nil -}