From f8a823cf7e59dcf4fd85d66564eb1ba79b2115d5 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Thu, 22 May 2014 14:02:10 -0700 Subject: [PATCH] refactor(userdata): Move userdata processing into a function --- coreos-cloudinit.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index a86630e..6b44342 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -75,24 +75,26 @@ func main() { } } - if len(userdataBytes) == 0 { - log.Printf("No user data to handle, exiting.") - os.Exit(0) - } - env := initialize.NewEnvironment("/", workspace) + if len(userdataBytes) > 0 { + if err := processUserdata(string(userdataBytes), env); err != nil { + log.Fatalf("Failed resolving user-data: %v", err) + if !ignoreFailure { + os.Exit(1) + } + } + } else { + log.Printf("No user data to handle.") + } +} - userdata := string(userdataBytes) +func processUserdata(userdata string, env *initialize.Environment) error { userdata = env.Apply(userdata) parsed, err := initialize.ParseUserData(userdata) if err != nil { log.Printf("Failed parsing user-data: %v", err) - if ignoreFailure { - os.Exit(0) - } else { - os.Exit(1) - } + return err } err = initialize.PrepWorkspace(env.Workspace()) @@ -109,11 +111,9 @@ func main() { if err == nil { var name string name, err = system.ExecuteScript(path) - initialize.PersistUnitNameInWorkspace(name, workspace) + initialize.PersistUnitNameInWorkspace(name, env.Workspace()) } } - if err != nil { - log.Fatalf("Failed resolving user-data: %v", err) - } + return err }