feat(autobrrctl): Show latest version in version output (#723)

* initial commit

* initial commit

* fix

* removed unused code

* go mod tidy
This commit is contained in:
soup 2023-02-23 23:08:59 +01:00 committed by GitHub
parent 2d36a12da8
commit a6496fc946
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 32 deletions

View file

@ -3,9 +3,11 @@ package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/autobrr/autobrr/internal/config"
@ -15,7 +17,7 @@ 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"
)
@ -23,27 +25,49 @@ const usage = `usage: autobrrctl --config path <action>
create-user <username> Create user
change-password <username> Change password for user
version Print version info
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()
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")
}
@ -62,10 +86,6 @@ func main() {
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)
case "create-user":
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
}

2
go.mod
View file

@ -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