From 42e3beb1ad6e61c5a5bd97ce29ba61745b1c12fb Mon Sep 17 00:00:00 2001 From: Tine Jozelj Date: Tue, 16 Jul 2019 14:44:57 +0200 Subject: [PATCH] feat(helm:create): allow absolute paths If starter template path is an absolute path, it shouldn't be prefixed with c.home.Starters() but rather be used as is. Signed-off-by: Tine Jozelj --- cmd/helm/create.go | 6 +++- cmd/helm/create_test.go | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/cmd/helm/create.go b/cmd/helm/create.go index 4c26e4ca8..d22409579 100644 --- a/cmd/helm/create.go +++ b/cmd/helm/create.go @@ -67,7 +67,7 @@ func newCreateCmd(out io.Writer) *cobra.Command { }, } - cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "the named Helm starter scaffold") + cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "The name or absolute path to Helm starter scaffold") return cmd } @@ -87,6 +87,10 @@ func (o *createOptions) run(out io.Writer) error { if o.starter != "" { // Create from the starter lstarter := filepath.Join(settings.Home.Starters(), o.starter) + // If path is absolute, we dont want to prefix it with helm starters folder + if filepath.IsAbs(o.starter) { + lstarter = o.starter + } return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter) } diff --git a/cmd/helm/create_test.go b/cmd/helm/create_test.go index bf36be68e..0f973d7de 100644 --- a/cmd/helm/create_test.go +++ b/cmd/helm/create_test.go @@ -128,3 +128,73 @@ func TestCreateStarterCmd(t *testing.T) { } } + +func TestCreateStarterAbsoluteCmd(t *testing.T) { + defer resetEnv()() + + cname := "testchart" + // Make a temp dir + tdir := testTempDir(t) + + hh := testHelmHome(t) + settings.Home = hh + + // Create a starter. + starterchart := hh.Starters() + os.Mkdir(starterchart, 0755) + if dest, err := chartutil.Create("starterchart", starterchart); err != nil { + t.Fatalf("Could not create chart: %s", err) + } else { + t.Logf("Created %s", dest) + } + tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl") + if err := ioutil.WriteFile(tplpath, []byte("test"), 0755); err != nil { + t.Fatalf("Could not write template: %s", err) + } + + defer testChdir(t, tdir)() + + starterChartPath := filepath.Join(starterchart, "starterchart") + + // Run a create + if _, _, err := executeActionCommand(fmt.Sprintf("--home='%s' create --starter=%s %s", hh.String(), starterChartPath, cname)); err != nil { + t.Errorf("Failed to run create: %s", err) + return + } + + // Test that the chart is there + if fi, err := os.Stat(cname); err != nil { + t.Fatalf("no chart directory: %s", err) + } else if !fi.IsDir() { + t.Fatalf("chart is not directory") + } + + c, err := loader.LoadDir(cname) + if err != nil { + t.Fatal(err) + } + + if c.Name() != cname { + t.Errorf("Expected %q name, got %q", cname, c.Name()) + } + if c.Metadata.APIVersion != chart.APIVersionV1 { + t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) + } + + if l := len(c.Templates); l != 6 { + t.Errorf("Expected 5 templates, got %d", l) + } + + found := false + for _, tpl := range c.Templates { + if tpl.Name == "templates/foo.tpl" { + found = true + if data := string(tpl.Data); data != "test" { + t.Errorf("Expected template 'test', got %q", data) + } + } + } + if !found { + t.Error("Did not find foo.tpl") + } +}