diff --git a/main.go b/main.go index 0f53e1b1..757ceeae 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "bufio" + "bytes" "flag" "fmt" "io/ioutil" @@ -269,6 +271,8 @@ var clientHelp = ` --hostname, Optionally set the 'Host' header (defaults to the host found in the server url). + + --remotes, a file to read remotes from (one per line). ` + commonHelp func client(args []string, envPrefix string) { @@ -284,6 +288,7 @@ func client(args []string, envPrefix string) { pid := flags.Bool("pid", false, "") hostname := flags.String("hostname", "", "") verbose := flags.Bool("v", false, "") + flagRemotes := flags.String("remotes", "", "") flags.Usage = func() { fmt.Print(clientHelp) os.Exit(1) @@ -292,12 +297,42 @@ func client(args []string, envPrefix string) { flags.Parse(args) //pull out options, put back remaining args args = flags.Args() - if len(args) < 2 { - log.Fatalf("A server and least one remote is required") + switch len(args) { + case 0: + log.Fatalf("A server is required") + case 1: + if *flagRemotes == "" { + log.Fatalf("A server and least one remote is required") + } } if *auth == "" { *auth = os.Getenv("AUTH") } + remotes := args[1:] + var remotesFn string + if *flagRemotes != "" { + remotesFn = *flagRemotes + } else if len(remotes) == 1 && (remotes[0] == "-" || remotes[0] == "") { + remotes = remotes[:0] + remotesFn = "-" + } + if remotesFn != "" { + fh := os.Stdin + if remotesFn != "-" { + var err error + if fh, err = os.Open(remotesFn); err != nil { + log.Fatal(err) + } + } + scanner := bufio.NewScanner(fh) + for scanner.Scan() { + line := bytes.TrimSpace(scanner.Bytes()) + if len(line) != 0 { + remotes = append(remotes, string(line)) + } + } + fh.Close() + } c, err := chclient.NewClient(&chclient.Config{ Fingerprint: *fingerprint, Auth: *auth, @@ -306,7 +341,7 @@ func client(args []string, envPrefix string) { MaxRetryInterval: *maxRetryInterval, HTTPProxy: *proxy, Server: args[0], - Remotes: args[1:], + Remotes: remotes, HostHeader: *hostname, }) if err != nil {