From cb2d89af6f5b632bda1f4916f6d4e51c841ffd58 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 7 May 2019 11:51:21 +0200 Subject: [PATCH] simplify path parsing by making at string instead of an array + add tests --- command/build.go | 9 +++++---- command/build_test.go | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/command/build.go b/command/build.go index aa54de632..65d19bd23 100644 --- a/command/build.go +++ b/command/build.go @@ -56,7 +56,7 @@ type Config struct { Color, Debug, Force, Timestamp bool ParallelBuilds int64 OnError string - Args []string + Path string } func (c *BuildCommand) ParseArgs(args []string) (Config, int) { @@ -83,11 +83,12 @@ func (c *BuildCommand) ParseArgs(args []string) (Config, int) { cfg.ParallelBuilds = math.MaxInt64 } - cfg.Args = flags.Args() - if len(cfg.Args) != 1 { + args = flags.Args() + if len(args) != 1 { flags.Usage() return cfg, 1 } + cfg.Path = args[0] return cfg, 0 } @@ -100,7 +101,7 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int { // Parse the template var tpl *template.Template var err error - tpl, err = template.ParseFile(cfg.Args[0]) + tpl, err = template.ParseFile(cfg.Path) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to parse template: %s", err)) return 1 diff --git a/command/build_test.go b/command/build_test.go index ac88d63b7..ac29bda99 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -231,7 +231,7 @@ func TestBuildCommand_ParseArgs(t *testing.T) { {fields{defaultMeta}, args{[]string{"file.json"}}, Config{ - Args: []string{"file.json"}, + Path: "file.json", ParallelBuilds: math.MaxInt64, Color: true, }, @@ -240,7 +240,7 @@ func TestBuildCommand_ParseArgs(t *testing.T) { {fields{defaultMeta}, args{[]string{"-parallel=true", "file.json"}}, Config{ - Args: []string{"file.json"}, + Path: "file.json", ParallelBuilds: math.MaxInt64, Color: true, }, @@ -249,7 +249,7 @@ func TestBuildCommand_ParseArgs(t *testing.T) { {fields{defaultMeta}, args{[]string{"-parallel=false", "file.json"}}, Config{ - Args: []string{"file.json"}, + Path: "file.json", ParallelBuilds: 1, Color: true, }, @@ -258,7 +258,16 @@ func TestBuildCommand_ParseArgs(t *testing.T) { {fields{defaultMeta}, args{[]string{"-parallel-builds=5", "file.json"}}, Config{ - Args: []string{"file.json"}, + Path: "file.json", + ParallelBuilds: 5, + Color: true, + }, + 0, + }, + {fields{defaultMeta}, + args{[]string{"-parallel=false", "-parallel-builds=5", "otherfile.json"}}, + Config{ + Path: "otherfile.json", ParallelBuilds: 5, Color: true, },