Skip to content

Commit

Permalink
auto-create magic config.json on magic init
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <josh@dolit.ski>
  • Loading branch information
jdolitsky committed Aug 3, 2021
1 parent 8a22e5c commit 8385cb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,19 @@ Creating directory '/Users/me/Library/Application Support/magic/etc' ...
Creating mapping file '/Users/me/Library/Application Support/magic/etc/aws.yml' ...
Creating mapping file '/Users/me/Library/Application Support/magic/etc/azure.yml' ...
Creating mapping file '/Users/me/Library/Application Support/magic/etc/gcp.yml' ...
Creating magic config file '/Users/me/Library/Application Support/magic/config.json' ...
```

If you wish to make `magic` the default credential helper, manually modify
the `credsStore` field in `$HOME/.docker/config.json`:
Next, modify the `DOCKER_CONFIG` env var to point to the magic directory:

```javascript
{
// ...
"credsStore": "magic",
// ...
}
```
$ export DOCKER_CONFIG="$(docker-credential-magic home)"
```

You may wish to add the previous command to your `~/.bashrc` / `~/.bash_profile`.

If no matching domains are found, `magic` will fall back to use
your existing `$HOME/.docker/config.json`.

Note: At this time, `magic` will not automatically install the supported
helpers on your machine. You should install each of these manually.
Expand Down
37 changes: 23 additions & 14 deletions cmd/docker-credential-magic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func main() {
switch subcommand {
case constants.HelperSubcommandGet:
subcommandGet()
case "env":
subcommandEnv()
case "home":
subcommandHome()
case "init":
subcommandInit()
case "version":
Expand All @@ -56,7 +56,7 @@ func main() {
}

func usage() {
fmt.Printf("Usage: docker-credential-magic <%s|env|init|version>\n",
fmt.Printf("Usage: docker-credential-magic <%s|home|init|version>\n",
constants.HelperSubcommandGet)
os.Exit(1)
}
Expand Down Expand Up @@ -137,27 +137,25 @@ func subcommandGet() {
os.Exit(0)
}

func subcommandEnv() {
func subcommandHome() {
dockerCredentialMagicConfig := getDockerCredentialMagicConfig()
fmt.Printf("%s=\"%s\"\n",
constants.EnvVarDockerCredentialMagicConfig,
dockerCredentialMagicConfig)
fmt.Println(dockerCredentialMagicConfig)
os.Exit(0)
}

func subcommandInit() {
dockerCredentialMagicConfig := getDockerCredentialMagicConfig()
parentDir := filepath.Join(dockerCredentialMagicConfig, constants.MappingsSubdir)
parentDirAbs, err := filepath.Abs(parentDir)
dockerCredentialMagicConfigDirAbs, err := filepath.Abs(dockerCredentialMagicConfig)
if err != nil {
fmt.Printf("Error: '%s' is not a valid directory\n", dockerCredentialMagicConfig)
os.Exit(1)
}
if info, err := os.Stat(parentDirAbs); err == nil && info.IsDir() {
fmt.Printf("Directory '%s' already exists. Skipping.\n", parentDirAbs)
parentDir := filepath.Join(dockerCredentialMagicConfigDirAbs, constants.MappingsSubdir)
if info, err := os.Stat(parentDir); err == nil && info.IsDir() {
fmt.Printf("Directory '%s' already exists. Skipping.\n", parentDir)
} else {
fmt.Printf("Creating directory '%s' ...\n", parentDirAbs)
if err := os.MkdirAll(parentDirAbs, 0755); err != nil {
fmt.Printf("Creating directory '%s' ...\n", parentDir)
if err := os.MkdirAll(parentDir, 0755); err != nil {
fmt.Printf("Error creating directory: %s\n", err.Error())
os.Exit(1)
}
Expand All @@ -168,7 +166,7 @@ func subcommandInit() {
os.Exit(1)
}
for _, item := range items {
filename := filepath.Join(parentDirAbs, item.Name())
filename := filepath.Join(parentDir, item.Name())
if _, err := os.Stat(filename); err == nil {
fmt.Printf("File '%s' already exists. Skipping.\n", filename)
continue
Expand All @@ -191,6 +189,17 @@ func subcommandInit() {
os.Exit(1)
}
}
magicConfig := filepath.Join(dockerCredentialMagicConfigDirAbs, constants.DockerConfigFileBasename)
if _, err := os.Stat(magicConfig); err == nil {
fmt.Printf("File '%s' already exists. Skipping.\n", magicConfig)
} else {
fmt.Printf("Creating magic config file '%s' ...\n", magicConfig)
err := ioutil.WriteFile(magicConfig, []byte(constants.DockerConfigFileContents), 0644)
if err != nil {
fmt.Printf("Error writing magic config file %s: %s\n", magicConfig, err.Error())
os.Exit(1)
}
}
os.Exit(0)
}

Expand Down

0 comments on commit 8385cb0

Please sign in to comment.