mirror of https://gitea.com/webb/interrupt
Move to library for CLI parsing, add ability to get upload key
This commit is contained in:
parent
0521af1ae4
commit
b771c1307c
|
@ -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
32
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")
|
||||
|
||||
}
|
||||
|
|
47
main.go
47
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)
|
||||
}
|
||||
}
|
||||
|
|
12
upload.go
12
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue