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 <tine.jozelj@tjo.space>
pull/5985/head
Tine Jozelj 5 years ago
parent 61578de6a0
commit 42e3beb1ad
No known key found for this signature in database
GPG Key ID: 8133A0BED3248588

@ -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)
}

@ -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")
}
}

Loading…
Cancel
Save