Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 1ceb12a

Browse files
committed
chore: manage CLI Hints enable/disable behaviour
- cliHints entry in config.json - if not present, enabled by default - can be overridden by the DOCKER_CLI_HINTS environment variable Signed-off-by: Yves Brissaud <yves.brissaud@docker.com>
1 parent 90058ec commit 1ceb12a

File tree

5 files changed

+178
-77
lines changed

5 files changed

+178
-77
lines changed

api/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,5 @@ func configFilePath(dir string) string {
101101
// File contains the current context from the docker configuration file
102102
type File struct {
103103
CurrentContext string `json:"currentContext,omitempty"`
104+
CliHints *bool `json:"cliHints,omitempty"`
104105
}

cli/mobycli/cli_hints.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mobycli
18+
19+
import (
20+
"os"
21+
"strconv"
22+
23+
"github.com/docker/compose-cli/api/config"
24+
)
25+
26+
const (
27+
cliHintsEnvVarName = "DOCKER_CLI_HINTS"
28+
cliHintsDefaultBehaviour = true
29+
)
30+
31+
func CliHintsEnabled() bool {
32+
if envValue, ok := os.LookupEnv(cliHintsEnvVarName); ok {
33+
if enabled, err := strconv.ParseBool(envValue); err == nil {
34+
return enabled
35+
}
36+
}
37+
38+
conf, err := config.LoadFile(config.Dir())
39+
if err != nil {
40+
// can't read the config file, use the default behaviour
41+
return cliHintsDefaultBehaviour
42+
}
43+
if conf.CliHints != nil {
44+
return *conf.CliHints
45+
}
46+
47+
return cliHintsDefaultBehaviour
48+
}

cli/mobycli/cli_hints_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mobycli
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/docker/compose-cli/api/config"
25+
26+
"gotest.tools/v3/assert"
27+
)
28+
29+
func TestCliHintsEnabled(t *testing.T) {
30+
testCases := []struct {
31+
name string
32+
setup func()
33+
expected bool
34+
}{
35+
{
36+
"enabled by default",
37+
func() {},
38+
true,
39+
},
40+
{
41+
"handle true value",
42+
func() {
43+
t.Setenv(cliHintsEnvVarName, "t")
44+
},
45+
true,
46+
},
47+
{
48+
"handle false value",
49+
func() {
50+
t.Setenv(cliHintsEnvVarName, "FALSE")
51+
},
52+
false,
53+
},
54+
{
55+
"handle error",
56+
func() {
57+
t.Setenv(cliHintsEnvVarName, "123")
58+
},
59+
true,
60+
},
61+
{
62+
"enabled in config file",
63+
func() {
64+
d := testConfigDir(t)
65+
writeSampleConfig(t, d, configEnabled)
66+
},
67+
true,
68+
},
69+
{
70+
"disabled in config file",
71+
func() {
72+
d := testConfigDir(t)
73+
writeSampleConfig(t, d, configDisabled)
74+
},
75+
false,
76+
},
77+
{
78+
"enabled in config file but disabled by env var",
79+
func() {
80+
d := testConfigDir(t)
81+
writeSampleConfig(t, d, configEnabled)
82+
t.Setenv(cliHintsEnvVarName, "FALSE")
83+
},
84+
false,
85+
},
86+
{
87+
"disabled in config file but enabled by env var",
88+
func() {
89+
d := testConfigDir(t)
90+
writeSampleConfig(t, d, configDisabled)
91+
t.Setenv(cliHintsEnvVarName, "true")
92+
},
93+
true,
94+
},
95+
}
96+
97+
for _, testCase := range testCases {
98+
tc := testCase
99+
t.Run(tc.name, func(t *testing.T) {
100+
tc.setup()
101+
assert.Equal(t, CliHintsEnabled(), tc.expected)
102+
})
103+
}
104+
}
105+
106+
func testConfigDir(t *testing.T) string {
107+
dir := config.Dir()
108+
d, _ := os.MkdirTemp("", "")
109+
config.WithDir(d)
110+
t.Cleanup(func() {
111+
_ = os.RemoveAll(d)
112+
config.WithDir(dir)
113+
})
114+
return d
115+
}
116+
117+
func writeSampleConfig(t *testing.T, d string, conf []byte) {
118+
err := os.WriteFile(filepath.Join(d, config.ConfigFileName), conf, 0644)
119+
assert.NilError(t, err)
120+
}
121+
122+
var configEnabled = []byte(`{
123+
"cliHints": true
124+
}`)
125+
126+
var configDisabled = []byte(`{
127+
"cliHints": false
128+
}`)

cli/mobycli/scout_suggest.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,13 @@ package mobycli
1919
import (
2020
"fmt"
2121
"os"
22-
"strconv"
2322
"strings"
2423

2524
"github.com/docker/compose/v2/pkg/utils"
2625

2726
"github.com/fatih/color"
2827
)
2928

30-
const (
31-
scoutHintEnvVarName = "DOCKER_SCOUT_HINTS"
32-
)
33-
34-
func isDockerScoutHintsEnabled() bool {
35-
enabled, err := strconv.ParseBool(os.Getenv(scoutHintEnvVarName))
36-
return err != nil || enabled
37-
}
38-
3929
func displayScoutQuickViewSuggestMsgOnPull(args []string) {
4030
image := pulledImageFromArgs(args)
4131
displayScoutQuickViewSuggestMsg(image)
@@ -52,7 +42,7 @@ func displayScoutQuickViewSuggestMsgOnBuild(args []string) {
5242
}
5343

5444
func displayScoutQuickViewSuggestMsg(image string) {
55-
if !isDockerScoutHintsEnabled() {
45+
if !CliHintsEnabled() {
5646
return
5747
}
5848
if len(image) > 0 {

cli/mobycli/scout_suggest_test.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)