This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
89 lines (71 loc) · 1.79 KB
/
config.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"path/filepath"
)
const (
VERSION = "0.1.1"
)
type HtmlConfig struct {
PageTitle string `yaml:"PageTitle"`
}
type PipelineStatusConfig struct {
Region string `yaml:"Region"`
TargetPath string `yaml:"TargetPath"`
Html HtmlConfig `yaml:"Html"`
BucketName string `yaml:"BucketName"`
BucketKey string `yaml:"BucketKey"` // key inside the s3
TimeFormat string `yaml:"TimeFormat"`
}
type Arguments struct {
config string
}
func parseArguments() Arguments {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
arguments := &Arguments{
config: path.Join(dir, "config.yml"),
}
flag.StringVar(&arguments.config, "config", arguments.config, "Config file")
flag.Usage = flagUsage
flag.Parse()
return *arguments
}
func flagUsage() {
_, _ = fmt.Fprintf(os.Stderr, "Version of %s: %s\n", os.Args[0], VERSION)
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
func newPipelineStatusConfig(configPath string) PipelineStatusConfig {
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
fmt.Println("Could not read config", err)
os.Exit(1)
}
config := &PipelineStatusConfig{}
if err := yaml.Unmarshal(bytes, config); err != nil {
fmt.Println("Could not parse config", err)
os.Exit(1)
}
return setDefaultValues(*config)
}
func setDefaultValues(config PipelineStatusConfig) PipelineStatusConfig {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
if config.Region == "" {
config.Region = "eu-central-1"
}
if config.TargetPath == "" {
config.TargetPath = filepath.Join(dir, "target")
}
if config.Html.PageTitle == "" {
config.Html.PageTitle = "Codepipeline Status"
}
if config.TimeFormat == "" {
config.TimeFormat = "2006-01-02 15:04"
}
return config
}