github.com/drone/runner-go@v1.12.0/registry/auths/encode.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package auths
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  
    11  	"github.com/drone/drone-go/drone"
    12  )
    13  
    14  // Encode encodes the registry credentials to using the
    15  // docker config json format and returns the resulting
    16  // data in string format.
    17  func Encode(registry ...*drone.Registry) string {
    18  	c := new(config)
    19  	c.Auths = map[string]auth{}
    20  	for _, r := range registry {
    21  		c.Auths[r.Address] = auth{
    22  			Auth: encode(r.Username, r.Password),
    23  		}
    24  	}
    25  	buf := new(bytes.Buffer)
    26  	enc := json.NewEncoder(buf)
    27  	enc.Encode(c)
    28  	return buf.String()
    29  }