go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/configmeta/meta.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package configmeta 9 10 import ( 11 "context" 12 13 "go.charczuk.com/sdk/configutil" 14 ) 15 16 // These are set with `-ldflags="-X` on `go install` 17 var ( 18 // Version is the current version. 19 Version = "" 20 // GitRef is the currently deployed git ref 21 GitRef = "HEAD" 22 // ServiceName is the name of the service 23 ServiceName = "" 24 // ProjectName is the name of the project the service belongs to 25 ProjectName = "" 26 // ClusterName is the name of the cluster the service is deployed to 27 ClusterName = "" 28 // Region is the region the service is deployed to 29 Region = "" 30 ) 31 32 const ( 33 // DefaultVersion is the default version. 34 DefaultVersion = "HEAD" 35 ) 36 37 // Meta is the cluster config meta. 38 type Meta struct { 39 // Region is the aws region the service is deployed to. 40 Region string `json:"region,omitempty" yaml:"region,omitempty"` 41 // ServiceName is name of the service 42 ServiceName string `json:"serviceName,omitempty" yaml:"serviceName,omitempty"` 43 // ProjectName is the project name injected by Deployinator. 44 ProjectName string `json:"projectName,omitempty" yaml:"projectName,omitempty"` 45 // ClusterName is the name of the cluster the service is deployed to 46 ClusterName string `json:"clusterName,omitempty" yaml:"clusterName,omitempty"` 47 // Environment is the environment of the cluster (sandbox, prod etc.) 48 ServiceEnv string `json:"serviceEnv,omitempty" yaml:"serviceEnv,omitempty"` 49 // Hostname is the environment of the cluster (sandbox, prod etc.) 50 Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty"` 51 // Version is the application version. 52 Version string `json:"version,omitempty" yaml:"version,omitempty"` 53 // GitRef is the git ref of the image. 54 GitRef string `json:"gitRef,omitempty" yaml:"gitRef,omitempty"` 55 } 56 57 // SetFrom returns a resolve action to set this meta from a root meta. 58 func (m *Meta) SetFrom(other *Meta) configutil.ResolveAction { 59 return func(_ context.Context) error { 60 m.Region = other.Region 61 m.ServiceName = other.ServiceName 62 m.ProjectName = other.ProjectName 63 m.ClusterName = other.ClusterName 64 m.ServiceEnv = other.ServiceEnv 65 m.Hostname = other.Hostname 66 m.Version = other.Version 67 m.GitRef = other.GitRef 68 return nil 69 } 70 } 71 72 // ApplyTo applies a given meta to another meta. 73 func (m *Meta) ApplyTo(other *Meta) configutil.ResolveAction { 74 return func(_ context.Context) error { 75 other.Region = m.Region 76 other.ServiceName = m.ServiceName 77 other.ProjectName = m.ProjectName 78 other.ClusterName = m.ClusterName 79 other.ServiceEnv = m.ServiceEnv 80 other.Hostname = m.Hostname 81 other.Version = m.Version 82 other.GitRef = m.GitRef 83 return nil 84 } 85 } 86 87 // Resolve implements configutil.Resolver 88 func (m *Meta) Resolve(ctx context.Context) error { 89 return configutil.Resolve(ctx, 90 configutil.Set(&m.Region, configutil.Env[string](VarRegion), configutil.Lazy(&m.Region), configutil.Lazy(&Region)), 91 configutil.Set(&m.ServiceName, configutil.Env[string](VarServiceName), configutil.Lazy(&m.ServiceName), configutil.Lazy(&ServiceName)), 92 configutil.Set(&m.ProjectName, configutil.Env[string](VarProjectName), configutil.Lazy(&m.ProjectName), configutil.Lazy(&ProjectName)), 93 configutil.Set(&m.ClusterName, configutil.Env[string](VarClusterName), configutil.Lazy(&m.ClusterName), configutil.Lazy(&ClusterName)), 94 configutil.Set(&m.ServiceEnv, configutil.Env[string](VarServiceEnv), configutil.Lazy(&m.ServiceEnv)), 95 configutil.Set(&m.Hostname, configutil.Env[string](VarHostname), configutil.Lazy(&m.Hostname)), 96 configutil.Set(&m.Version, configutil.Env[string](VarVersion), configutil.Lazy(&m.Version), configutil.Lazy(&Version), configutil.Const(DefaultVersion)), 97 configutil.Set(&m.GitRef, configutil.Env[string](VarGitRef), configutil.Lazy(&m.GitRef), configutil.Lazy(&GitRef)), 98 ) 99 } 100 101 // IsProdlike returns if the ServiceEnv is prodlike, that is 102 // an environment where we care about secrets leaking. 103 func (m Meta) IsProdlike() bool { 104 return m.ServiceEnv != EnvDev && m.ServiceEnv != EnvTest 105 }