■ ■ ■ ■ ■ ■ ■
cron/internal/config/config.go
| skipped 18 lines |
19 | 19 | | // Used to embed config.yaml. |
20 | 20 | | _ "embed" |
21 | 21 | | "errors" |
| 22 | + | "flag" |
22 | 23 | | "fmt" |
23 | 24 | | "os" |
24 | 25 | | "reflect" |
| skipped 10 lines |
35 | 36 | | ShardNumFilename string = ".shard_num" |
36 | 37 | | // TransferStatusFilename file identifies if shard transfer to BigQuery is completed. |
37 | 38 | | TransferStatusFilename string = ".transfer_complete" |
| 39 | + | |
| 40 | + | configFlag string = "config" |
| 41 | + | configDefault string = "" |
| 42 | + | configUsage string = "Location of config file. Required" |
| 43 | + | |
38 | 44 | | projectID string = "SCORECARD_PROJECT_ID" |
39 | 45 | | requestTopicURL string = "SCORECARD_REQUEST_TOPIC_URL" |
40 | 46 | | requestSubscriptionURL string = "SCORECARD_REQUEST_SUBSCRIPTION_URL" |
| skipped 14 lines |
55 | 61 | | ErrorEmptyConfigValue = errors.New("config value set to empty") |
56 | 62 | | // ErrorValueConversion indicates an unexpected type was found for the value of the config option. |
57 | 63 | | ErrorValueConversion = errors.New("unexpected type, cannot convert value") |
| 64 | + | // ErrorNoConfig indicates no config file was provided, or flag.Parse() was not called. |
| 65 | + | ErrorNoConfig = errors.New("no configuration file provided with --" + configFlag) |
58 | 66 | | //go:embed config.yaml |
59 | | - | configYAML []byte |
| 67 | + | configYAML []byte |
| 68 | + | configFilename = flag.String(configFlag, configDefault, configUsage) |
60 | 69 | | ) |
61 | 70 | | |
62 | 71 | | //nolint:govet |
| skipped 130 lines |
193 | 202 | | return "", err |
194 | 203 | | } |
195 | 204 | | return s[key], nil |
| 205 | + | } |
| 206 | + | |
| 207 | + | // ReadConfig reads the contents of a configuration file specified with --config for later use by getters. |
| 208 | + | // This function must be called before any other exported function, and after flag.Parse() is called. |
| 209 | + | func ReadConfig() error { |
| 210 | + | var err error |
| 211 | + | if configFilename == nil || *configFilename == "" { |
| 212 | + | return nil |
| 213 | + | } |
| 214 | + | configYAML, err = os.ReadFile(*configFilename) |
| 215 | + | if err != nil { |
| 216 | + | return fmt.Errorf("config file: %w", err) |
| 217 | + | } |
| 218 | + | return nil |
196 | 219 | | } |
197 | 220 | | |
198 | 221 | | // GetProjectID returns the cloud projectID for the cron job. |
| skipped 107 lines |