mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-14 05:12:27 +08:00
36 lines
925 B
Go
36 lines
925 B
Go
package docker
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Create the docker config file for authentication
|
|
func CreateDockerCfgFile(username, password, registry, path string) error {
|
|
if username == "" {
|
|
return fmt.Errorf("Username must be specified")
|
|
}
|
|
if password == "" {
|
|
return fmt.Errorf("Password must be specified")
|
|
}
|
|
|
|
err := os.MkdirAll(path, 0600)
|
|
if err != nil {
|
|
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", path))
|
|
}
|
|
|
|
authBytes := []byte(fmt.Sprintf("%s:%s", username, password))
|
|
encodedString := base64.StdEncoding.EncodeToString(authBytes)
|
|
jsonBytes := []byte(fmt.Sprintf(`{"auths": {"%s": {"auth": "%s"}}}`, "https://"+registry, encodedString))
|
|
filePath := path + "/config.json"
|
|
err = ioutil.WriteFile(filePath, jsonBytes, 0644)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to create docker config file")
|
|
}
|
|
return nil
|
|
}
|