package actions import ( "bufio" "context" "fmt" "os" "strings" "syscall" "time" "git.t-juice.club/torjus/gpaste/client" "git.t-juice.club/torjus/gpaste/files" "github.com/urfave/cli/v2" "golang.org/x/term" ) func ActionUpload(c *cli.Context) error { clnt := client.Client{ BaseURL: c.String("url"), } for _, arg := range c.Args().Slice() { f, err := os.Open(arg) if err != nil { return err } defer f.Close() file := &files.File{ OriginalFilename: arg, Body: f, } resp, err := clnt.Upload(c.Context, file) if err != nil { errmsg := fmt.Sprintf("Error uploading file: %s", err) return cli.Exit(errmsg, 1) } fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp.Files[0].URL) } return nil } 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 } 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) } clnt := client.Client{ BaseURL: c.String("url"), } if err := clnt.Login(c.Context, username, password); err != nil { errmsg := fmt.Sprintf("Error logging in: %s", err) return cli.Exit(errmsg, 1) } if err := clnt.WriteConfig(); err != nil { errMsg := fmt.Sprintf("Failed to write config: %s", err) return cli.Exit(errMsg, 1) } // TODO: Store this somewhere, so we don't need to log in each time fmt.Println("Successfully logged in.") return nil } func ActionUserCreate(c *cli.Context) error { // TODO: Needs to supply auth token to actually work fmt.Println("Need to be logged in to create user") username := readString("Enter username: ") password, err := readPassword() if err != nil { return fmt.Errorf("error reading password: %w", err) } clnt := client.Client{ BaseURL: c.String("url"), } ctx, cancel := context.WithTimeout(c.Context, 10*time.Second) defer cancel() if err := clnt.Login(ctx, username, password); err != nil { errmsg := fmt.Sprintf("Error logging in: %s", err) return cli.Exit(errmsg, 1) } fmt.Println("User to create:") username = readString("Enter username: ") password, err = readPassword() if err != nil { return fmt.Errorf("error reading password: %w", err) } if err := clnt.UserCreate(ctx, username, password); err != nil { errmsg := fmt.Sprintf("Error creating user: %s", err) return cli.Exit(errmsg, 1) } 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 } func readString(prompt string) string { fmt.Print(prompt) scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { return scanner.Text() } return "" }