Skip to content

Commit

Permalink
Merge pull request #2 from samshadwell/update-config
Browse files Browse the repository at this point in the history
Add new config field.
  • Loading branch information
samshadwell authored Nov 18, 2023
2 parents c463833 + b6e734a commit 74b443e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ This integration makes the bookkeeping easier. Our setup is:

## Configuration

!! FIXME !!
Update the config.yml.example, document it

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:
Expand Down
30 changes: 20 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,36 @@ import (
"gopkg.in/yaml.v2"
)

type Config struct {
YnabToken string `yaml:"ynabToken"`
BudgetId string `yaml:"budgetId"`
type config struct {
YnabToken string `yaml:"ynabToken"`
BudgetId string `yaml:"budgetId"`
SplitAccountIds []string `yaml:"splitAccountIds"`
}

func NewConfig() *Config {
func LoadConfig() (*config, error) {
f, err := os.Open("config.yml")
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening config file. Did you create a config.yml?\n\t%v\n", err)
os.Exit(1)
return nil, fmt.Errorf("Error opening config file. Did you create a config.yml?\n\t%w\n", err)
}
defer f.Close()

var cfg Config
decoder := yaml.NewDecoder(f)
var cfg config
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\n\t%v\n", err)
os.Exit(1)
return nil, fmt.Errorf(
"Error decoding config file. Make sure it has the correct format. See config.yml.example for an example\n\t%w\n",
err,
)
}

return &cfg
if len(cfg.YnabToken) == 0 {
return nil, fmt.Errorf("Error: config file is missing ynabToken")
}

if len(cfg.BudgetId) == 0 {
return nil, fmt.Errorf("Error: config file is missing budgetId")
}

return &cfg, nil
}
3 changes: 0 additions & 3 deletions config.yml.example

This file was deleted.

20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ package main

import (
"fmt"
"reflect"
"os"

"github.com/brunomvsouza/ynab.go"
)

func main() {
config := NewConfig()
c := ynab.NewClient(config.YnabToken)
config, err := LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
client := ynab.NewClient(config.YnabToken)

transactions, _ := c.Transaction().GetTransactions(config.BudgetId, nil)
fmt.Println(reflect.TypeOf(transactions))
fmt.Println(len(transactions))
transactions, err := client.Transaction().GetTransactionsByAccount(config.BudgetId, config.SplitAccountIds[0], nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while fetching transactions from YNAB:\n\t%v\n", err)
os.Exit(1)
}

fmt.Println("Server knowledge: ", transactions[0])
}

0 comments on commit 74b443e

Please sign in to comment.