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) {
|
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
47
main.go
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
12
upload.go
12
upload.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue