Skip to content

Commit 15ba743

Browse files
authored
Merge pull request #9 from thockin/master
Better flag help, error on bad --dest, v2.0.1
2 parents 8bb6ccf + 4523b3a commit 15ba743

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ REGISTRY ?= gcr.io/google_containers
1818
IMAGE = $(REGISTRY)/git-sync-$(ARCH)
1919
LEGACY_AMD64_IMAGE = $(REGISTRY)/git-sync
2020

21-
TAG = v2.0.0
21+
TAG = v2.0.1
2222

2323
# Architectures supported: amd64, arm, arm64 and ppc64le
2424
ARCH ?= amd64

main.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,42 @@ import (
3737
"github.com/thockin/logr"
3838
)
3939

40-
var flRepo = flag.String("repo", envString("GIT_SYNC_REPO", ""), "git repo url")
41-
var flBranch = flag.String("branch", envString("GIT_SYNC_BRANCH", "master"), "git branch")
42-
var flRev = flag.String("rev", envString("GIT_SYNC_REV", "HEAD"), "git rev")
40+
var flRepo = flag.String("repo", envString("GIT_SYNC_REPO", ""),
41+
"the git repository to clone")
42+
var flBranch = flag.String("branch", envString("GIT_SYNC_BRANCH", "master"),
43+
"the git branch to check out")
44+
var flRev = flag.String("rev", envString("GIT_SYNC_REV", "HEAD"),
45+
"the git revision (tag or hash) to check out")
4346
var flDepth = flag.Int("depth", envInt("GIT_SYNC_DEPTH", 0),
44-
"shallow clone with a history truncated to the specified number of commits")
47+
"use a shallow clone with a history truncated to the specified number of commits")
4548

4649
var flRoot = flag.String("root", envString("GIT_SYNC_ROOT", "/git"),
47-
"root directory for git operations")
50+
"the root directory for git operations")
4851
var flDest = flag.String("dest", envString("GIT_SYNC_DEST", ""),
49-
"path at which to publish the checked-out files (a subdirectory under --root, defaults to leaf dir of --root)")
52+
"the name at which to publish the checked-out files under --root (defaults to leaf dir of --root)")
5053
var flWait = flag.Int("wait", envInt("GIT_SYNC_WAIT", 0),
51-
"number of seconds between syncs")
54+
"the number of seconds between syncs")
5255
var flOneTime = flag.Bool("one-time", envBool("GIT_SYNC_ONE_TIME", false),
5356
"exit after the initial checkout")
5457
var flMaxSyncFailures = flag.Int("max-sync-failures", envInt("GIT_SYNC_MAX_SYNC_FAILURES", 0),
55-
"number of consecutive failures allowed before aborting (the first pull must succeed)")
58+
"the number of consecutive failures allowed before aborting (the first pull must succeed)")
5659
var flChmod = flag.Int("change-permissions", envInt("GIT_SYNC_PERMISSIONS", 0),
57-
"change the permissions of the checked-out files to this")
60+
"the file permissions to apply to the checked-out files")
5861

59-
var flUsername = flag.String("username", envString("GIT_SYNC_USERNAME", ""), "username")
60-
var flPassword = flag.String("password", envString("GIT_SYNC_PASSWORD", ""), "password")
62+
var flUsername = flag.String("username", envString("GIT_SYNC_USERNAME", ""),
63+
"the username to use")
64+
var flPassword = flag.String("password", envString("GIT_SYNC_PASSWORD", ""),
65+
"the password to use")
6166

62-
var flSSH = flag.Bool("ssh", envBool("GIT_SYNC_SSH", false), "use SSH protocol")
67+
var flSSH = flag.Bool("ssh", envBool("GIT_SYNC_SSH", false),
68+
"use SSH for git operations")
6369

6470
var log = newLoggerOrDie()
6571

6672
func newLoggerOrDie() logr.Logger {
6773
g, err := glogr.New()
6874
if err != nil {
69-
fmt.Fprintf(os.Stderr, "failind to initialize logging: %v", err)
75+
fmt.Fprintf(os.Stderr, "failind to initialize logging: %v\n", err)
7076
os.Exit(1)
7177
}
7278
return g
@@ -108,32 +114,40 @@ func main() {
108114

109115
flag.Parse()
110116
if *flRepo == "" {
117+
fmt.Fprintf(os.Stderr, "ERROR: --repo or $GIT_SYNC_REPO must be provided\n")
111118
flag.Usage()
112119
os.Exit(1)
113120
}
114121
if *flDest == "" {
115122
parts := strings.Split(strings.Trim(*flRepo, "/"), "/")
116123
*flDest = parts[len(parts)-1]
117124
}
125+
if strings.Contains(*flDest, "/") {
126+
fmt.Fprintf(os.Stderr, "ERROR: --dest must be a bare name\n")
127+
flag.Usage()
128+
os.Exit(1)
129+
}
118130
if _, err := exec.LookPath("git"); err != nil {
119-
log.Errorf("required git executable not found: %v", err)
131+
fmt.Fprintf(os.Stderr, "ERROR: git executable not found: %v\n", err)
120132
os.Exit(1)
121133
}
122134

123135
if *flUsername != "" && *flPassword != "" {
124136
if err := setupGitAuth(*flUsername, *flPassword, *flRepo); err != nil {
125-
log.Errorf("error creating .netrc file: %v", err)
137+
fmt.Fprintf(os.Stderr, "ERROR: can't create .netrc file: %v\n", err)
126138
os.Exit(1)
127139
}
128140
}
129141

130142
if *flSSH {
131143
if err := setupGitSSH(); err != nil {
132-
log.Errorf("error configuring SSH: %v", err)
144+
fmt.Fprintf(os.Stderr, "ERROR: can't configure SSH: %v\n", err)
133145
os.Exit(1)
134146
}
135147
}
136148

149+
// From here on, output goes through logging.
150+
137151
initialSync := true
138152
failCount := 0
139153
for {
@@ -173,7 +187,7 @@ func setFlagDefaults() {
173187
// Force logging to stderr.
174188
stderrFlag := flag.Lookup("logtostderr")
175189
if stderrFlag == nil {
176-
fmt.Fprintf(os.Stderr, "can't find flag 'logtostderr'")
190+
fmt.Fprintf(os.Stderr, "can't find flag 'logtostderr'\n")
177191
os.Exit(1)
178192
}
179193
stderrFlag.Value.Set("true")

0 commit comments

Comments
 (0)