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 ""
|
||||
}
|
||||
|
@@ -64,5 +64,5 @@ func main() {
|
||||
},
|
||||
}
|
||||
|
||||
app.Run(os.Args)
|
||||
_ = app.Run(os.Args)
|
||||
}
|
||||
|
@@ -22,8 +22,11 @@ func ActionServe(c *cli.Context) error {
|
||||
configPath = c.String("config")
|
||||
}
|
||||
|
||||
var cfg *gpaste.ServerConfig
|
||||
var r io.ReadCloser
|
||||
var (
|
||||
cfg *gpaste.ServerConfig
|
||||
r io.ReadCloser
|
||||
)
|
||||
|
||||
r, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
cfg = &gpaste.ServerConfig{
|
||||
@@ -52,8 +55,10 @@ func ActionServe(c *cli.Context) error {
|
||||
// Setup contexts for clean shutdown
|
||||
rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer rootCancel()
|
||||
|
||||
httpCtx, httpCancel := context.WithCancel(rootCtx)
|
||||
defer httpCancel()
|
||||
|
||||
httpShutdownCtx, httpShutdownCancel := context.WithCancel(context.Background())
|
||||
defer httpShutdownCancel()
|
||||
|
||||
@@ -66,14 +71,18 @@ func ActionServe(c *cli.Context) error {
|
||||
// Wait for cancel
|
||||
go func() {
|
||||
<-httpCtx.Done()
|
||||
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
|
||||
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) // nolint: gomnd
|
||||
defer cancel()
|
||||
srv.Shutdown(timeoutCtx)
|
||||
|
||||
_ = srv.Shutdown(timeoutCtx)
|
||||
}()
|
||||
serverLogger.Infow("Starting HTTP server.", "addr", cfg.ListenAddr)
|
||||
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
serverLogger.Errorw("Error during shutdown.", "error", err)
|
||||
}
|
||||
|
||||
serverLogger.Infow("HTTP server shutdown complete.", "addr", cfg.ListenAddr)
|
||||
httpShutdownCancel()
|
||||
}()
|
||||
|
@@ -29,5 +29,5 @@ func main() {
|
||||
Action: actions.ActionServe,
|
||||
}
|
||||
|
||||
app.Run(os.Args)
|
||||
_ = app.Run(os.Args)
|
||||
}
|
||||
|
Reference in New Issue
Block a user