github.com/bshelton229/agent@v3.5.4+incompatible/bootstrap/config.go (about)

     1  package bootstrap
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/buildkite/agent/env"
     7  )
     8  
     9  // Config provides the configuration for the Bootstrap. Some of the keys are
    10  // read from the environment after hooks are run, so we use struct tags to provide
    11  // that mapping along with some reflection. It's a little bit magical but it's
    12  // less work to maintain in the long run.
    13  //
    14  // To add a new config option that is mapped from an env, add an struct tag and it's done
    15  type Config struct {
    16  	// The command to run
    17  	Command string
    18  
    19  	// The ID of the job being run
    20  	JobID string
    21  
    22  	// If the bootstrap is in debug mode
    23  	Debug bool
    24  
    25  	// The repository that needs to be cloned
    26  	Repository string
    27  
    28  	// The commit being built
    29  	Commit string
    30  
    31  	// The branch of the commit
    32  	Branch string
    33  
    34  	// The tag of the job commit
    35  	Tag string
    36  
    37  	// Optional refspec to override git fetch
    38  	RefSpec string `env:"BUILDKITE_REFSPEC"`
    39  
    40  	// Plugin definition for the job
    41  	Plugins string
    42  
    43  	// Should git submodules be checked out
    44  	GitSubmodules bool
    45  
    46  	// If the commit was part of a pull request, this will container the PR number
    47  	PullRequest string
    48  
    49  	// The provider of the the pipeline
    50  	PipelineProvider string
    51  
    52  	// Slug of the current organization
    53  	OrganizationSlug string
    54  
    55  	// Slug of the current pipeline
    56  	PipelineSlug string
    57  
    58  	// Name of the agent running the bootstrap
    59  	AgentName string
    60  
    61  	// Should the bootstrap remove an existing checkout before running the job
    62  	CleanCheckout bool
    63  
    64  	// Flags to pass to "git clone" command
    65  	GitCloneFlags string `env:"BUILDKITE_GIT_CLONE_FLAGS"`
    66  
    67  	// Flags to pass to "git clean" command
    68  	GitCleanFlags string `env:"BUILDKITE_GIT_CLEAN_FLAGS"`
    69  
    70  	// Whether or not to run the hooks/commands in a PTY
    71  	RunInPty bool
    72  
    73  	// Are aribtary commands allowed to be executed
    74  	CommandEval bool
    75  
    76  	// Are plugins enabled?
    77  	PluginsEnabled bool
    78  
    79  	// Whether to validate plugin configuration
    80  	PluginValidation bool
    81  
    82  	// Are local hooks enabled?
    83  	LocalHooksEnabled bool
    84  
    85  	// Path where the builds will be run
    86  	BuildPath string
    87  
    88  	// Path to the buildkite-agent binary
    89  	BinPath string
    90  
    91  	// Path to the global hooks
    92  	HooksPath string
    93  
    94  	// Path to the plugins directory
    95  	PluginsPath string
    96  
    97  	// Paths to automatically upload as artifacts when the build finishes
    98  	AutomaticArtifactUploadPaths string `env:"BUILDKITE_ARTIFACT_PATHS"`
    99  
   100  	// A custom destination to upload artifacts to (i.e. s3://...)
   101  	ArtifactUploadDestination string `env:"BUILDKITE_ARTIFACT_UPLOAD_DESTINATION"`
   102  
   103  	// Whether ssh-keyscan is run on ssh hosts before checkout
   104  	SSHKeyscan bool
   105  
   106  	// The shell used to execute commands
   107  	Shell string
   108  }
   109  
   110  // ReadFromEnvironment reads configuration from the Environment, returns a map
   111  // of the env keys that changed and the new values
   112  func (c *Config) ReadFromEnvironment(environ *env.Environment) map[string]string {
   113  	changed := map[string]string{}
   114  
   115  	// Use reflection for the type and values
   116  	t := reflect.TypeOf(*c)
   117  	v := reflect.ValueOf(c).Elem()
   118  
   119  	// Iterate over all available fields and read the tag value
   120  	for i := 0; i < t.NumField(); i++ {
   121  		field := t.Field(i)
   122  		value := v.Field(i)
   123  
   124  		// Find struct fields with env tag
   125  		if tag := field.Tag.Get("env"); tag != "" && environ.Exists(tag) {
   126  			newValue, _ := environ.Get(tag)
   127  
   128  			// We only care if the value has changed
   129  			if newValue != value.String() {
   130  				value.SetString(newValue)
   131  				changed[tag] = newValue
   132  			}
   133  		}
   134  	}
   135  
   136  	return changed
   137  }