Skip to content

Commit e76cf56

Browse files
committed
Clarify logging vs printf for fatal messages
Also log when HTTP serving fails.
1 parent 581674f commit e76cf56

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

cmd/git-sync/main.go

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ func main() {
264264
os.Exit(127)
265265
}
266266

267+
//
268+
// Parse and verify flags. Errors here are fatal.
269+
//
270+
267271
pflag.Parse()
268272
flag.CommandLine.Parse(nil) // Otherwise glog complains
269273
setGlogFlags()
@@ -362,11 +366,6 @@ func main() {
362366
}
363367
}
364368

365-
if _, err := exec.LookPath(*flGitCmd); err != nil {
366-
fmt.Fprintf(os.Stderr, "ERROR: git executable %q not found: %v\n", *flGitCmd, err)
367-
os.Exit(1)
368-
}
369-
370369
if *flSSH {
371370
if *flUsername != "" {
372371
fmt.Fprintf(os.Stderr, "ERROR: only one of --ssh and --username may be specified\n")
@@ -398,19 +397,30 @@ func main() {
398397
}
399398
}
400399

400+
// From here on, output goes through logging.
401+
log.V(0).Info("starting up", "pid", os.Getpid(), "args", os.Args)
402+
403+
if _, err := exec.LookPath(*flGitCmd); err != nil {
404+
log.Error(err, "ERROR: git executable not found", "git", *flGitCmd)
405+
os.Exit(1)
406+
}
407+
401408
if err := os.MkdirAll(*flRoot, 0700); err != nil {
402-
fmt.Fprintf(os.Stderr, "ERROR: can't make root dir: %v", err)
409+
log.Error(err, "ERROR: can't make root dir", "path", *flRoot)
403410
os.Exit(1)
404411
}
405412
absRoot, err := normalizePath(*flRoot)
406413
if err != nil {
407-
fmt.Fprintf(os.Stderr, "ERROR: can't normalize root path: %v", err)
414+
log.Error(err, "ERROR: can't normalize root path", "path", *flRoot)
408415
os.Exit(1)
409416
}
417+
if absRoot != *flRoot {
418+
log.V(0).Info("normalized root path", "path", *flRoot, "result", absRoot)
419+
}
410420

411421
if *flAddUser {
412422
if err := addUser(); err != nil {
413-
fmt.Fprintf(os.Stderr, "ERROR: can't write to /etc/passwd: %v\n", err)
423+
log.Error(err, "ERROR: can't add user")
414424
os.Exit(1)
415425
}
416426
}
@@ -421,29 +431,29 @@ func main() {
421431

422432
if *flUsername != "" && *flPassword != "" {
423433
if err := setupGitAuth(ctx, *flUsername, *flPassword, *flRepo); err != nil {
424-
fmt.Fprintf(os.Stderr, "ERROR: can't create .netrc file: %v\n", err)
434+
log.Error(err, "ERROR: can't set up git auth")
425435
os.Exit(1)
426436
}
427437
}
428438

429439
if *flSSH {
430440
if err := setupGitSSH(*flSSHKnownHosts); err != nil {
431-
fmt.Fprintf(os.Stderr, "ERROR: can't configure SSH: %v\n", err)
441+
log.Error(err, "ERROR: can't set up git SSH")
432442
os.Exit(1)
433443
}
434444
}
435445

436446
if *flCookieFile {
437447
if err := setupGitCookieFile(ctx); err != nil {
438-
fmt.Fprintf(os.Stderr, "ERROR: can't set git cookie file: %v\n", err)
448+
log.Error(err, "ERROR: can't set up git cookie file")
439449
os.Exit(1)
440450
}
441451
}
442452

443453
if *flAskPassURL != "" {
444454
if err := callGitAskPassURL(ctx, *flAskPassURL); err != nil {
445455
askpassCount.WithLabelValues(metricKeyError).Inc()
446-
fmt.Fprintf(os.Stderr, "ERROR: failed to call ASKPASS callback URL: %v\n", err)
456+
log.Error(err, "ERROR: failed to call ASKPASS callback URL", "url", *flAskPassURL)
447457
os.Exit(1)
448458
}
449459
askpassCount.WithLabelValues(metricKeySuccess).Inc()
@@ -455,38 +465,38 @@ func main() {
455465
if *flHTTPBind != "" {
456466
ln, err := net.Listen("tcp", *flHTTPBind)
457467
if err != nil {
458-
fmt.Fprintf(os.Stderr, "ERROR: unable to bind HTTP endpoint: %v\n", err)
468+
log.Error(err, "ERROR: failed to bind HTTP endpoint", "endpoint", *flHTTPBind)
459469
os.Exit(1)
460470
}
461471
mux := http.NewServeMux()
462-
go func() {
463-
if *flHTTPMetrics {
464-
mux.Handle("/metrics", promhttp.Handler())
465-
}
472+
if *flHTTPMetrics {
473+
mux.Handle("/metrics", promhttp.Handler())
474+
}
466475

467-
if *flHTTPprof {
468-
mux.HandleFunc("/debug/pprof/", pprof.Index)
469-
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
470-
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
471-
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
472-
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
473-
}
476+
if *flHTTPprof {
477+
mux.HandleFunc("/debug/pprof/", pprof.Index)
478+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
479+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
480+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
481+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
482+
}
474483

475-
// This is a dumb liveliness check endpoint. Currently this checks
476-
// nothing and will always return 200 if the process is live.
477-
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
478-
if !getRepoReady() {
479-
http.Error(w, "repo is not ready", http.StatusServiceUnavailable)
480-
}
481-
// Otherwise success
482-
})
483-
http.Serve(ln, mux)
484+
// This is a dumb liveliness check endpoint. Currently this checks
485+
// nothing and will always return 200 if the process is live.
486+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
487+
if !getRepoReady() {
488+
http.Error(w, "repo is not ready", http.StatusServiceUnavailable)
489+
}
490+
// Otherwise success
491+
})
492+
log.V(0).Info("serving HTTP", "endpoint", *flHTTPBind)
493+
go func() {
494+
err := http.Serve(ln, mux)
495+
log.Error(err, "HTTP server terminated")
496+
os.Exit(1)
484497
}()
485498
}
486499

487-
// From here on, output goes through logging.
488-
log.V(0).Info("starting up", "pid", os.Getpid(), "args", os.Args)
489-
490500
// Startup webhooks goroutine
491501
var webhook *Webhook
492502
if *flWebhookURL != "" {

0 commit comments

Comments
 (0)