-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.go
More file actions
204 lines (179 loc) · 5.13 KB
/
setup.go
File metadata and controls
204 lines (179 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strings"
"github.com/docker/cli/cli/config/configfile"
"github.com/goccy/go-yaml"
)
// setupCmd handles the logic for the "setup" command.
type setupCmd struct {
Command string
Out io.Writer
Registry string
configPath string
}
// Run executes the setup command.
func (c *setupCmd) Run() error {
switch c.Command {
case "show":
return c.show()
case "default":
return c.configure(true)
default:
return c.configure(false)
}
}
// show displays the current configuration.
func (c *setupCmd) show() error {
config, err := c.loadConfig()
if err != nil {
return err
}
// Check if default credential store is set to 'env'
defaultIsEnv := config.CredentialsStore == "env"
// Collect registries that use 'env' credential helper
var envRegistries []string
if config.CredentialHelpers != nil {
for registry, helper := range config.CredentialHelpers {
if helper == "env" {
envRegistries = append(envRegistries, registry)
}
}
}
slices.Sort(envRegistries)
// Create output structure
output := struct {
Default bool `yaml:"default"`
Registries []string `yaml:"registries"`
}{
Default: defaultIsEnv,
Registries: envRegistries,
}
// Marshal to YAML and output
yamlData, err := yaml.MarshalWithOptions(&output, yaml.IndentSequence(true))
if err != nil {
return fmt.Errorf("failed to marshal output to YAML: %w", err)
}
_, err = fmt.Fprint(c.Out, string(yamlData))
return err
}
// configure sets up the credential helper for a registry or as the default.
func (c *setupCmd) configure(defaultSetup bool) error {
if !defaultSetup {
if err := c.validateRegistry(); err != nil {
return err
}
}
if err := c.ensureDockerDir(); err != nil {
return err
}
config, err := c.loadConfig()
if err != nil {
return err
}
// Check if already configured
if (defaultSetup && config.CredentialsStore == "env") ||
(!defaultSetup && config.CredentialHelpers[c.Registry] == "env") {
if defaultSetup {
_, err = fmt.Fprintln(c.Out, "Default credential store is already configured to use \"env\" credential helper")
} else {
_, err = fmt.Fprintf(c.Out, "Registry %q is already configured to use \"env\" credential helper\n", c.Registry)
}
return err
}
// Configure credential helper
if defaultSetup {
config.CredentialsStore = "env"
} else {
if config.CredentialHelpers == nil {
config.CredentialHelpers = make(map[string]string)
}
config.CredentialHelpers[c.Registry] = "env"
}
// Save configuration
if err = c.saveConfig(config); err != nil {
return err
}
if defaultSetup {
_, err = fmt.Fprintln(c.Out, "Default credential store successfully configured to use \"env\" credential helper")
} else {
_, err = fmt.Fprintf(c.Out, "Registry %q successfully configured to use \"env\" credential helper\n", c.Registry)
}
return err
}
func (c *setupCmd) validateRegistry() error {
if c.Registry == "" {
return errors.New("registry cannot be empty")
}
if strings.ContainsAny(c.Registry, " /\\") {
return fmt.Errorf("invalid registry: %q", c.Registry)
}
return nil
}
func (c *setupCmd) ensureDockerDir() error {
dockerDir := filepath.Dir(c.configPath)
if err := os.MkdirAll(dockerDir, 0700); err != nil {
return fmt.Errorf("failed to create Docker directory %q: %w", dockerDir, err)
}
return nil
}
func (c *setupCmd) loadConfig() (*configfile.ConfigFile, error) {
configData, err := os.ReadFile(c.configPath)
if os.IsNotExist(err) {
return configfile.New(c.configPath), nil
}
if err != nil {
return nil, fmt.Errorf("failed to read Docker config file %q: %w", c.configPath, err)
}
var config configfile.ConfigFile
if err := json.Unmarshal(configData, &config); err != nil {
return nil, fmt.Errorf("failed to parse Docker config file %q: %w", c.configPath, err)
}
return &config, nil
}
func (c *setupCmd) saveConfig(config *configfile.ConfigFile) error {
configData, err := json.MarshalIndent(config, "", "\t")
if err != nil {
return fmt.Errorf("failed to marshal Docker config: %w", err)
}
if err = os.WriteFile(c.configPath, configData, 0600); err != nil {
return fmt.Errorf("failed to write Docker config file %q: %w", c.configPath, err)
}
return nil
}
// RunSetupCommand is the main entry point for the setup command.
func RunSetupCommand(args []string, out io.Writer) error {
if len(args) < 1 {
return errors.New("missing argument\nUsage: docker-credential-env setup <show|default|registry-url>")
}
cmd := &setupCmd{
Command: args[0],
Out: out,
}
// Determine config path
if dockerConfigDir := os.Getenv("DOCKER_CONFIG"); dockerConfigDir != "" {
cmd.configPath = filepath.Join(dockerConfigDir, "config.json")
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get user home directory: %w", err)
}
cmd.configPath = filepath.Join(homeDir, ".docker", "config.json")
}
// Validate arguments
switch cmd.Command {
case "show", "default":
if len(args) > 1 {
return fmt.Errorf("%q command does not accept additional arguments", cmd.Command)
}
default: // Assumes registry
cmd.Registry = args[0]
}
return cmd.Run()
}