[lxc-devel] [distrobuilder/master] main.go: Do not show help message if downloader.Run fails

marcosps on Github lxc-bot at linuxcontainers.org
Thu Jun 7 01:46:59 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1229 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180607/de3d80ac/attachment.bin>
-------------- next part --------------
From 23fbd5a63c6ea18d1969de343c47ef0c67c90270 Mon Sep 17 00:00:00 2001
From: Marcos Paulo de Souza <marcos.souza.org at gmail.com>
Date: Wed, 6 Jun 2018 22:38:16 -0300
Subject: [PATCH] main.go: Do not show help message if downloader.Run fails

Before this patch, if a user executes distrobuilder using the debian
example without having debootstrap installed, the following message is
returned:

Error: Error while downloading source: exec: "debootstrap": executable file not found in $PATH
Usage:
  distrobuilder build-dir <filename|-> <target dir> [flags]

Flags:
  -h, --help   help for build-dir

Global Flags:
      --cache-dir   Cache directory
      --cleanup     Clean up cache directory (default true)
  -o, --options     Override options (list of key=value)

This happens because distrobuilder subcommands uses cobra's RunE or PreRunE,
so if an error is returned, the help message is shown.

In order to avoid this message, execute os.Exit(1) when downloader.Run fails:

Error: Error while downloading source: exec: "debootstrap": executable file not found in $PATH

Signed-off-by: Marcos Paulo de Souza <marcos.souza.org at gmail.com>
---
 distrobuilder/main.go | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index 89e2a4a..4a8d696 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -190,7 +190,8 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
 	// Download the root filesystem
 	err = downloader.Run(*c.definition, c.sourceDir)
 	if err != nil {
-		return fmt.Errorf("Error while downloading source: %s", err)
+		fmt.Fprintf(os.Stderr, "Error while downloading source: %s\n", err)
+		os.Exit(1)
 	}
 
 	// Setup the mounts and chroot into the rootfs


More information about the lxc-devel mailing list