github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/config/context.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"path/filepath"
     7  	"strings"
     8  )
     9  
    10  // Context structure
    11  type Context struct {
    12  	config  *Config
    13  	context *ContextParams
    14  }
    15  
    16  // NewContext with received params and uninitialized configuration
    17  func NewContext(params ContextParams) Context {
    18  	return Context{
    19  		context: &ContextParams{},
    20  		config:  &Config{},
    21  	}
    22  }
    23  
    24  // ContextParams is the set of environment configurations
    25  type ContextParams struct {
    26  	Remote               string
    27  	Infrastructure       string
    28  	InfrastructureDomain string
    29  	ServiceDomain        string
    30  	Username             string
    31  	Token                string
    32  }
    33  
    34  // Remote used on the context
    35  func (c *Context) Remote() string {
    36  	return c.context.Remote
    37  }
    38  
    39  // Infrastructure used on the context
    40  func (c *Context) Infrastructure() string {
    41  	infra := c.context.Infrastructure
    42  
    43  	if infra == "localhost" || strings.HasPrefix(infra, "localhost:") {
    44  		return "http://" + infra
    45  	}
    46  
    47  	return infra
    48  }
    49  
    50  // InfrastructureDomain used on the context
    51  func (c *Context) InfrastructureDomain() string {
    52  	return c.context.InfrastructureDomain
    53  }
    54  
    55  // ServiceDomain used on the context
    56  func (c *Context) ServiceDomain() string {
    57  	return c.context.ServiceDomain
    58  }
    59  
    60  // Username used on the context
    61  func (c *Context) Username() string {
    62  	return c.context.Username
    63  }
    64  
    65  // Token used on the context
    66  func (c *Context) Token() string {
    67  	return c.context.Token
    68  }
    69  
    70  // Config gets the configuration
    71  func (c *Context) Config() *Config {
    72  	return c.config
    73  }
    74  
    75  // SetEndpoint for the context
    76  func (c *Context) SetEndpoint(remote string) error {
    77  	var conf = c.Config()
    78  	var params = conf.GetParams()
    79  	var rl = params.Remotes
    80  
    81  	if !rl.Has(remote) {
    82  		return fmt.Errorf(`error loading selected remote "%v"`, remote)
    83  	}
    84  
    85  	var r = rl.Get(remote)
    86  
    87  	c.context.Remote = remote
    88  	c.context.Infrastructure = r.InfrastructureServer()
    89  	c.context.InfrastructureDomain = getRemoteAddress(r.Infrastructure)
    90  	c.context.ServiceDomain = r.Service
    91  	c.context.Username = r.Username
    92  	c.context.Token = r.Token
    93  	return nil
    94  }
    95  
    96  // Setup the environment
    97  func Setup(path string) (wectx Context, err error) {
    98  	wectx = NewContext(ContextParams{})
    99  	path, err = filepath.Abs(path)
   100  
   101  	if err != nil {
   102  		return wectx, err
   103  	}
   104  
   105  	var c = &Config{
   106  		Path: path,
   107  	}
   108  
   109  	if err = c.Load(); err != nil {
   110  		return wectx, err
   111  	}
   112  
   113  	wectx.config = c
   114  	return wectx, nil
   115  }
   116  
   117  func getRemoteAddress(address string) string {
   118  	if strings.HasPrefix(address, "https://api.") {
   119  		address = strings.TrimPrefix(address, "https://api.")
   120  	}
   121  
   122  	var h, _, err = net.SplitHostPort(address)
   123  
   124  	if err != nil {
   125  		return address
   126  	}
   127  
   128  	return h
   129  }