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

Commit ce28ca2

Browse files
committed
ref: use plugins part of config file
To avoid any conflict with the CLI, use the `plugins` section of the config. This section is read and saved by the CLI without the risk to remove the value in there. So it's a safe way to deal with this feature. As it's a cross plugin configuration (now for scout but goal is wider than that) then put it under a generic `-x-cli-hints` name so that other plugins might use it if needed. Value can be true/false, but is parsed against these exact two values instead of using the ParseBool. Both the environment variable and the config value are parsed the same way. Signed-off-by: Yves Brissaud <[email protected]>
1 parent fac770a commit ce28ca2

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

api/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ func configFilePath(dir string) string {
100100

101101
// File contains the current context from the docker configuration file
102102
type File struct {
103-
CurrentContext string `json:"currentContext,omitempty"`
104-
CliHints *bool `json:"cliHints,omitempty"`
103+
CurrentContext string `json:"currentContext,omitempty"`
104+
Plugins map[string]map[string]string `json:"plugins,omitempty"`
105105
}

cli/mobycli/cli_hints.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@
1717
package mobycli
1818

1919
import (
20+
"fmt"
2021
"os"
21-
"strconv"
2222

2323
"github.com/docker/compose-cli/api/config"
2424
)
2525

2626
const (
2727
cliHintsEnvVarName = "DOCKER_CLI_HINTS"
2828
cliHintsDefaultBehaviour = true
29+
30+
cliHintsPluginName = "-x-cli-hints"
31+
cliHintsEnabledName = "enabled"
32+
cliHintsEnabled = "true"
33+
cliHintsDisabled = "false"
2934
)
3035

3136
func CliHintsEnabled() bool {
3237
if envValue, ok := os.LookupEnv(cliHintsEnvVarName); ok {
33-
if enabled, err := strconv.ParseBool(envValue); err == nil {
38+
if enabled, err := parseCliHintFlag(envValue); err == nil {
3439
return enabled
3540
}
3641
}
@@ -40,9 +45,24 @@ func CliHintsEnabled() bool {
4045
// can't read the config file, use the default behaviour
4146
return cliHintsDefaultBehaviour
4247
}
43-
if conf.CliHints != nil {
44-
return *conf.CliHints
48+
if cliHintsPluginConfig, ok := conf.Plugins[cliHintsPluginName]; ok {
49+
if cliHintsValue, ok := cliHintsPluginConfig[cliHintsEnabledName]; ok {
50+
if cliHints, err := parseCliHintFlag(cliHintsValue); err == nil {
51+
return cliHints
52+
}
53+
}
4554
}
4655

4756
return cliHintsDefaultBehaviour
4857
}
58+
59+
func parseCliHintFlag(value string) (bool, error) {
60+
switch value {
61+
case cliHintsEnabled:
62+
return true, nil
63+
case cliHintsDisabled:
64+
return false, nil
65+
default:
66+
return cliHintsDefaultBehaviour, fmt.Errorf("could not parse CLI hints enabled flag")
67+
}
68+
}

cli/mobycli/cli_hints_test.go

+49-9
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ func TestCliHintsEnabled(t *testing.T) {
3838
true,
3939
},
4040
{
41-
"handle true value",
41+
"enabled from environment variable",
4242
func() {
43-
t.Setenv(cliHintsEnvVarName, "t")
43+
t.Setenv(cliHintsEnvVarName, "true")
4444
},
4545
true,
4646
},
4747
{
48-
"handle false value",
48+
"disabled from environment variable",
4949
func() {
50-
t.Setenv(cliHintsEnvVarName, "FALSE")
50+
t.Setenv(cliHintsEnvVarName, "false")
5151
},
5252
false,
5353
},
5454
{
55-
"handle error",
55+
"unsupported value",
5656
func() {
57-
t.Setenv(cliHintsEnvVarName, "123")
57+
t.Setenv(cliHintsEnvVarName, "maybe")
5858
},
5959
true,
6060
},
@@ -66,6 +66,23 @@ func TestCliHintsEnabled(t *testing.T) {
6666
},
6767
true,
6868
},
69+
{
70+
"plugin defined in config file but no enabled entry",
71+
func() {
72+
d := testConfigDir(t)
73+
writeSampleConfig(t, d, configPartial)
74+
},
75+
true,
76+
},
77+
78+
{
79+
"unsupported value",
80+
func() {
81+
d := testConfigDir(t)
82+
writeSampleConfig(t, d, configPartial)
83+
},
84+
true,
85+
},
6986
{
7087
"disabled in config file",
7188
func() {
@@ -79,7 +96,7 @@ func TestCliHintsEnabled(t *testing.T) {
7996
func() {
8097
d := testConfigDir(t)
8198
writeSampleConfig(t, d, configEnabled)
82-
t.Setenv(cliHintsEnvVarName, "FALSE")
99+
t.Setenv(cliHintsEnvVarName, "false")
83100
},
84101
false,
85102
},
@@ -120,9 +137,32 @@ func writeSampleConfig(t *testing.T, d string, conf []byte) {
120137
}
121138

122139
var configEnabled = []byte(`{
123-
"cliHints": true
140+
"plugins": {
141+
"-x-cli-hints": {
142+
"enabled": "true"
143+
}
144+
}
124145
}`)
125146

126147
var configDisabled = []byte(`{
127-
"cliHints": false
148+
"plugins": {
149+
"-x-cli-hints": {
150+
"enabled": "false"
151+
}
152+
}
153+
}`)
154+
155+
var configPartial = []byte(`{
156+
"plugins": {
157+
"-x-cli-hints": {
158+
}
159+
}
160+
}`)
161+
162+
var configOnce = []byte(`{
163+
"plugins": {
164+
"-x-cli-hints": {
165+
"enabled": "maybe"
166+
}
167+
}
128168
}`)

0 commit comments

Comments
 (0)