From a17ac9632def889983fb62469be471aa884884e6 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Tue, 10 Mar 2020 15:38:57 -0700 Subject: [PATCH] Improve error handling around username/hostname If this couldn't read the current user, it would print a generic message, then proceed to dereference the invalid user struct :( Provide somewhat reasonable defaults, and better error messages instead. Test: m nothing Test: run docker with misconfigured user Change-Id: I21af77c7d8d1d2d27cb04546667eb1094c62a7a1 --- ui/build/kati.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ui/build/kati.go b/ui/build/kati.go index a845c5be2..8796a4f05 100644 --- a/ui/build/kati.go +++ b/ui/build/kati.go @@ -104,17 +104,20 @@ func runKati(ctx Context, config Config, extraSuffix string, args []string, envF envFunc(cmd.Environment) if _, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok { - u, err := user.Current() - if err != nil { - ctx.Println("Failed to get current user") + username := "unknown" + if u, err := user.Current(); err == nil { + username = u.Username + } else { + ctx.Println("Failed to get current user:", err) } - cmd.Environment.Set("BUILD_USERNAME", u.Username) + cmd.Environment.Set("BUILD_USERNAME", username) } if _, ok := cmd.Environment.Get("BUILD_HOSTNAME"); !ok { hostname, err := os.Hostname() if err != nil { - ctx.Println("Failed to read hostname") + ctx.Println("Failed to read hostname:", err) + hostname = "unknown" } cmd.Environment.Set("BUILD_HOSTNAME", hostname) }