From b771c1307cd1fe1e9758afb367a40cdab9919385 Mon Sep 17 00:00:00 2001 From: webb Date: Fri, 4 Sep 2020 10:38:48 -0400 Subject: [PATCH] Move to library for CLI parsing, add ability to get upload key --- account.go | 26 ++++++++++++++++++++++++++ cfg.go | 32 +++++++++++++++++++++++++------- main.go | 47 +++++++++++++++++++++++++++++++++-------------- upload.go | 12 +++++------- 4 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 account.go diff --git a/account.go b/account.go new file mode 100644 index 0000000..c1a3a4b --- /dev/null +++ b/account.go @@ -0,0 +1,26 @@ +package main + +import ( + "errors" + "fmt" + "gitea.com/webb/teal" +) + +func getAccountHandler(s string) (error){ + username, password, err := getLoginDetails() + if err != nil { + return err + } + + c, err := teal.Login(username, password) + if err != nil { + return err + } + + switch s { + case "key": + fmt.Println(c.User.UploadKey) + return nil + } + return errors.New("internal: no fetchable attribute found") +} diff --git a/cfg.go b/cfg.go index b48bfe3..220050f 100644 --- a/cfg.go +++ b/cfg.go @@ -6,15 +6,33 @@ import ( ) func getUploadKey() (string, error) { - readKey := os.Getenv("PXL_KEY") - if readKey != "" { - return readKey, nil + rdKey := os.Getenv("PXL_KEY") + if rdKey != "" { + return rdKey, nil } - readKey = os.Getenv("MIRAGE_KEY") - if readKey != "" { - return readKey, nil + rdKey = os.Getenv("MIRAGE_KEY") + if rdKey != "" { + return rdKey, nil } - return "", errors.New("upload key not set as environment variable") + return "", errors.New("upload key not set") +} + +func getLoginDetails() (string, string, error) { + rdUsr, rdPsswd := os.Getenv("PXL_USERNAME"), os.Getenv("PXL_PASSWORD") + if rdUsr != "" && rdPsswd != "" { + return rdUsr, rdPsswd, nil + } + + if rdUsr == "" && rdPsswd == "" { + return "", "", errors.New("user credentials not set") + } + + if rdUsr == "" { + return "", "", errors.New("username not set") + } + + return "", "", errors.New("password not set") + } diff --git a/main.go b/main.go index 1b6ef97..6356841 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,9 @@ package main import ( - "fmt" "gitea.com/webb/teal" + "github.com/urfave/cli" + "log" "os" ) @@ -11,19 +12,37 @@ var itCl = teal.Client{ } func main() { - switch os.Args[1] { - case "upload": - if len(os.Args[2:]) > 1 { - fmt.Println("interrupt: too many arguments!") - os.Exit(1) - } else if len(os.Args[2:]) == 0 { - fmt.Println("interrupt: Please supply a file to upload.") - os.Exit(1) - } - - uploadHandler(os.Args[2]) + cli := &cli.App{ + Commands: []*cli.Command{ + { + Name: "upload", + Aliases: []string{"u"}, + Usage: "upload a file", + Action: func(c *cli.Context) error { + uploadHandler(c.Args().First()) + return nil + }, + }, + { + Name: "get", + Aliases: []string{"g"}, + Usage: "get information from your account", + Subcommands: []*cli.Command{ + { + Name: "key", + Usage: "get your upload key by logging in with your username and password", + Action: func(c *cli.Context) error { + err := getAccountHandler("key") + return err + }, + }, + }, + }, + }, } - fmt.Println("interrupt: Missing subcommand.") - os.Exit(1) + err := cli.Run(os.Args) + if err != nil { + log.Fatal(err) + } } diff --git a/upload.go b/upload.go index d91ae0a..3922d67 100644 --- a/upload.go +++ b/upload.go @@ -6,7 +6,7 @@ import ( "os" ) -func uploadHandler(a string) { +func uploadHandler(a string) error { uploadKey, err := getUploadKey() if err != nil { fmt.Println(err) @@ -15,13 +15,12 @@ func uploadHandler(a string) { var reader io.Reader - if os.Args[2] == "-" { + if a == "-" { reader = os.Stdin } else { reader, err = os.Open(a) if err != nil { - fmt.Println(err) - os.Exit(1) + return err } } @@ -29,10 +28,9 @@ func uploadHandler(a string) { r, err := itCl.UploadFile(reader, a) if err != nil { - fmt.Println(err) - os.Exit(1) + return err } fmt.Println(r) - os.Exit(0) + return nil }