github.com/git-ogawa/go-dbyml@v1.2.1/dbyml/registry.go (about)

     1  package dbyml
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"fmt"
     7  	"reflect"
     8  
     9  	"github.com/docker/docker/api/types"
    10  )
    11  
    12  // RegistryInfo defines a registry settings where a built image will be pushed.
    13  type RegistryInfo struct {
    14  	// Whether to enabled pushing to a registry
    15  	Enabled bool `yaml:"enabled"`
    16  
    17  	// Registry host such as `myregistry.com:5000`
    18  	Host string `yaml:"host"`
    19  
    20  	// Project name
    21  	Project string `yaml:"project"`
    22  
    23  	// credentials settings to a registry
    24  	Auth map[string]string `yaml:"auth"`
    25  
    26  	Insecure bool `yaml:"insecure"`
    27  }
    28  
    29  // NewRegistryInfo creates a new RegistryInfo struct with default values.
    30  func NewRegistryInfo() *RegistryInfo {
    31  	registry := new(RegistryInfo)
    32  	registry.Enabled = false
    33  	return registry
    34  }
    35  
    36  // ShowProperties shows the current registry settings to stdout.
    37  func (registry RegistryInfo) ShowProperties() {
    38  	rv := reflect.ValueOf(registry)
    39  	rt := rv.Type()
    40  	for i := 0; i < rt.NumField(); i++ {
    41  		field := rt.Field(i)
    42  		value := rv.FieldByName(field.Name)
    43  		if field.Name == "Auth" {
    44  			fmt.Printf("%-30v: *********\n", field.Name)
    45  		} else {
    46  			fmt.Printf("%-30v: %v\n", field.Name, value)
    47  		}
    48  	}
    49  }
    50  
    51  // BasicAuth returns base64 the encoded credentials for the registry.
    52  func (registry *RegistryInfo) BasicAuth() string {
    53  	return GetAuthBase64(registry.Auth["username"], registry.Auth["password"])
    54  }
    55  
    56  // GetAuthBase64 encodes credentials for the registry with base64.
    57  func GetAuthBase64(username string, password string) string {
    58  	auth := types.AuthConfig{
    59  		Username: username,
    60  		Password: password,
    61  	}
    62  	authBytes, _ := json.Marshal(auth)
    63  	return base64.URLEncoding.EncodeToString(authBytes)
    64  }