summaryrefslogtreecommitdiff
path: root/config.go
blob: d7cd3e43cf20ed9db5f0ade5f8ce19b87252f531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package planr

import (
  "github.com/BurntSushi/toml"
  "log"
  "path"
  "strings"
)

type Config struct {
  Version      string
}

const PLANR_CONFIG_FILE = "config.toml"

// TODO: REMOVE
const STRICTLY_REQUIRE_CONFIG = false

func DecodeConfig(configDir string) *Config {
  cfg := new(Config)

  configFile := path.Join(configDir, PLANR_CONFIG_FILE)

  if _, err := toml.DecodeFile(configFile, cfg); err != nil {
    cfg = nil

    // TODO: handle missing config
    if STRICTLY_REQUIRE_CONFIG {
      log.Fatalf("Could not decode global configuration %s: %v", configFile, err)
    }
  }

  return cfg
}

func (cfg Config) IncompatibleWithVersion() bool {
  if strings.Count(cfg.Version, ".") != 2 {
    log.Fatalf("Version %s is not semantic", cfg.Version)
  }

  cfgbits := strings.SplitN(cfg.Version, ".", 2)
  bits := strings.SplitN(VERSION, ".", 2)

  // major version change
  if cfgbits[0] != bits[0] {
    return true
  }

  // Config newer, possible feature additions
  return cfgbits[1] > bits[1]
}