Skip to content

Commit f3e8309

Browse files
committed
Move config file loading to more appropriate packages.
Signed-off-by: Daniel Nephin <[email protected]>
1 parent f82f61e commit f3e8309

6 files changed

Lines changed: 34 additions & 35 deletions

File tree

cli/command/cli.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package command
22

33
import (
4-
"fmt"
54
"io"
65
"net/http"
76
"os"
@@ -157,7 +156,7 @@ func getConfiguredCredentialStore(c *configfile.ConfigFile, serverAddress string
157156
// Initialize the dockerCli runs initialization that must happen after command
158157
// line flags are parsed.
159158
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
160-
cli.configFile = LoadDefaultConfigFile(cli.err)
159+
cli.configFile = cliconfig.LoadDefaultConfigFile(cli.err)
161160

162161
var err error
163162
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
@@ -219,19 +218,6 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
219218
return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
220219
}
221220

222-
// LoadDefaultConfigFile attempts to load the default config file and returns
223-
// an initialized ConfigFile struct if none is found.
224-
func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
225-
configFile, e := cliconfig.Load(cliconfig.Dir())
226-
if e != nil {
227-
fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
228-
}
229-
if !configFile.ContainsAuth() {
230-
credentials.DetectDefaultStore(configFile)
231-
}
232-
return configFile
233-
}
234-
235221
// NewAPIClientFromFlags creates a new APIClient from command line flags
236222
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
237223
host, err := getServerHost(opts.Hosts, opts.TLSOptions)

cli/config/config.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67
"path/filepath"
@@ -9,6 +10,7 @@ import (
910
"github.com/docker/docker/api/types"
1011
"github.com/docker/docker/pkg/homedir"
1112
"github.com/pkg/errors"
13+
"github.com/docker/cli/cli/config/credentials"
1214
)
1315

1416
const (
@@ -38,15 +40,6 @@ func SetDir(dir string) {
3840
configDir = dir
3941
}
4042

41-
// NewConfigFile initializes an empty configuration file for the given filename 'fn'
42-
func NewConfigFile(fn string) *configfile.ConfigFile {
43-
return &configfile.ConfigFile{
44-
AuthConfigs: make(map[string]types.AuthConfig),
45-
HTTPHeaders: make(map[string]string),
46-
Filename: fn,
47-
}
48-
}
49-
5043
// LegacyLoadFromReader is a convenience function that creates a ConfigFile object from
5144
// a non-nested reader
5245
func LegacyLoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
@@ -118,3 +111,16 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
118111
}
119112
return &configFile, nil
120113
}
114+
115+
// LoadDefaultConfigFile attempts to load the default config file and returns
116+
// an initialized ConfigFile struct if none is found.
117+
func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
118+
configFile, e := Load(Dir())
119+
if e != nil {
120+
fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
121+
}
122+
if !configFile.ContainsAuth() {
123+
credentials.DetectDefaultStore(configFile)
124+
}
125+
return configFile
126+
}

cli/config/config_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,6 @@ func TestConfigDir(t *testing.T) {
484484
}
485485
}
486486

487-
func TestConfigFile(t *testing.T) {
488-
configFilename := "configFilename"
489-
configFile := NewConfigFile(configFilename)
490-
491-
if configFile.Filename != configFilename {
492-
t.Fatalf("Expected %s, got %s", configFilename, configFile.Filename)
493-
}
494-
}
495-
496487
func TestJSONReaderNoFile(t *testing.T) {
497488
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "[email protected]" } } }`
498489

cli/config/configfile/file.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ type ConfigFile struct {
4343
PruneFilters []string `json:"pruneFilters,omitempty"`
4444
}
4545

46+
// NewConfigFile initializes an empty configuration file for the given filename 'fn'
47+
func NewConfigFile(fn string) *ConfigFile {
48+
return &ConfigFile{
49+
AuthConfigs: make(map[string]types.AuthConfig),
50+
HTTPHeaders: make(map[string]string),
51+
Filename: fn,
52+
}
53+
}
54+
4655
// LegacyLoadFromReader reads the non-nested configuration data given and sets up the
4756
// auth config information with given directory and populates the receiver object
4857
func (configFile *ConfigFile) LegacyLoadFromReader(configData io.Reader) error {

cli/config/configfile/file_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/docker/docker/api/types"
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestEncodeAuth(t *testing.T) {
@@ -25,3 +26,10 @@ func TestEncodeAuth(t *testing.T) {
2526
t.Fatal("AuthString encoding isn't correct.")
2627
}
2728
}
29+
30+
func TestConfigFile(t *testing.T) {
31+
configFilename := "configFilename"
32+
configFile := NewConfigFile(configFilename)
33+
34+
assert.Equal(t, configFilename, configFile.Filename)
35+
}

cli/config/credentials/file_store_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"io/ioutil"
55
"testing"
66

7-
cliconfig "github.com/docker/cli/cli/config"
87
"github.com/docker/cli/cli/config/configfile"
98
"github.com/docker/docker/api/types"
109
)
@@ -14,7 +13,7 @@ func newConfigFile(auths map[string]types.AuthConfig) *configfile.ConfigFile {
1413
name := tmp.Name()
1514
tmp.Close()
1615

17-
c := cliconfig.NewConfigFile(name)
16+
c := configfile.NewConfigFile(name)
1817
c.AuthConfigs = auths
1918
return c
2019
}

0 commit comments

Comments
 (0)