Skip to content

Commit eaca89c

Browse files
Add a fallback to inline environment when path doesn't exist (#637)
* Add a fallback to inline environment when path doesn't exist * Add test + better error message in case of env not being found Co-authored-by: Julien Duchesne <[email protected]>
1 parent 0428803 commit eaca89c

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

pkg/tanka/errors.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ func (e ErrNoEnv) Error() string {
1717

1818
// ErrMultipleEnvs means that the given jsonnet has multiple Environment objects
1919
type ErrMultipleEnvs struct {
20-
path string
21-
names []string
20+
path string
21+
givenName string
22+
foundEnvs []string
2223
}
2324

2425
func (e ErrMultipleEnvs) Error() string {
25-
return fmt.Sprintf("found multiple Environments in '%s'. Use `--name` to select a single one: \n - %s", e.path, strings.Join(e.names, "\n - "))
26+
if e.givenName != "" {
27+
return fmt.Sprintf("found multiple Environments in %q matching %q. Provide a more specific name that matches a single one: \n - %s", e.path, e.givenName, strings.Join(e.foundEnvs, "\n - "))
28+
}
29+
30+
return fmt.Sprintf("found multiple Environments in %q. Use `--name` to select a single one: \n - %s", e.path, strings.Join(e.foundEnvs, "\n - "))
2631
}
2732

2833
// ErrParallel is an array of errors collected while processing in parallel

pkg/tanka/inline.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (i *InlineLoader) Load(path string, opts LoaderOpts) (*v1alpha1.Environment
4646
}
4747
if len(envs) > 1 {
4848
sort.Strings(names)
49-
return nil, ErrMultipleEnvs{path, names}
49+
return nil, ErrMultipleEnvs{path, opts.Name, names}
5050
}
5151
}
5252

pkg/tanka/load.go

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tanka
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"path/filepath"
78

@@ -41,6 +42,15 @@ func Load(path string, opts Opts) (*LoadResult, error) {
4142
}
4243

4344
func LoadEnvironment(path string, opts Opts) (*v1alpha1.Environment, error) {
45+
_, err := os.Stat(path)
46+
if os.IsNotExist(err) {
47+
log.Printf("Path %q does not exist, trying to use it as an environment name", path)
48+
opts.Name = path
49+
path = "."
50+
} else if err != nil {
51+
return nil, err
52+
}
53+
4454
loader, err := DetectLoader(path)
4555
if err != nil {
4656
return nil, err

pkg/tanka/load_test.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tanka
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/grafana/tanka/pkg/kubernetes/manifest"
@@ -157,11 +158,11 @@ func TestLoadSelectEnvironment(t *testing.T) {
157158

158159
// Empty options, match all environments
159160
_, err = Load("./testdata/cases/multiple-inline-envs", Opts{})
160-
assert.EqualError(t, err, "found multiple Environments in './testdata/cases/multiple-inline-envs'. Use `--name` to select a single one: \n - project1-env1\n - project1-env2\n - project2-env1")
161+
assert.EqualError(t, err, "found multiple Environments in \"./testdata/cases/multiple-inline-envs\". Use `--name` to select a single one: \n - project1-env1\n - project1-env2\n - project2-env1")
161162

162163
// Partial match two environments
163164
_, err = Load("./testdata/cases/multiple-inline-envs", Opts{Name: "env1"})
164-
assert.EqualError(t, err, "found multiple Environments in './testdata/cases/multiple-inline-envs'. Use `--name` to select a single one: \n - project1-env1\n - project2-env1")
165+
assert.EqualError(t, err, "found multiple Environments in \"./testdata/cases/multiple-inline-envs\" matching \"env1\". Provide a more specific name that matches a single one: \n - project1-env1\n - project2-env1")
165166

166167
// Partial match
167168
result, err := Load("./testdata/cases/multiple-inline-envs", Opts{Name: "project2"})
@@ -174,6 +175,30 @@ func TestLoadSelectEnvironment(t *testing.T) {
174175
assert.Equal(t, "project1-env1", result.Env.Metadata.Name)
175176
}
176177

178+
// Tests that the load function will consider the path to be an environment name if it is not found
179+
func TestLoadEnvironmentFallbackToName(t *testing.T) {
180+
// Temporarily change the working directory to the testdata directory
181+
cwd, err := os.Getwd()
182+
require.NoError(t, err)
183+
err = os.Chdir("./testdata/cases/multiple-inline-envs")
184+
require.NoError(t, err)
185+
defer os.Chdir(cwd)
186+
187+
// Partial match two environments
188+
_, err = Load("env1", Opts{})
189+
assert.EqualError(t, err, "found multiple Environments in \".\" matching \"env1\". Provide a more specific name that matches a single one: \n - project1-env1\n - project2-env1")
190+
191+
// Partial match
192+
result, err := Load("project2", Opts{})
193+
require.NoError(t, err)
194+
assert.Equal(t, "project2-env1", result.Env.Metadata.Name)
195+
196+
// Full match
197+
result, err = Load("project1-env1", Opts{})
198+
require.NoError(t, err)
199+
assert.Equal(t, "project1-env1", result.Env.Metadata.Name)
200+
}
201+
177202
func TestLoadSelectEnvironmentFullMatchHasPriority(t *testing.T) {
178203
// `base` matches both `base` and `base-and-more`
179204
// However, the full match should win

0 commit comments

Comments
 (0)