Skip to content

Commit 96f2094

Browse files
committed
default description about kube contexts
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent 4fb2057 commit 96f2094

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

kube/context.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package kube
2020

2121
import (
22+
"fmt"
23+
2224
"github.com/AlecAivazis/survey/v2/terminal"
2325
"github.com/docker/compose-cli/api/context/store"
2426
"github.com/docker/compose-cli/api/errdefs"
@@ -105,5 +107,19 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
105107
ContextName: cp.KubeContextName,
106108
KubeconfigPath: cp.KubeConfigPath,
107109
FromEnvironment: cp.FromEnvironment,
108-
}, cp.Description, nil
110+
}, cp.getDescription(), nil
111+
}
112+
113+
func (cp ContextParams) getDescription() string {
114+
if cp.Description != "" {
115+
return cp.Description
116+
}
117+
if cp.FromEnvironment {
118+
return "From environment variables"
119+
}
120+
configFile := "default kube config"
121+
if cp.KubeconfigPath != "" {
122+
configFile = cp.KubeconfigPath
123+
}
124+
return fmt.Sprintf("%s (in %s)", cp.ContextName, configFile)
109125
}

kube/context_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// +build kube
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kube
20+
21+
import (
22+
"testing"
23+
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func TestContextDescriptionIfEnvVar(t *testing.T) {
28+
cp := ContextParams{
29+
FromEnvironment: true,
30+
}
31+
description := cp.getDescription()
32+
assert.Equal(t, description, "From environment variables")
33+
}
34+
35+
func TestContextDescriptionIfProvided(t *testing.T) {
36+
cp := ContextParams{
37+
Description: "custom description",
38+
FromEnvironment: true,
39+
}
40+
description := cp.getDescription()
41+
assert.Equal(t, description, "custom description")
42+
}
43+
44+
func TestContextDescriptionIfConfigFile(t *testing.T) {
45+
cp := ContextParams{
46+
ContextName: "my-context",
47+
KubeconfigPath: "~/.kube/config",
48+
}
49+
description := cp.getDescription()
50+
assert.Equal(t, description, "my-context (in ~/.kube/config)")
51+
}
52+
func TestContextDescriptionIfDefaultConfigFile(t *testing.T) {
53+
cp := ContextParams{
54+
ContextName: "my-context",
55+
}
56+
description := cp.getDescription()
57+
assert.Equal(t, description, "my-context (in default kube config)")
58+
}

0 commit comments

Comments
 (0)