From c0f884f049aafa7100c4c0b7670289340bfafd56 Mon Sep 17 00:00:00 2001 From: Anders Pitman Date: Fri, 8 Jan 2021 14:18:51 -0700 Subject: [PATCH] Improve argument parsing and error messages --- client.go | 17 ++++++++++++++--- cmd/boringproxy/main.go | 30 +++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index c1a8471..0601021 100644 --- a/client.go +++ b/client.go @@ -11,6 +11,7 @@ import ( "log" "net" "net/http" + "os" "strings" "sync" "time" @@ -105,19 +106,29 @@ func (c *Client) RunPuppetClient() { url := fmt.Sprintf("https://%s/api/users/%s/clients/%s", c.server, c.user, c.clientName) clientReq, err := http.NewRequest("PUT", url, nil) if err != nil { - log.Fatal("Failed to PUT client") + fmt.Fprintf(os.Stderr, "Failed to create request for URL %s", url) + os.Exit(1) } if len(c.token) > 0 { clientReq.Header.Add("Authorization", "bearer "+c.token) } resp, err := c.httpClient.Do(clientReq) if err != nil { - log.Fatal("Failed to PUT client") + fmt.Fprintf(os.Stderr, "Failed to create client. Ensure the server is running. URL: %s", url) + os.Exit(1) } defer resp.Body.Close() if resp.StatusCode != 200 { - log.Fatal("Failed to PUT client") + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to create client. HTTP Status code: %d. Failed to read body", resp.StatusCode) + os.Exit(1) + } + + msg := string(body) + fmt.Fprintf(os.Stderr, "Failed to create client. Are the user (%s) and token correct? HTTP Status code: %d. Message: %s", c.user, resp.StatusCode, msg) + os.Exit(1) } for { diff --git a/cmd/boringproxy/main.go b/cmd/boringproxy/main.go index 5746857..1119726 100644 --- a/cmd/boringproxy/main.go +++ b/cmd/boringproxy/main.go @@ -17,6 +17,11 @@ Commands: Use "%[1]s command -h" for a list of flags for the command. ` +func fail(msg string) { + fmt.Fprintln(os.Stderr, msg) + os.Exit(1) +} + func main() { if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, os.Args[0]+": Need a command") @@ -32,7 +37,7 @@ func main() { case "server": boringproxy.Listen() case "client": - flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError) + flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError) server := flagSet.String("server", "", "boringproxy server") token := flagSet.String("token", "", "Access token") name := flagSet.String("client-name", "", "Client name") @@ -41,10 +46,22 @@ func main() { acmeEmail := flagSet.String("acme-email", "", "Email for ACME (ie Let's Encrypt)") dnsServer := flagSet.String("dns-server", "", "Custom DNS server") - err := flagSet.Parse(os.Args[2:]) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: parsing flags: %s\n", os.Args[0], err) - } + err := flagSet.Parse(os.Args[2:]) + if err != nil { + fmt.Fprintf(os.Stderr, "%s: parsing flags: %s\n", os.Args[0], err) + } + + if *server == "" { + fail("-server is required") + } + + if *token == "" { + fail("-token is required") + } + + if *name == "" { + fail("-client-name is required") + } config := &boringproxy.ClientConfig{ ServerAddr: *server, @@ -59,7 +76,6 @@ func main() { client := boringproxy.NewClient(config) client.RunPuppetClient() default: - fmt.Fprintln(os.Stderr, os.Args[0]+": Invalid command "+command) - os.Exit(1) + fail(os.Args[0] + ": Invalid command " + command) } }