| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package config |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "os" |
| 22 | ) |
| 23 | |
| 24 | // FromEnvironment tries to build a client-only config from environment variables. If none are set then returns nil. |
| 25 | func FromEnvironment() (*Config, error) { |
| 26 | remoteURL := os.Getenv("RELIC_URL") |
| 27 | if remoteURL == "" { |
| 28 | return nil, nil |
| 29 | } |
| 30 | clientCert := os.Getenv("RELIC_CLIENT_CERT") |
| 31 | clientKey := os.Getenv("RELIC_CLIENT_KEY") |
| 32 | accessToken := os.Getenv("RELIC_ACCESS_TOKEN") |
| 33 | if clientCert == "" && accessToken == "" { |
| 34 | return nil, errors.New("RELIC_CLIENT_CERT or RELIC_ACCESS_TOKEN must be set when RELIC_URL is set") |
| 35 | } |
| 36 | if clientKey == "" { |
| 37 | clientKey = clientCert |
| 38 | } |
| 39 | cfg := &Config{ |
| 40 | Remote: &RemoteConfig{ |
| 41 | DirectoryURL: remoteURL, |
| 42 | KeyFile: clientKey, |
| 43 | CertFile: clientCert, |
| 44 | AccessToken: accessToken, |
| 45 | CaCert: os.Getenv("RELIC_CACERT"), |
| 46 | }, |
| 47 | } |
| 48 | return cfg, cfg.Normalize("<env>") |
| 49 | } |