github.com/jchengjr77/canaveral@v1.0.1-0.20200715160102-ea9245d1a2fb/nativestore/credentials.go (about)

     1  // Package nativestore implementation taken from
     2  // https://flowerinthenight.com/blog/2017/10/30/nativestore
     3  package nativestore
     4  
     5  import (
     6  	"os"
     7  
     8  	"github.com/docker/docker-credential-helpers/credentials"
     9  )
    10  
    11  // SetCreds sets creentials for the user in their native storage.
    12  func SetCreds(label, url, username, secret string) error {
    13  	creds := credentials.Credentials{
    14  		ServerURL: url,
    15  		Username:  username,
    16  		Secret:    secret,
    17  	}
    18  	credentials.SetCredsLabel(label)
    19  	if os.Getenv("CredentialsTest") == "true" {
    20  		return mockStore.Add(&creds)
    21  	}
    22  	return store.Add(&creds)
    23  }
    24  
    25  // FetchCreds gets user credentials from the native storage, looking up by URL.
    26  func FetchCreds(label, url string) (string, string, error) {
    27  	credentials.SetCredsLabel(label)
    28  	if os.Getenv("CredentialsTest") == "true" {
    29  		return mockStore.Get(url)
    30  	}
    31  	return store.Get(url)
    32  }
    33  
    34  // DeleteCreds deletes credentials from the native storage, looking up by URL.
    35  func DeleteCreds(label, url string) error {
    36  	credentials.SetCredsLabel(label)
    37  	if os.Getenv("CredentialsTest") == "true" {
    38  		return mockStore.Delete(url)
    39  	}
    40  	return store.Delete(url)
    41  }