From 9a4e1ba483fbc871d43a13f10ac147c9e2fadc7e Mon Sep 17 00:00:00 2001 From: Aishwarya Lad <67022814+Aishwarya-Lad@users.noreply.github.com> Date: Tue, 25 Jun 2024 15:04:24 -0700 Subject: [PATCH] address review comments --- cmd/drone-docker/main.go | 6 +++--- docker.go | 10 +++++----- internal/docker/config.go | 12 +++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 2cd9275..2e5d649 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -225,17 +225,17 @@ func main() { cli.StringFlag{ Name: "docker.baseimageusername", Usage: "Docker username for base image registry", - EnvVar: "PLUGIN_DOCKER_USERNAME,PLUGIN_BASE_IMAGE_USERNAME", + EnvVar: "PLUGIN_DOCKER_USERNAME,PLUGIN_BASE_IMAGE_USERNAME,DOCKER_BASE_IMAGE_USERNAME", }, cli.StringFlag{ Name: "docker.baseimagepassword", Usage: "Docker password for base image registry", - EnvVar: "PLUGIN_DOCKER_PASSWORD,PLUGIN_BASE_IMAGE_PASSWORD", + EnvVar: "PLUGIN_DOCKER_PASSWORD,PLUGIN_BASE_IMAGE_PASSWORD,DOCKER_BASE_IMAGE_PASSWORD", }, cli.StringFlag{ Name: "docker.baseimageregistry", Usage: "Docker registry for base image registry", - EnvVar: "PLUGIN_DOCKER_REGISTRY,PLUGIN_BASE_IMAGE_REGISTRY", + EnvVar: "PLUGIN_DOCKER_REGISTRY,PLUGIN_BASE_IMAGE_REGISTRY,DOCKER_BASE_IMAGE_REGISTRY", }, cli.StringFlag{ Name: "docker.email", diff --git a/docker.go b/docker.go index db6c9b8..bae3d20 100644 --- a/docker.go +++ b/docker.go @@ -1,6 +1,7 @@ package docker import ( + "errors" "fmt" "os" "os/exec" @@ -11,7 +12,6 @@ import ( "github.com/drone-plugins/drone-docker/internal/docker" "github.com/drone-plugins/drone-plugin-lib/drone" - "github.com/pkg/errors" ) type ( @@ -163,7 +163,7 @@ func (p Plugin) Exec() error { return fmt.Errorf("Error writing config.json: %s", err) } _, err = file.Write([]byte(p.Login.Config)) - fmt.Println("Writing p.Login.Config: %s", p.Login.Config) + fmt.Println("Writing p.Login.Config") if err != nil { return fmt.Errorf("Error writing config.json: %s", err) @@ -175,7 +175,7 @@ func (p Plugin) Exec() error { json, err := setDockerAuth(p.Login.Username, p.Login.Password, p.Login.Registry, p.BaseImageUsername, p.BaseImagePassword, p.BaseImageRegistry) if err != nil { - return errors.Wrap(err, "Failed to set authentication in docker config") + return fmt.Errorf("Failed to set authentication in docker config %s", err) } if json != nil { path := filepath.Join(dockerHome, "config.json") @@ -306,15 +306,15 @@ func setDockerAuth(username, password, registry, baseImageUsername, baseImagePassword, baseImageRegistry string) ([]byte, error) { var credentials []docker.RegistryCredentials // add only docker registry to the config + dockerConfig := docker.NewConfig() if password != "" && strings.Contains(registry, "docker") { - dockerConfig := docker.NewConfig() pushToRegistryCreds := docker.RegistryCredentials{ Registry: registry, Username: username, Password: password, } // push registry auth - credentials := append(credentials, pushToRegistryCreds) + credentials = append(credentials, pushToRegistryCreds) } if baseImageRegistry != "" { diff --git a/internal/docker/config.go b/internal/docker/config.go index 32362f7..500d9f8 100644 --- a/internal/docker/config.go +++ b/internal/docker/config.go @@ -3,11 +3,11 @@ package docker import ( "encoding/base64" "encoding/json" + "errors" "fmt" "io/ioutil" "os" - - "github.com/pkg/errors" + "strings" ) const ( @@ -43,7 +43,6 @@ func NewConfig() *Config { func (c *Config) SetAuth(registry, username, password string) { authBytes := []byte(username + ":" + password) encodedString := base64.StdEncoding.EncodeToString(authBytes) - log.Printf("auth : %s", encodedString) c.Auths[registry] = Auth{Auth: encodedString} } @@ -67,7 +66,7 @@ func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]by jsonBytes, err := json.Marshal(c) if err != nil { - return nil, errors.Wrap(err, "failed to serialize docker config json") + return nil, errors.New("failed to serialize docker config json") } return jsonBytes, nil @@ -77,15 +76,14 @@ func WriteDockerConfig(data []byte, path string) (string error) { err := os.MkdirAll(path, 0600) if err != nil { if !os.IsExist(err) { - return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", path)) + return errors.New("failed to create %s directory") } } filePath := path + "/config.json" - log.Printf("Config data is %s", data) err = ioutil.WriteFile(filePath, data, 0644) if err != nil { - return errors.Wrap(err, fmt.Sprintf("failed to create docker config file at %s", path)) + return errors.New("failed to create docker config file at %s") } return nil }