refactor(userdata): Move userdata processing into a function

This commit is contained in:
Alex Crawford 2014-05-22 14:02:10 -07:00
parent a4035cffea
commit f8a823cf7e

View File

@ -75,24 +75,26 @@ func main() {
} }
} }
if len(userdataBytes) == 0 { env := initialize.NewEnvironment("/", workspace)
log.Printf("No user data to handle, exiting.") if len(userdataBytes) > 0 {
os.Exit(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.")
}
} }
env := initialize.NewEnvironment("/", workspace) func processUserdata(userdata string, env *initialize.Environment) error {
userdata := string(userdataBytes)
userdata = env.Apply(userdata) userdata = env.Apply(userdata)
parsed, err := initialize.ParseUserData(userdata) parsed, err := initialize.ParseUserData(userdata)
if err != nil { if err != nil {
log.Printf("Failed parsing user-data: %v", err) log.Printf("Failed parsing user-data: %v", err)
if ignoreFailure { return err
os.Exit(0)
} else {
os.Exit(1)
}
} }
err = initialize.PrepWorkspace(env.Workspace()) err = initialize.PrepWorkspace(env.Workspace())
@ -109,11 +111,9 @@ func main() {
if err == nil { if err == nil {
var name string var name string
name, err = system.ExecuteScript(path) name, err = system.ExecuteScript(path)
initialize.PersistUnitNameInWorkspace(name, workspace) initialize.PersistUnitNameInWorkspace(name, env.Workspace())
} }
} }
if err != nil { return err
log.Fatalf("Failed resolving user-data: %v", err)
}
} }