gpaste/cmd/client/actions/actions.go

141 lines
3.2 KiB
Go
Raw Normal View History

2022-01-20 02:47:58 +00:00
package actions
import (
2022-01-20 22:31:09 +00:00
"bufio"
2022-01-20 02:47:58 +00:00
"context"
"fmt"
"os"
"strings"
"syscall"
"time"
2022-01-20 22:17:09 +00:00
"git.t-juice.club/torjus/gpaste/client"
"git.t-juice.club/torjus/gpaste/files"
2022-01-20 02:47:58 +00:00
"github.com/urfave/cli/v2"
"golang.org/x/term"
)
func ActionUpload(c *cli.Context) error {
2022-01-20 22:17:09 +00:00
clnt := client.Client{
BaseURL: c.String("url"),
}
2022-01-20 02:47:58 +00:00
for _, arg := range c.Args().Slice() {
f, err := os.Open(arg)
if err != nil {
return err
}
defer f.Close()
2022-01-20 22:17:09 +00:00
file := &files.File{
OriginalFilename: arg,
Body: f,
2022-01-20 02:47:58 +00:00
}
2022-01-20 22:17:09 +00:00
resp, err := clnt.Upload(c.Context, file)
if err != nil {
errmsg := fmt.Sprintf("Error uploading file: %s", err)
return cli.Exit(errmsg, 1)
2022-01-20 02:47:58 +00:00
}
2022-01-24 18:15:43 +00:00
fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp.Files[0].URL)
2022-01-20 02:47:58 +00:00
}
return nil
}
2022-01-21 06:17:52 +00:00
func ActionDelete(c *cli.Context) error {
clnt := client.Client{
BaseURL: c.String("url"),
}
for _, arg := range c.Args().Slice() {
ctx, cancel := context.WithTimeout(c.Context, 5*time.Second)
defer cancel()
if err := clnt.Delete(ctx, arg); err != nil {
fmt.Printf("Error deleting file %s\n", arg)
fmt.Printf("%s\n", err)
}
fmt.Printf("Deleted %s\n", arg)
}
return nil
}
2022-01-20 02:47:58 +00:00
func ActionLogin(c *cli.Context) error {
username := c.Args().First()
if username == "" {
return cli.Exit("USERNAME not supplied.", 1)
}
password, err := readPassword()
if err != nil {
return fmt.Errorf("error reading password: %w", err)
}
2022-01-20 22:20:01 +00:00
clnt := client.Client{
BaseURL: c.String("url"),
2022-01-20 02:47:58 +00:00
}
2022-01-20 22:20:01 +00:00
if err := clnt.Login(c.Context, username, password); err != nil {
errmsg := fmt.Sprintf("Error logging in: %s", err)
return cli.Exit(errmsg, 1)
2022-01-20 02:47:58 +00:00
}
2022-01-21 01:40:33 +00:00
if err := clnt.WriteConfig(); err != nil {
errMsg := fmt.Sprintf("Failed to write config: %s", err)
return cli.Exit(errMsg, 1)
}
2022-01-20 22:20:01 +00:00
// TODO: Store this somewhere, so we don't need to log in each time
fmt.Println("Successfully logged in.")
2022-01-20 02:47:58 +00:00
return nil
}
func ActionUserCreate(c *cli.Context) error {
// TODO: Needs to supply auth token to actually work
2022-01-20 22:31:09 +00:00
fmt.Println("Need to be logged in to create user")
username := readString("Enter username: ")
2022-01-20 02:47:58 +00:00
password, err := readPassword()
if err != nil {
return fmt.Errorf("error reading password: %w", err)
}
2022-01-20 22:31:09 +00:00
clnt := client.Client{
BaseURL: c.String("url"),
}
2022-01-20 02:47:58 +00:00
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
defer cancel()
2022-01-20 22:31:09 +00:00
if err := clnt.Login(ctx, username, password); err != nil {
errmsg := fmt.Sprintf("Error logging in: %s", err)
return cli.Exit(errmsg, 1)
2022-01-20 02:47:58 +00:00
}
2022-01-20 22:31:09 +00:00
fmt.Println("User to create:")
username = readString("Enter username: ")
password, err = readPassword()
2022-01-20 02:47:58 +00:00
if err != nil {
2022-01-20 22:31:09 +00:00
return fmt.Errorf("error reading password: %w", err)
2022-01-20 02:47:58 +00:00
}
2022-01-20 22:31:09 +00:00
if err := clnt.UserCreate(ctx, username, password); err != nil {
errmsg := fmt.Sprintf("Error creating user: %s", err)
return cli.Exit(errmsg, 1)
2022-01-20 02:47:58 +00:00
}
fmt.Printf("Created user %s\n", username)
return nil
}
func readPassword() (string, error) {
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
password := string(bytePassword)
return strings.TrimSpace(password), nil
}
2022-01-20 22:31:09 +00:00
func readString(prompt string) string {
fmt.Print(prompt)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
return scanner.Text()
}
return ""
}