github.com/drone/runner-go@v1.12.0/registry/file.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 registry
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/drone/drone-go/drone"
    11  	"github.com/drone/runner-go/logger"
    12  	"github.com/drone/runner-go/registry/auths"
    13  )
    14  
    15  // File returns a new registry credential provider that
    16  // parses and returns credentials from the Docker user
    17  // configuration file.
    18  func File(path string) Provider {
    19  	return &file{path}
    20  }
    21  
    22  type file struct {
    23  	path string
    24  }
    25  
    26  func (p *file) List(ctx context.Context, _ *Request) ([]*drone.Registry, error) {
    27  	if p.path == "" {
    28  		return nil, nil
    29  	}
    30  
    31  	logger := logger.FromContext(ctx)
    32  	logger.WithField("path", p.path).
    33  		Trace("registry: file: parsing credentials file")
    34  
    35  	// load the registry credentials from the file.
    36  	res, err := auths.ParseFile(p.path)
    37  	if err != nil {
    38  		logger.WithError(err).
    39  			Debug("registry: file: cannot parse credentials file")
    40  		return nil, err
    41  	}
    42  
    43  	// if no error is returned and the list is empty,
    44  	// this indicates the client returned No Content,
    45  	// and we should exit with no credentials, but no error.
    46  	if len(res) == 0 {
    47  		logger.Trace("registry: file: credential list is empty")
    48  		return nil, nil
    49  	}
    50  
    51  	for _, v := range res {
    52  		logger.
    53  			WithField("address", v.Address).
    54  			WithField("username", v.Username).
    55  			Trace("registry: file: received credentials")
    56  	}
    57  
    58  	return res, err
    59  }