Util | |
---|---|
1 | package main |
2 | |
3 | import ( |
4 | "encoding/json" |
5 | "log" |
6 | "io/ioutil" |
7 | ) |
8 | |
9 | var Repos []Repository |
10 | |
11 | // Loads the configuration file. The configuration file is assumed to |
12 | // be a json file. Currently just loads all repositories with their |
13 | // corresponding file paths. It also reads in the SCM type, which |
14 | // I intend to identify through the program in the future without |
15 | // needing it to be specified in the config |
16 | func LoadConfig() { |
17 | const CONF_PATH = "config.json" |
18 | |
19 | type JsonRepo struct { |
20 | Name string |
21 | Path string |
22 | Scm string |
23 | } |
24 | |
25 | type Configuration struct { |
26 | Repositories []JsonRepo |
27 | } |
28 | |
29 | var conf Configuration |
30 | |
31 | content, err := ioutil.ReadFile(CONF_PATH) |
32 | if err != nil { |
33 | log.Fatal("ReadFile: ", err) |
34 | } |
35 | |
36 | if json.Unmarshal(content, &conf) != nil { |
37 | log.Fatal("Unmarshal: ", err) |
38 | } |
39 | |
40 | for _, r := range conf.Repositories { |
41 | switch r.Scm { |
42 | // to implement more later.. for now it's just git |
43 | case "git": |
44 | Repos = append(Repos, |
45 | &GitRepository { RepositoryInfo { r.Name, r.Path } }) |
46 | } |
47 | } |
48 | } |
49 | |
50 | // Gets the repository by name - assuming name uniqueness right now |
51 | // based on the config file, likely will need a way to reinforce this later. |
52 | func GetRepository(name string) Repository { |
53 | // Simply iterating through the array - shouldn't be an issue |
54 | // if we aren't assuming clients have hundreds of repositories. |
55 | // If it is, I will use a map implementation instead. |
56 | var repo Repository |
57 | |
58 | for _, r := range Repos { |
59 | if r.GetName() == name { |
60 | repo = r |
61 | } |
62 | } |
63 | |
64 | return repo |
65 | } |