Skip to content

Commit 111cbd5

Browse files
authored
fix(cli): Environment name at env creation (#404)
Environment name is the relative path of an env dir. tk env add is incorrectly using the base dir name. This PR fixes it. jpath pkg is used to benefit from the existing implementation. FindRoot function is made public instead of using Resolve because Resolve does many other things as well, some of which require files that don't exist at that time. Signed-off-by: Michal Wasilewski <[email protected]>
1 parent 614245c commit 111cbd5

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

cmd/tk/env.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/spf13/pflag"
1515
"k8s.io/apimachinery/pkg/labels"
1616

17+
"github.com/grafana/tanka/pkg/jsonnet/jpath"
1718
"github.com/grafana/tanka/pkg/kubernetes/client"
1819
"github.com/grafana/tanka/pkg/spec/v1alpha1"
1920
"github.com/grafana/tanka/pkg/term"
@@ -156,8 +157,12 @@ func addEnv(dir string, cfg *v1alpha1.Config) error {
156157
}
157158
}
158159

160+
rootDir, err := jpath.FindRoot(path)
161+
if err != nil {
162+
return err
163+
}
159164
// the other properties are already set by v1alpha1.New() and pflag.Parse()
160-
cfg.Metadata.Name = filepath.Base(path)
165+
cfg.Metadata.Name, _ = filepath.Rel(rootDir, path)
161166

162167
// write spec.json
163168
if err := writeJSON(cfg, filepath.Join(path, "spec.json")); err != nil {

pkg/jsonnet/jpath/jpath.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Resolve(workdir string) (path []string, base, root string, err error) {
3838
return nil, "", "", err
3939
}
4040

41-
root, err = findRoot(workdir)
41+
root, err = FindRoot(workdir)
4242
if err != nil {
4343
return nil, "", "", err
4444
}
@@ -60,10 +60,10 @@ func Resolve(workdir string) (path []string, base, root string, err error) {
6060
}, base, root, nil
6161
}
6262

63-
// findRoot searches for a rootDir by the following criteria:
63+
// FindRoot searches for a rootDir by the following criteria:
6464
// - tkrc.yaml is considered first, for a jb-independent way of marking the root
6565
// - if it is not present (default), jsonnetfile.json is used.
66-
func findRoot(start string) (dir string, err error) {
66+
func FindRoot(start string) (dir string, err error) {
6767
// root path based on os
6868
stop := "/"
6969
if runtime.GOOS == "windows" {

0 commit comments

Comments
 (0)