This commit is contained in:
		| @@ -15,27 +15,34 @@ import ( | ||||
| 	"golang.org/x/term" | ||||
| ) | ||||
|  | ||||
| const defaultTimeout = 10 * time.Second | ||||
|  | ||||
| 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 | ||||
| } | ||||
|  | ||||
| @@ -43,15 +50,19 @@ 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) | ||||
| 		ctx, cancel := context.WithTimeout(c.Context, defaultTimeout) | ||||
| 		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 | ||||
| } | ||||
|  | ||||
| @@ -60,6 +71,7 @@ func ActionLogin(c *cli.Context) error { | ||||
| 	if username == "" { | ||||
| 		return cli.Exit("USERNAME not supplied.", 1) | ||||
| 	} | ||||
|  | ||||
| 	password, err := readPassword() | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error reading password: %w", err) | ||||
| @@ -72,6 +84,7 @@ func ActionLogin(c *cli.Context) error { | ||||
| 		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) | ||||
| @@ -85,7 +98,9 @@ func ActionLogin(c *cli.Context) error { | ||||
| 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) | ||||
| @@ -94,7 +109,8 @@ func ActionUserCreate(c *cli.Context) error { | ||||
| 	clnt := client.Client{ | ||||
| 		BaseURL: c.String("url"), | ||||
| 	} | ||||
| 	ctx, cancel := context.WithTimeout(c.Context, 10*time.Second) | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(c.Context, defaultTimeout) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	if err := clnt.Login(ctx, username, password); err != nil { | ||||
| @@ -103,7 +119,9 @@ func ActionUserCreate(c *cli.Context) error { | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println("User to create:") | ||||
|  | ||||
| 	username = readString("Enter username: ") | ||||
|  | ||||
| 	password, err = readPassword() | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error reading password: %w", err) | ||||
| @@ -121,20 +139,24 @@ func ActionUserCreate(c *cli.Context) error { | ||||
|  | ||||
| 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 "" | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user