From 5b67d6fbd97ca1258e5ae9276c9c88905fc56a2e Mon Sep 17 00:00:00 2001 From: Tine Jozelj Date: Tue, 16 Jul 2019 14:47:20 +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 | 92 ++++++++++++++++++++++++++++++++++++++++ docs/helm/helm_create.md | 4 +- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/cmd/helm/create.go b/cmd/helm/create.go index 451b4ce86..da91291fd 100644 --- a/cmd/helm/create.go +++ b/cmd/helm/create.go @@ -88,7 +88,7 @@ func newCreateCmd(out io.Writer) *cobra.Command { }, } - cmd.Flags().StringVarP(&cc.starter, "starter", "p", "", "The named Helm starter scaffold") + cmd.Flags().StringVarP(&cc.starter, "starter", "p", "", "The name or absolute path to Helm starter scaffold") return cmd } @@ -106,6 +106,10 @@ func (c *createCmd) run() error { if c.starter != "" { // Create from the starter lstarter := filepath.Join(c.home.Starters(), c.starter) + // If path is absolute, we dont want to prefix it with helm starters folder + if filepath.IsAbs(c.starter) { + lstarter = c.starter + } return chartutil.CreateFrom(cfile, filepath.Dir(c.name), lstarter) } diff --git a/cmd/helm/create_test.go b/cmd/helm/create_test.go index 20bace864..fb118ba58 100644 --- a/cmd/helm/create_test.go +++ b/cmd/helm/create_test.go @@ -163,3 +163,95 @@ func TestCreateStarterCmd(t *testing.T) { } } + +func TestCreateStarterAbsoluteCmd(t *testing.T) { + cname := "testchart" + // Make a temp dir + tdir, err := ioutil.TempDir("", "helm-create-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tdir) + + thome, err := tempHelmHome(t) + if err != nil { + t.Fatal(err) + } + cleanup := resetEnv() + defer func() { + os.RemoveAll(thome.String()) + cleanup() + }() + + settings.Home = thome + + // Create a starter. + starterchart := filepath.Join(tdir, "starters") + os.Mkdir(starterchart, 0755) + if dest, err := chartutil.Create(&chart.Metadata{Name: "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) + } + + // CD into it + pwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if err := os.Chdir(tdir); err != nil { + t.Fatal(err) + } + defer os.Chdir(pwd) + + // Run a create + cmd := newCreateCmd(ioutil.Discard) + cmd.ParseFlags([]string{"--starter", filepath.Join(starterchart, "starterchart")}) + if err := cmd.RunE(cmd, []string{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 := chartutil.LoadDir(cname) + if err != nil { + t.Fatal(err) + } + + if c.Metadata.Name != cname { + t.Errorf("Expected %q name, got %q", cname, c.Metadata.Name) + } + if c.Metadata.ApiVersion != chartutil.ApiVersionV1 { + t.Errorf("Wrong API version: %q", c.Metadata.ApiVersion) + } + + expectedTemplateCount := 8 + if l := len(c.Templates); l != expectedTemplateCount { + t.Errorf("Expected %d templates, got %d", expectedTemplateCount, l) + } + + found := false + for _, tpl := range c.Templates { + if tpl.Name == "templates/foo.tpl" { + found = true + data := tpl.Data + if string(data) != "test" { + t.Errorf("Expected template 'test', got %q", string(data)) + } + } + } + if !found { + t.Error("Did not find foo.tpl") + } + +} diff --git a/docs/helm/helm_create.md b/docs/helm/helm_create.md index 0bf526a22..f0c5cd037 100644 --- a/docs/helm/helm_create.md +++ b/docs/helm/helm_create.md @@ -44,7 +44,7 @@ helm create NAME [flags] ``` -h, --help help for create - -p, --starter string The named Helm starter scaffold + -p, --starter string The name or absolute path to Helm starter scaffold ``` ### Options inherited from parent commands @@ -63,4 +63,4 @@ helm create NAME [flags] * [helm](helm.md) - The Helm package manager for Kubernetes. -###### Auto generated by spf13/cobra on 5-Jun-2019 +###### Auto generated by spf13/cobra on 7-Jul-2019