...

Source file src/github.com/palantir/go-githubapp/githubapp/config.go

Documentation: github.com/palantir/go-githubapp/githubapp

     1  // Copyright 2018 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 githubapp
    16  
    17  import (
    18  	"os"
    19  	"strconv"
    20  )
    21  
    22  type Config struct {
    23  	WebURL   string `yaml:"web_url" json:"webUrl"`
    24  	V3APIURL string `yaml:"v3_api_url" json:"v3ApiUrl"`
    25  	V4APIURL string `yaml:"v4_api_url" json:"v4ApiUrl"`
    26  
    27  	App struct {
    28  		IntegrationID int64  `yaml:"integration_id" json:"integrationId"`
    29  		WebhookSecret string `yaml:"webhook_secret" json:"webhookSecret"`
    30  		PrivateKey    string `yaml:"private_key" json:"privateKey"`
    31  	} `yaml:"app" json:"app"`
    32  
    33  	OAuth struct {
    34  		ClientID     string `yaml:"client_id" json:"clientId"`
    35  		ClientSecret string `yaml:"client_secret" json:"clientSecret"`
    36  	} `yaml:"oauth" json:"oauth"`
    37  }
    38  
    39  // SetValuesFromEnv sets values in the configuration from coresponding
    40  // environment variables, if they exist. The optional prefix is added to the
    41  // start of the environment variable names.
    42  func (c *Config) SetValuesFromEnv(prefix string) {
    43  	setStringFromEnv("GITHUB_WEB_URL", prefix, &c.WebURL)
    44  	setStringFromEnv("GITHUB_V3_API_URL", prefix, &c.V3APIURL)
    45  	setStringFromEnv("GITHUB_V4_API_URL", prefix, &c.V4APIURL)
    46  
    47  	setIntFromEnv("GITHUB_APP_INTEGRATION_ID", prefix, &c.App.IntegrationID)
    48  	setStringFromEnv("GITHUB_APP_WEBHOOK_SECRET", prefix, &c.App.WebhookSecret)
    49  	setStringFromEnv("GITHUB_APP_PRIVATE_KEY", prefix, &c.App.PrivateKey)
    50  
    51  	setStringFromEnv("GITHUB_OAUTH_CLIENT_ID", prefix, &c.OAuth.ClientID)
    52  	setStringFromEnv("GITHUB_OAUTH_CLIENT_SECRET", prefix, &c.OAuth.ClientSecret)
    53  }
    54  
    55  func setStringFromEnv(key, prefix string, value *string) {
    56  	if v, ok := os.LookupEnv(prefix + key); ok {
    57  		*value = v
    58  	}
    59  }
    60  
    61  func setIntFromEnv(key, prefix string, value *int64) {
    62  	if v, ok := os.LookupEnv(prefix + key); ok {
    63  		if i, err := strconv.ParseInt(v, 10, 0); err == nil {
    64  			*value = i
    65  		}
    66  	}
    67  }
    68  

View as plain text