Skip to content

Commit 74d5558

Browse files
committed
resolve comments
1 parent 5639b70 commit 74d5558

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

docker.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,36 +158,30 @@ func (p Plugin) Exec() error {
158158
os.MkdirAll(dockerHome, 0600)
159159

160160
path := filepath.Join(dockerHome, "config.json")
161-
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
162-
if err != nil {
163-
return fmt.Errorf("Error writing config.json: %s", err)
164-
}
165-
_, err = file.Write([]byte(p.Login.Config))
166-
fmt.Println("Writing p.Login.Config")
167-
161+
err := os.WriteFile(path, []byte(p.Login.Config), 0600)
168162
if err != nil {
169163
return fmt.Errorf("Error writing config.json: %s", err)
170164
}
171-
defer file.Close()
165+
file.Close()
172166
}
167+
173168
// add base image docker credentials to the existing config file, else create new
174169
if p.BaseImagePassword != "" {
175170
json, err := setDockerAuth(p.Login.Username, p.Login.Password, p.Login.Registry,
176171
p.BaseImageUsername, p.BaseImagePassword, p.BaseImageRegistry)
177172
if err != nil {
178173
return fmt.Errorf("Failed to set authentication in docker config %s", err)
179174
}
180-
if json != nil {
181-
path := filepath.Join(dockerHome, "config.json")
182-
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
183-
if err != nil {
184-
return fmt.Errorf("Error opening config.json: %s", err)
185-
}
186-
defer file.Close()
187-
_, err = file.Write(json)
188-
if err != nil {
189-
return fmt.Errorf("Error writing config.json: %s", err)
190-
}
175+
os.MkdirAll(dockerHome, 0600)
176+
path := filepath.Join(dockerHome, "config.json")
177+
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
178+
if err != nil {
179+
return fmt.Errorf("Error opening config.json: %s", err)
180+
}
181+
defer file.Close()
182+
_, err = file.Write(json)
183+
if err != nil {
184+
return fmt.Errorf("Error writing config.json: %s", err)
191185
}
192186
}
193187

@@ -307,7 +301,7 @@ func setDockerAuth(username, password, registry, baseImageUsername,
307301
var credentials []docker.RegistryCredentials
308302
// add only docker registry to the config
309303
dockerConfig := docker.NewConfig()
310-
if password != "" && strings.Contains(registry, "docker") {
304+
if password != "" {
311305
pushToRegistryCreds := docker.RegistryCredentials{
312306
Registry: registry,
313307
Username: username,

internal/docker/config.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"io/ioutil"
9-
"os"
108
"strings"
119
)
1210

@@ -71,19 +69,3 @@ func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]by
7169

7270
return jsonBytes, nil
7371
}
74-
75-
func WriteDockerConfig(data []byte, path string) (string error) {
76-
err := os.MkdirAll(path, 0600)
77-
if err != nil {
78-
if !os.IsExist(err) {
79-
return errors.New("failed to create %s directory")
80-
}
81-
}
82-
83-
filePath := path + "/config.json"
84-
err = ioutil.WriteFile(filePath, data, 0644)
85-
if err != nil {
86-
return errors.New("failed to create docker config file at %s")
87-
}
88-
return nil
89-
}

0 commit comments

Comments
 (0)