From a6496fc946df00e5adda32aed960304e9169a3a4 Mon Sep 17 00:00:00 2001 From: soup Date: Thu, 23 Feb 2023 23:08:59 +0100 Subject: [PATCH] feat(autobrrctl): Show latest version in version output (#723) * initial commit * initial commit * fix * removed unused code * go mod tidy --- cmd/autobrrctl/main.go | 101 ++++++++++++++++++++++++++++------------- go.mod | 2 +- 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/cmd/autobrrctl/main.go b/cmd/autobrrctl/main.go index 4002b18..9714505 100644 --- a/cmd/autobrrctl/main.go +++ b/cmd/autobrrctl/main.go @@ -3,9 +3,11 @@ package main import ( "bufio" "context" + "encoding/json" "flag" "fmt" "log" + "net/http" "os" "github.com/autobrr/autobrr/internal/config" @@ -15,57 +17,75 @@ import ( "github.com/autobrr/autobrr/pkg/argon2id" "github.com/autobrr/autobrr/pkg/errors" - "golang.org/x/crypto/ssh/terminal" + "golang.org/x/term" _ "modernc.org/sqlite" ) const usage = `usage: autobrrctl --config path - create-user Create user - change-password Change password for user - version Print version info - help Show this help message -` + create-user Create user + change-password Change password for user + version Can be run without --config + help Show this help message -func init() { - flag.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), usage) - } -} +` var ( version = "dev" commit = "" date = "" + + owner = "autobrr" + repo = "autobrr" ) +func init() { + flag.Usage = func() { + fmt.Fprint(flag.CommandLine.Output(), usage) + } +} + func main() { var configPath string flag.StringVar(&configPath, "config", "", "path to configuration file") flag.Parse() - if configPath == "" { - log.Fatal("--config required") - } - - // read config - cfg := config.New(configPath, version) - - // init new logger - l := logger.New(cfg.Config) - - // open database connection - db, _ := database.NewDB(cfg.Config, l) - if err := db.Open(); err != nil { - log.Fatal("could not open db connection") - } - - userRepo := database.NewUserRepo(l, db) - switch cmd := flag.Arg(0); cmd { case "version": fmt.Fprintf(flag.CommandLine.Output(), "Version: %v\nCommit: %v\nBuild: %v\n", version, commit, date) + + // get the latest release tag from Github + resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)) + if err == nil && resp.StatusCode == http.StatusOK { + defer resp.Body.Close() + var rel struct { + TagName string `json:"tag_name"` + } + if err := json.NewDecoder(resp.Body).Decode(&rel); err == nil { + fmt.Fprintf(flag.CommandLine.Output(), "Latest release: %v\n", rel.TagName) + } + } + case "create-user": + + if configPath == "" { + log.Fatal("--config required") + } + + // read config + cfg := config.New(configPath, version) + + // init new logger + l := logger.New(cfg.Config) + + // open database connection + db, _ := database.NewDB(cfg.Config, l) + if err := db.Open(); err != nil { + log.Fatal("could not open db connection") + } + + userRepo := database.NewUserRepo(l, db) + username := flag.Arg(1) if username == "" { flag.Usage() @@ -89,6 +109,25 @@ func main() { log.Fatalf("failed to create user: %v", err) } case "change-password": + + if configPath == "" { + log.Fatal("--config required") + } + + // read config + cfg := config.New(configPath, version) + + // init new logger + l := logger.New(cfg.Config) + + // open database connection + db, _ := database.NewDB(cfg.Config, l) + if err := db.Open(); err != nil { + log.Fatal("could not open db connection") + } + + userRepo := database.NewUserRepo(l, db) + username := flag.Arg(1) if username == "" { flag.Usage() @@ -130,9 +169,9 @@ func readPassword() ([]byte, error) { var err error fd := int(os.Stdin.Fd()) - if terminal.IsTerminal(fd) { + if term.IsTerminal(fd) { fmt.Printf("Password: ") - password, err = terminal.ReadPassword(int(os.Stdin.Fd())) + password, err = term.ReadPassword(int(os.Stdin.Fd())) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index 907825b..b7be319 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 golang.org/x/net v0.2.0 golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 + golang.org/x/term v0.2.0 golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/yaml.v3 v3.0.1 @@ -87,7 +88,6 @@ require ( github.com/subosito/gotenv v1.4.1 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/sys v0.2.0 // indirect - golang.org/x/term v0.2.0 // indirect golang.org/x/text v0.4.0 // indirect golang.org/x/tools v0.1.12 // indirect gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect