From f5658a1de1f967d0706d08b1f4cd091fcf2f5692 Mon Sep 17 00:00:00 2001 From: Thomas Schwery Date: Mon, 6 Feb 2017 18:05:23 +0100 Subject: [PATCH] Use of a configuration file to store the client and secret keys --- configuration-example.toml | 3 +++ main.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 configuration-example.toml diff --git a/configuration-example.toml b/configuration-example.toml new file mode 100644 index 0000000..e040dc7 --- /dev/null +++ b/configuration-example.toml @@ -0,0 +1,3 @@ +# Get a key on https://developers.eveonline.com/applications +ClientID = "SAMPLE_CLIENTID" +ClientSecret = "SAMPLE_CLIENTSECRET" diff --git a/main.go b/main.go index b822e31..a227780 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "os" "strconv" + "github.com/BurntSushi/toml" "github.com/go-openapi/strfmt" "github.com/leekchan/accounting" "github.com/logrusorgru/aurora" @@ -41,6 +42,11 @@ type Character struct { ExpiresOn string } +type configurationFile struct { + ClientID string + ClientSecret string +} + var ( googleOauthConfig = &oauth2.Config{ RedirectURL: "http://localhost:3000/callback", @@ -74,6 +80,12 @@ func main() { log.SetOutput(f) + cErr := readConfigurationFile() + if cErr != nil { + fmt.Println("Missing configuration file.") + log.Fatal(cErr) + } + cToken := getDatabaseToken() if cToken == nil { @@ -104,6 +116,25 @@ func main() { printCharacterSkillQueue(swaggerclient, m) } +func readConfigurationFile() error { + cfgFilePath := "configuration.toml" + var config configurationFile + if _, err := toml.DecodeFile(cfgFilePath, &config); err != nil { + log.Println(err) + return err + } + + if config.ClientID == "" || config.ClientSecret == "" { + log.Println("Missing ClientID or ClientSecret configuration option in configuration file" + cfgFilePath + ".") + return errors.New("Missing ClientID or ClientSecret in configuration file " + cfgFilePath + ".") + } + + googleOauthConfig.ClientID = config.ClientID + googleOauthConfig.ClientSecret = config.ClientSecret + + return nil +} + func getCharacterInfo(client *http.Client) (*Character, error) { req, _ := http.NewRequest("GET", "https://login.eveonline.com/oauth/verify", nil)