github.com/blend/go-sdk@v1.20220411.3/configmeta/meta.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package configmeta
     9  
    10  import (
    11  	"context"
    12  
    13  	"github.com/blend/go-sdk/configutil"
    14  	"github.com/blend/go-sdk/env"
    15  )
    16  
    17  // These are set with `-ldflags="-X` on `go install`
    18  var (
    19  	// Version is the current version.
    20  	Version = ""
    21  	// GitRef is the currently deployed git ref
    22  	GitRef = "HEAD"
    23  	// ServiceName is the name of the service
    24  	ServiceName = ""
    25  	// ProjectName is the name of the project the service belongs to
    26  	ProjectName = ""
    27  	// ClusterName is the name of the cluster the service is deployed to
    28  	ClusterName = ""
    29  	// Region is the region the service is deployed to
    30  	Region = ""
    31  )
    32  
    33  // Meta is the cluster config meta.
    34  type Meta struct {
    35  	// Region is the aws region the service is deployed to.
    36  	Region string `json:"region,omitempty" yaml:"region,omitempty"`
    37  	// ServiceName is name of the service
    38  	ServiceName string `json:"serviceName,omitempty" yaml:"serviceName,omitempty"`
    39  	// ProjectName is the project name injected by Deployinator.
    40  	ProjectName string `json:"projectName,omitempty" yaml:"projectName,omitempty"`
    41  	// ClusterName is the name of the cluster the service is deployed to
    42  	ClusterName string `json:"clusterName,omitempty" yaml:"clusterName,omitempty"`
    43  	// Environment is the environment of the cluster (sandbox, prod etc.)
    44  	ServiceEnv string `json:"serviceEnv,omitempty" yaml:"serviceEnv,omitempty"`
    45  	// Hostname is the environment of the cluster (sandbox, prod etc.)
    46  	Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty"`
    47  	// Version is the application version.
    48  	Version string `json:"version,omitempty" yaml:"version,omitempty"`
    49  	// GitRef is the git ref of the image.
    50  	GitRef string `json:"gitRef,omitempty" yaml:"gitRef,omitempty"`
    51  }
    52  
    53  // SetFrom returns a resolve action to set this meta from a root meta.
    54  func (m *Meta) SetFrom(other *Meta) configutil.ResolveAction {
    55  	return func(_ context.Context) error {
    56  		m.Region = other.Region
    57  		m.ServiceName = other.ServiceName
    58  		m.ProjectName = other.ProjectName
    59  		m.ClusterName = other.ClusterName
    60  		m.ServiceEnv = other.ServiceEnv
    61  		m.Hostname = other.Hostname
    62  		m.Version = other.Version
    63  		m.GitRef = other.GitRef
    64  		return nil
    65  	}
    66  }
    67  
    68  // ApplyTo applies a given meta to another meta.
    69  func (m *Meta) ApplyTo(other *Meta) configutil.ResolveAction {
    70  	return func(_ context.Context) error {
    71  		other.Region = m.Region
    72  		other.ServiceName = m.ServiceName
    73  		other.ProjectName = m.ProjectName
    74  		other.ClusterName = m.ClusterName
    75  		other.ServiceEnv = m.ServiceEnv
    76  		other.Hostname = m.Hostname
    77  		other.Version = m.Version
    78  		other.GitRef = m.GitRef
    79  		return nil
    80  	}
    81  }
    82  
    83  // Resolve implements configutil.Resolver
    84  func (m *Meta) Resolve(ctx context.Context) error {
    85  	return configutil.Resolve(ctx,
    86  		configutil.SetString(&m.Region, configutil.Env(env.VarRegion), configutil.String(m.Region), configutil.LazyString(&Region)),
    87  		configutil.SetString(&m.ServiceName, configutil.Env(env.VarServiceName), configutil.String(m.ServiceName), configutil.LazyString(&ServiceName)),
    88  		configutil.SetString(&m.ProjectName, configutil.Env(env.VarProjectName), configutil.String(m.ProjectName), configutil.LazyString(&ProjectName)),
    89  		configutil.SetString(&m.ClusterName, configutil.Env(env.VarClusterName), configutil.String(m.ClusterName), configutil.LazyString(&ClusterName)),
    90  		configutil.SetString(&m.ServiceEnv, configutil.Env(env.VarServiceEnv), configutil.String(m.ServiceEnv), configutil.String(env.ServiceEnvDev)),
    91  		configutil.SetString(&m.Hostname, configutil.Env(env.VarHostname), configutil.String(m.Hostname)),
    92  		configutil.SetString(&m.Version, configutil.Env(env.VarVersion), configutil.String(m.Version), configutil.LazyString(&Version), configutil.String(DefaultVersion)),
    93  		configutil.SetString(&m.GitRef, configutil.Env(env.VarGitRef), configutil.String(m.GitRef), configutil.LazyString(&GitRef)),
    94  	)
    95  }
    96  
    97  // RegionOrDefault returns the region or the default.
    98  func (m Meta) RegionOrDefault() string {
    99  	if m.Region != "" {
   100  		return m.Region
   101  	}
   102  	return Region
   103  }
   104  
   105  // ServiceNameOrDefault returns the service name or the default.
   106  func (m Meta) ServiceNameOrDefault() string {
   107  	if m.ServiceName != "" {
   108  		return m.ServiceName
   109  	}
   110  	return ServiceName
   111  }
   112  
   113  // ProjectNameOrDefault returns the project name or the default.
   114  func (m Meta) ProjectNameOrDefault() string {
   115  	if m.ProjectName != "" {
   116  		return m.ProjectName
   117  	}
   118  	return ProjectName
   119  }
   120  
   121  // ClusterNameOrDefault returns the cluster name or the default.
   122  func (m Meta) ClusterNameOrDefault() string {
   123  	if m.ClusterName != "" {
   124  		return m.ClusterName
   125  	}
   126  	return ClusterName
   127  }
   128  
   129  // ServiceEnvOrDefault returns the cluster environment.
   130  func (m Meta) ServiceEnvOrDefault() string {
   131  	if m.ServiceEnv != "" {
   132  		return m.ServiceEnv
   133  	}
   134  	return env.DefaultServiceEnv
   135  }
   136  
   137  // VersionOrDefault returns a version or a default.
   138  func (m Meta) VersionOrDefault() string {
   139  	if m.Version != "" {
   140  		return m.Version
   141  	}
   142  	if Version != "" {
   143  		return Version
   144  	}
   145  	return DefaultVersion
   146  }
   147  
   148  // GitRefOrDefault returns a gitref or a default.
   149  func (m Meta) GitRefOrDefault() string {
   150  	if m.GitRef != "" {
   151  		return m.GitRef
   152  	}
   153  	if GitRef != "" {
   154  		return GitRef
   155  	}
   156  	return "HEAD"
   157  }
   158  
   159  // IsProdlike returns if the cluster meta environment is prodlike.
   160  func (m Meta) IsProdlike() bool {
   161  	return env.IsProdlike(m.ServiceEnvOrDefault())
   162  }