Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 96bef40

Browse files
author
Ian Campbell
committed
refactor prepareCredentialSet into sub functions
I added some more and the linter started complaining about the cyclomatic complexity. Reduce this function to subfunctions. Signed-off-by: Ian Campbell <ijc@docker.com>
1 parent 1255e0d commit 96bef40

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

internal/commands/cnab.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,57 @@ type bindMount struct {
3535

3636
const defaultSocketPath string = "/var/run/docker.sock"
3737

38-
func prepareCredentialSet(contextName string, contextStore store.Store, b *bundle.Bundle, namedCredentialsets []string) (map[string]string, error) {
39-
// docker desktop contexts require some rewriting for being used within a container
40-
contextStore = dockerDesktopAwareStore{Store: contextStore}
41-
creds := map[string]string{}
38+
func addNamedCredentialSets(creds map[string]string, namedCredentialsets []string) error {
4239
for _, file := range namedCredentialsets {
4340
if _, err := os.Stat(file); err != nil {
4441
file = filepath.Join(duffleHome().Credentials(), file+".yaml")
4542
}
4643
c, err := credentials.Load(file)
4744
if err != nil {
48-
return nil, err
45+
return err
4946
}
5047
values, err := c.Resolve()
5148
if err != nil {
52-
return nil, err
49+
return err
5350
}
5451
for k, v := range values {
5552
if _, ok := creds[k]; ok {
56-
return nil, fmt.Errorf("ambiguous credential resolution: %q is present in multiple credential sets", k)
53+
return fmt.Errorf("ambiguous credential resolution: %q is present in multiple credential sets", k)
5754
}
5855
creds[k] = v
5956
}
6057
}
58+
return nil
59+
}
60+
61+
func addDockerCredentials(creds map[string]string, contextName string, contextStore store.Store) error {
6162
if contextName != "" {
6263
data, err := ioutil.ReadAll(store.Export(contextName, contextStore))
6364
if err != nil {
64-
return nil, err
65+
return err
6566
}
6667
creds[internal.CredentialDockerContextName] = string(data)
6768
}
69+
return nil
70+
}
71+
72+
func prepareCredentialSet(contextName string, contextStore store.Store, b *bundle.Bundle, namedCredentialsets []string) (map[string]string, error) {
73+
// docker desktop contexts require some rewriting for being used within a container
74+
contextStore = dockerDesktopAwareStore{Store: contextStore}
75+
creds := map[string]string{}
76+
if err := addNamedCredentialSets(creds, namedCredentialsets); err != nil {
77+
return nil, err
78+
}
79+
if err := addDockerCredentials(creds, contextName, contextStore); err != nil {
80+
return nil, err
81+
}
82+
6883
_, requiresDockerContext := b.Credentials[internal.CredentialDockerContextName]
6984
_, hasDockerContext := creds[internal.CredentialDockerContextName]
7085
if requiresDockerContext && !hasDockerContext {
7186
return nil, errors.New("no target context specified. Use --target-context= or DOCKER_TARGET_CONTEXT= to define it")
7287
}
88+
7389
return creds, nil
7490
}
7591

0 commit comments

Comments
 (0)