Skip to content

Commit

Permalink
client: --remotes flag for reading the list of remotes from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
tgulacsi committed Jul 18, 2020
1 parent b96e3f9 commit 23ab1c5
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -324,6 +326,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) {
Expand All @@ -339,6 +343,7 @@ func client(args []string, envPrefix string) {
hostname := flags.String("hostname", "", "")
pid := flags.Bool("pid", false, "")
verbose := flags.Bool("v", false, "")
flagRemotes := flags.String("remotes", "", "")
flags.Usage = func() {
fmt.Print(clientHelp)
os.Exit(1)
Expand All @@ -347,11 +352,20 @@ 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")
}
}
config.Server = args[0]
config.Remotes = args[1:]
//read remotes
var err error
if config.Remotes, err = readRemotes(args[1:], *flagRemotes); err != nil {
log.Fatal(err)
}
//default auth
if config.Auth == "" {
config.Auth = os.Getenv("AUTH")
Expand Down Expand Up @@ -394,3 +408,36 @@ func flagsFromEnv(fs *flag.FlagSet, prefix string) {
f.Value.Set(os.Getenv(prefix + strings.ToUpper(envRepl.Replace(k))))
}
}

func readRemotes(remotes []string, fl string) ([]string, error) {
var remotesFn string
if fl != "" {
remotesFn = fl
} else if len(remotes) == 1 && (remotes[0] == "-" || remotes[0] == "") {
remotes = remotes[:0]
remotesFn = "-"
}
if remotesFn == "" {
return remotes, nil
}
fh := os.Stdin
if remotesFn != "-" {
var err error
if fh, err = os.Open(remotesFn); err != nil {
return remotes, nil
}
}
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line := scanner.Bytes()
if i := bytes.IndexByte(line, '#'); i >= 0 {
line = line[:i]
}
line = bytes.TrimSpace(line)
if len(line) != 0 {
remotes = append(remotes, string(line))
}
}
fh.Close()
return remotes, nil
}

0 comments on commit 23ab1c5

Please sign in to comment.