mirror of
https://github.com/boringproxy/boringproxy.git
synced 2025-02-25 18:55:29 -06:00
Improve argument parsing and error messages
This commit is contained in:
parent
72fe16abe6
commit
c0f884f049
17
client.go
17
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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user