From d0f1d97797ec2674c94ab4581a66bc5a7f5c849d Mon Sep 17 00:00:00 2001 From: Santosh Mahto Date: Thu, 17 Jun 2021 08:18:07 +0530 Subject: [PATCH] Reverse the exitcode initialization value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fakemachine is subject to panic and causes Debos to exit success due to the current logic of the exitcode. For example, the fakemachine function CopyFileTo() panics if the file is missing. In the case of a panic, the function never returns. Thus, the exitcode cannot be set to 1 and Debos exits with 0. This commit reverses the logic of the exitcode: it is initialized to 1 (i.e. failure), and it is set to 0 (i.e. success) only if Debos has reached the end of the things it has to do (or for the help message). Signed-off-by: Gaƫl PORTAY --- cmd/debos/debos.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/cmd/debos/debos.go b/cmd/debos/debos.go index 0f9d6a16..0f0b6564 100644 --- a/cmd/debos/debos.go +++ b/cmd/debos/debos.go @@ -88,7 +88,7 @@ func main() { "no_proxy", } - var exitcode int = 0 + var exitcode int = 1 // Allow to run all deferred calls prior to os.Exit() defer func() { os.Exit(exitcode) @@ -102,22 +102,20 @@ func main() { if err != nil { flagsErr, ok := err.(*flags.Error) if ok && flagsErr.Type == flags.ErrHelp { + exitcode = 0 return } else { - exitcode = 1 return } } if len(args) != 1 { log.Println("No recipe given!") - exitcode = 1 return } if options.DisableFakeMachine && options.Backend != "auto" { log.Println("--disable-fakemachine and --fakemachine-backend are mutually exclusive") - exitcode = 1 return } @@ -140,12 +138,10 @@ func main() { r := actions.Recipe{} if _, err := os.Stat(file); os.IsNotExist(err) { log.Println(err) - exitcode = 1 return } if err := r.Parse(file, options.PrintRecipe, options.Verbose, options.TemplateVars); err != nil { log.Println(err) - exitcode = 1 return } @@ -169,7 +165,6 @@ func main() { if options.Backend == "auto" { runInFakeMachine = false } else { - exitcode = 1 return } } @@ -233,13 +228,14 @@ func main() { for _, a := range r.Actions { err = a.Verify(&context) - if exitcode = checkError(&context, err, a, "Verify"); exitcode != 0 { + if ret := checkError(&context, err, a, "Verify"); ret != 0 { return } } if options.DryRun { log.Printf("==== Recipe done (Dry run) ====") + exitcode = 0 return } @@ -253,7 +249,6 @@ func main() { memsize, err := units.RAMInBytes(options.Memory) if err != nil { fmt.Printf("Couldn't parse memory size: %v\n", err) - exitcode = 1 return } m.SetMemory(int(memsize / 1024 / 1024)) @@ -268,7 +263,6 @@ func main() { size, err := units.FromHumanSize(options.ScratchSize) if err != nil { fmt.Printf("Couldn't parse scratch size: %v\n", err) - exitcode = 1 return } m.SetScratch(size, "") @@ -310,30 +304,32 @@ func main() { defer a.PostMachineCleanup(&context) err = a.PreMachine(&context, m, &args) - if exitcode = checkError(&context, err, a, "PreMachine"); exitcode != 0 { + if ret := checkError(&context, err, a, "PreMachine"); ret != 0 { return } } - exitcode, err = m.RunInMachineWithArgs(args) + var status int + status, err = m.RunInMachineWithArgs(args) if err != nil { fmt.Println(err) return } - if exitcode != 0 { + if status != 0 { context.State = debos.Failed return } for _, a := range r.Actions { err = a.PostMachine(&context) - if exitcode = checkError(&context, err, a, "Postmachine"); exitcode != 0 { + if ret := checkError(&context, err, a, "Postmachine"); ret != 0 { return } } log.Printf("==== Recipe done ====") + exitcode = 0 return } @@ -343,7 +339,7 @@ func main() { defer a.PostMachineCleanup(&context) err = a.PreNoMachine(&context) - if exitcode = checkError(&context, err, a, "PreNoMachine"); exitcode != 0 { + if err := checkError(&context, err, a, "PreNoMachine"); err != 0 { return } } @@ -353,23 +349,23 @@ func main() { if _, err = os.Stat(context.Rootdir); os.IsNotExist(err) { err = os.Mkdir(context.Rootdir, 0755) if err != nil && os.IsNotExist(err) { - exitcode = 1 return } } - exitcode = do_run(r, &context) - if exitcode != 0 { + if ret := do_run(r, &context); ret != 0 { return } if !fakemachine.InMachine() { for _, a := range r.Actions { err = a.PostMachine(&context) - if exitcode = checkError(&context, err, a, "PostMachine"); exitcode != 0 { + if err := checkError(&context, err, a, "PostMachine"); err != 0 { return } } + + exitcode = 0 log.Printf("==== Recipe done ====") } }