github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/internal/config/from_env.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/cli/cli/internal/ghinstance"
     8  )
     9  
    10  const (
    11  	GH_HOST                 = "GH_HOST"
    12  	GH_TOKEN                = "GH_TOKEN"
    13  	GITHUB_TOKEN            = "GITHUB_TOKEN"
    14  	GH_ENTERPRISE_TOKEN     = "GH_ENTERPRISE_TOKEN"
    15  	GITHUB_ENTERPRISE_TOKEN = "GITHUB_ENTERPRISE_TOKEN"
    16  )
    17  
    18  type ReadOnlyEnvError struct {
    19  	Variable string
    20  }
    21  
    22  func (e *ReadOnlyEnvError) Error() string {
    23  	return fmt.Sprintf("read-only value in %s", e.Variable)
    24  }
    25  
    26  func InheritEnv(c Config) Config {
    27  	return &envConfig{Config: c}
    28  }
    29  
    30  type envConfig struct {
    31  	Config
    32  }
    33  
    34  func (c *envConfig) Hosts() ([]string, error) {
    35  	hasDefault := false
    36  	hosts, err := c.Config.Hosts()
    37  	for _, h := range hosts {
    38  		if h == ghinstance.Default() {
    39  			hasDefault = true
    40  		}
    41  	}
    42  	token, _ := AuthTokenFromEnv(ghinstance.Default())
    43  	if (err != nil || !hasDefault) && token != "" {
    44  		hosts = append([]string{ghinstance.Default()}, hosts...)
    45  		return hosts, nil
    46  	}
    47  	return hosts, err
    48  }
    49  
    50  func (c *envConfig) DefaultHost() (string, error) {
    51  	val, _, err := c.DefaultHostWithSource()
    52  	return val, err
    53  }
    54  
    55  func (c *envConfig) DefaultHostWithSource() (string, string, error) {
    56  	if host := os.Getenv(GH_HOST); host != "" {
    57  		return host, GH_HOST, nil
    58  	}
    59  	return c.Config.DefaultHostWithSource()
    60  }
    61  
    62  func (c *envConfig) Get(hostname, key string) (string, error) {
    63  	val, _, err := c.GetWithSource(hostname, key)
    64  	return val, err
    65  }
    66  
    67  func (c *envConfig) GetWithSource(hostname, key string) (string, string, error) {
    68  	if hostname != "" && key == "oauth_token" {
    69  		if token, env := AuthTokenFromEnv(hostname); token != "" {
    70  			return token, env, nil
    71  		}
    72  	}
    73  
    74  	return c.Config.GetWithSource(hostname, key)
    75  }
    76  
    77  func (c *envConfig) CheckWriteable(hostname, key string) error {
    78  	if hostname != "" && key == "oauth_token" {
    79  		if token, env := AuthTokenFromEnv(hostname); token != "" {
    80  			return &ReadOnlyEnvError{Variable: env}
    81  		}
    82  	}
    83  
    84  	return c.Config.CheckWriteable(hostname, key)
    85  }
    86  
    87  func AuthTokenFromEnv(hostname string) (string, string) {
    88  	if ghinstance.IsEnterprise(hostname) {
    89  		if token := os.Getenv(GH_ENTERPRISE_TOKEN); token != "" {
    90  			return token, GH_ENTERPRISE_TOKEN
    91  		}
    92  
    93  		return os.Getenv(GITHUB_ENTERPRISE_TOKEN), GITHUB_ENTERPRISE_TOKEN
    94  	}
    95  
    96  	if token := os.Getenv(GH_TOKEN); token != "" {
    97  		return token, GH_TOKEN
    98  	}
    99  
   100  	return os.Getenv(GITHUB_TOKEN), GITHUB_TOKEN
   101  }
   102  
   103  func AuthTokenProvidedFromEnv() bool {
   104  	return os.Getenv(GH_ENTERPRISE_TOKEN) != "" ||
   105  		os.Getenv(GITHUB_ENTERPRISE_TOKEN) != "" ||
   106  		os.Getenv(GH_TOKEN) != "" ||
   107  		os.Getenv(GITHUB_TOKEN) != ""
   108  }
   109  
   110  func IsHostEnv(src string) bool {
   111  	return src == GH_HOST
   112  }
   113  
   114  func IsEnterpriseEnv(src string) bool {
   115  	return src == GH_ENTERPRISE_TOKEN || src == GITHUB_ENTERPRISE_TOKEN
   116  }