From c81b6b4e478a29f11cd45a40d4645cbb7aa7dbc2 Mon Sep 17 00:00:00 2001 From: samshadwell Date: Thu, 16 Nov 2023 23:22:08 -0500 Subject: [PATCH] Switch to yml-based config. --- .gitignore | 2 +- README.md | 8 ++++---- config.go | 31 +++++++++++++++++++++++++++++++ config.go.example | 15 --------------- config.yml.example | 3 +++ go.mod | 5 ++++- go.sum | 3 +++ 7 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 config.go delete mode 100644 config.go.example create mode 100644 config.yml.example diff --git a/.gitignore b/.gitignore index 171acde..28d935b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,4 @@ go.work # Sensitive config -config.go \ No newline at end of file +config.yml \ No newline at end of file diff --git a/README.md b/README.md index 6c345cd..ff6ec4f 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ This integration makes the bookkeeping easier. Our setup is: ## Configuration -Configuration is assumed to live in a `config.go` file at the root of the application. For security reasons, I don't -check this file into Git, but you can see the general structure in `config.go.example`. To use this yourself, first +Configuration is assumed to live in a `config.yml` file at the root of the application. For security reasons, I don't +check this file into Git, but you can see the general structure in `config.yml.example`. To use this yourself, first run: ```shell -cp config.go.example config.go +cp config.yml.example config.yml ``` -Then fill in the values in `config.go` with your own values. +Then fill in the values in `config.yml` with your own values. diff --git a/config.go b/config.go new file mode 100644 index 0000000..d9c089d --- /dev/null +++ b/config.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "os" + + "gopkg.in/yaml.v2" +) + +type Config struct { + YnabToken string `yaml:"ynabToken"` + BudgetId string `yaml:"budgetId"` +} + +func NewConfig() *Config { + f, err := os.Open("config.yml") + if err != nil { + fmt.Fprintf(os.Stderr, "Error opening config file. Did you create a config.yml? %v\n", err) + os.Exit(1) + } + defer f.Close() + + var cfg Config + decoder := yaml.NewDecoder(f) + err = decoder.Decode(&cfg) + if err != nil { + fmt.Fprintf(os.Stderr, "Error decoding config file. Make sure it has the correct format. See config.yml.example for an example %v\n", err) + } + + return &cfg +} diff --git a/config.go.example b/config.go.example deleted file mode 100644 index 16a4593..0000000 --- a/config.go.example +++ /dev/null @@ -1,15 +0,0 @@ -package main - -type Config struct { - YnabToken string - BudgetId string -} - -// FIXME: Update these to be relevant to your YNAB account. -func NewConfig() *Config { - return &Config{ - // See https://api.ynab.com/#authentication-overview for how to obtain a token. - YnabToken: "some-ynab-token", - BudgetId: "budget-id-123", - } -} diff --git a/config.yml.example b/config.yml.example new file mode 100644 index 0000000..efe8c50 --- /dev/null +++ b/config.yml.example @@ -0,0 +1,3 @@ +--- +ynabToken: "xxx" +budgetId: "yyy" diff --git a/go.mod b/go.mod index cdecb63..fd2c258 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/samshadwell/split-ynab go 1.21 -require github.com/brunomvsouza/ynab.go v1.4.0 // indirect +require ( + github.com/brunomvsouza/ynab.go v1.4.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum index 1c3b73b..4c86fe5 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,5 @@ github.com/brunomvsouza/ynab.go v1.4.0 h1:j32NsAq74sxWtfi16cFrn/aj2K8sZpaXPlcJiB9bwRk= github.com/brunomvsouza/ynab.go v1.4.0/go.mod h1:u5zDi6NY53RIqel+hzVodPr0CuZ0ZONbf03cex+kods= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=