|
| 1 | +// Copyright 2022 Palantir Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package handler |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "strconv" |
| 20 | + |
| 21 | + "github.com/palantir/bulldozer/bulldozer" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + DefaultSharedRepository = ".github" |
| 26 | + DefaultConfigurationPath = ".bulldozer.yml" |
| 27 | + DefaultSharedConfigurationPath = "bulldozer.yml" |
| 28 | + DefaultAppName = "bulldozer" |
| 29 | +) |
| 30 | + |
| 31 | +type Options struct { |
| 32 | + AppName string `yaml:"app_name"` |
| 33 | + PushRestrictionUserToken string `yaml:"push_restriction_user_token"` |
| 34 | + |
| 35 | + ConfigurationPath string `yaml:"configuration_path"` |
| 36 | + SharedRepository string `yaml:"shared_repository"` |
| 37 | + SharedConfigurationPath string `yaml:"shared_configuration_path"` |
| 38 | + DefaultRepositoryConfig *bulldozer.Config `yaml:"default_repository_config"` |
| 39 | + |
| 40 | + ConfigurationV0Paths []string `yaml:"configuration_v0_paths"` |
| 41 | + |
| 42 | + DisableUpdateFeature bool `yaml:"disable_update_feature"` |
| 43 | +} |
| 44 | + |
| 45 | +func (o *Options) fillDefaults() { |
| 46 | + if o.ConfigurationPath == "" { |
| 47 | + o.ConfigurationPath = DefaultConfigurationPath |
| 48 | + } |
| 49 | + if o.AppName == "" { |
| 50 | + o.AppName = DefaultAppName |
| 51 | + } |
| 52 | + if o.SharedRepository == "" { |
| 53 | + o.SharedRepository = DefaultSharedRepository |
| 54 | + } |
| 55 | + if o.SharedConfigurationPath == "" { |
| 56 | + o.SharedConfigurationPath = DefaultSharedConfigurationPath |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (o *Options) SetValuesFromEnv(prefix string) { |
| 61 | + setStringFromEnv("CONFIGURATION_PATH", prefix, &o.ConfigurationPath) |
| 62 | + setStringFromEnv("APP_NAME", prefix, &o.AppName) |
| 63 | + setStringFromEnv("SHARED_REPOSITORY", prefix, &o.SharedRepository) |
| 64 | + setStringFromEnv("SHARED_CONFIGURATION_PATH", prefix, &o.SharedConfigurationPath) |
| 65 | + setBooleanFromEnv("DISABLE_UPDATE_FEATURE", prefix, &o.DisableUpdateFeature) |
| 66 | + setStringFromEnv("PUSH_RESTRICTION_USER_TOKEN", prefix, &o.PushRestrictionUserToken) |
| 67 | + o.fillDefaults() |
| 68 | +} |
| 69 | + |
| 70 | +func setStringFromEnv(key, prefix string, value *string) bool { |
| 71 | + if v, ok := os.LookupEnv(prefix + key); ok { |
| 72 | + *value = v |
| 73 | + return true |
| 74 | + } |
| 75 | + return false |
| 76 | +} |
| 77 | + |
| 78 | +func setBooleanFromEnv(key, prefix string, value *bool) bool { |
| 79 | + if v, ok := os.LookupEnv(prefix + key); ok { |
| 80 | + *value, _ = strconv.ParseBool(v) |
| 81 | + return true |
| 82 | + } |
| 83 | + return false |
| 84 | +} |
0 commit comments