Skip to content

Commit

Permalink
Ensure user always sees fatal errors at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Nov 1, 2016
1 parent 53e1178 commit 49cb225
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions caddy/caddymain/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Run() {
if revoke != "" {
err := caddytls.Revoke(revoke)
if err != nil {
log.Fatal(err)
mustLogFatalf(err.Error())
}
fmt.Printf("Revoked certificate for %s\n", revoke)
os.Exit(0)
Expand All @@ -97,36 +97,36 @@ func Run() {
// Set CPU cap
err := setCPU(cpu)
if err != nil {
mustLogFatal(err)
mustLogFatalf(err.Error())
}

// Get Caddyfile input
caddyfile, err := caddy.LoadCaddyfile(serverType)
if err != nil {
mustLogFatal(err)
mustLogFatalf(err.Error())
}

// Start your engines
instance, err := caddy.Start(caddyfile)
if err != nil {
mustLogFatal(err)
mustLogFatalf(err.Error())
}

// Twiddle your thumbs
instance.Wait()
}

// mustLogFatal wraps log.Fatal() in a way that ensures the
// mustLogFatalf wraps log.Fatalf() in a way that ensures the
// output is always printed to stderr so the user can see it
// if the user is still there, even if the process log was not
// enabled. If this process is an upgrade, however, and the user
// might not be there anymore, this just logs to the process
// log and exits.
func mustLogFatal(args ...interface{}) {
func mustLogFatalf(format string, args ...interface{}) {
if !caddy.IsUpgrade() {
log.SetOutput(os.Stderr)
}
log.Fatal(args...)
log.Fatalf(format, args...)
}

// confLoader loads the Caddyfile using the -conf flag.
Expand Down Expand Up @@ -178,16 +178,16 @@ func moveStorage() {
// Just use a default config to get default (file) storage
fileStorage, err := new(caddytls.Config).StorageFor(caddytls.DefaultCAUrl)
if err != nil {
log.Fatalf("[ERROR] Unable to get new path for certificate storage: %v", err)
mustLogFatalf("[ERROR] Unable to get new path for certificate storage: %v", err)
}
newPath := fileStorage.(*caddytls.FileStorage).Path
err = os.MkdirAll(string(newPath), 0700)
if err != nil {
log.Fatalf("[ERROR] Unable to make new certificate storage path: %v\n\nPlease follow instructions at:\nhttps://github.com/mholt/caddy/issues/902#issuecomment-228876011", err)
mustLogFatalf("[ERROR] Unable to make new certificate storage path: %v\n\nPlease follow instructions at:\nhttps://github.com/mholt/caddy/issues/902#issuecomment-228876011", err)
}
err = os.Rename(oldPath, string(newPath))
if err != nil {
log.Fatalf("[ERROR] Unable to migrate certificate storage: %v\n\nPlease follow instructions at:\nhttps://github.com/mholt/caddy/issues/902#issuecomment-228876011", err)
mustLogFatalf("[ERROR] Unable to migrate certificate storage: %v\n\nPlease follow instructions at:\nhttps://github.com/mholt/caddy/issues/902#issuecomment-228876011", err)
}
// convert mixed case folder and file names to lowercase
var done bool // walking is recursive and preloads the file names, so we must restart walk after a change until no changes
Expand All @@ -200,7 +200,7 @@ func moveStorage() {
lowerPath := filepath.Join(filepath.Dir(path), lowerBase)
err = os.Rename(path, lowerPath)
if err != nil {
log.Fatalf("[ERROR] Unable to lower-case: %v\n\nPlease follow instructions at:\nhttps://github.com/mholt/caddy/issues/902#issuecomment-228876011", err)
mustLogFatalf("[ERROR] Unable to lower-case: %v\n\nPlease follow instructions at:\nhttps://github.com/mholt/caddy/issues/902#issuecomment-228876011", err)
}
// terminate traversal and restart since Walk needs the updated file list with new file names
done = false
Expand Down

0 comments on commit 49cb225

Please sign in to comment.