Move to library for CLI parsing, add ability to get upload key

This commit is contained in:
webb 2020-09-04 10:38:48 -04:00
parent 0521af1ae4
commit b771c1307c
No known key found for this signature in database
GPG Key ID: 8B6F0D2784D22DB2
4 changed files with 89 additions and 28 deletions

26
account.go Normal file
View File

@ -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")
}

32
cfg.go
View File

@ -6,15 +6,33 @@ import (
) )
func getUploadKey() (string, error) { func getUploadKey() (string, error) {
readKey := os.Getenv("PXL_KEY") rdKey := os.Getenv("PXL_KEY")
if readKey != "" { if rdKey != "" {
return readKey, nil return rdKey, nil
} }
readKey = os.Getenv("MIRAGE_KEY") rdKey = os.Getenv("MIRAGE_KEY")
if readKey != "" { if rdKey != "" {
return readKey, nil 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")
} }

47
main.go
View File

@ -1,8 +1,9 @@
package main package main
import ( import (
"fmt"
"gitea.com/webb/teal" "gitea.com/webb/teal"
"github.com/urfave/cli"
"log"
"os" "os"
) )
@ -11,19 +12,37 @@ var itCl = teal.Client{
} }
func main() { func main() {
switch os.Args[1] { cli := &cli.App{
case "upload": Commands: []*cli.Command{
if len(os.Args[2:]) > 1 { {
fmt.Println("interrupt: too many arguments!") Name: "upload",
os.Exit(1) Aliases: []string{"u"},
} else if len(os.Args[2:]) == 0 { Usage: "upload a file",
fmt.Println("interrupt: Please supply a file to upload.") Action: func(c *cli.Context) error {
os.Exit(1) uploadHandler(c.Args().First())
} return nil
},
uploadHandler(os.Args[2]) },
{
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.") err := cli.Run(os.Args)
os.Exit(1) if err != nil {
log.Fatal(err)
}
} }

View File

@ -6,7 +6,7 @@ import (
"os" "os"
) )
func uploadHandler(a string) { func uploadHandler(a string) error {
uploadKey, err := getUploadKey() uploadKey, err := getUploadKey()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -15,13 +15,12 @@ func uploadHandler(a string) {
var reader io.Reader var reader io.Reader
if os.Args[2] == "-" { if a == "-" {
reader = os.Stdin reader = os.Stdin
} else { } else {
reader, err = os.Open(a) reader, err = os.Open(a)
if err != nil { if err != nil {
fmt.Println(err) return err
os.Exit(1)
} }
} }
@ -29,10 +28,9 @@ func uploadHandler(a string) {
r, err := itCl.UploadFile(reader, a) r, err := itCl.UploadFile(reader, a)
if err != nil { if err != nil {
fmt.Println(err) return err
os.Exit(1)
} }
fmt.Println(r) fmt.Println(r)
os.Exit(0) return nil
} }