-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (105 loc) · 2.74 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/cmonzillo91/puppetfile-editor/puppet"
"github.com/pkg/errors"
)
func main() {
log.SetOutput(os.Stdout)
conf := &Config{}
conf.LoadFromFlags()
if err := conf.ValidateConfig(); err != nil {
log.Fatal(err)
os.Exit(1)
}
// Open file
log.Println("Reading Puppetfile")
file, err := os.Open(conf.FileName)
if err != nil {
log.Fatal("Could not read TestPuppetfile from disk: %s", err)
os.Exit(1)
}
// Parse Puppetfile
log.Println("Parsing Puppetfile")
parser := puppet.PuppetParse{}
modules, err := parser.ReadModules(file)
if err != nil {
file.Close()
log.Fatal("Could not parse Puppetfile: %s", err)
os.Exit(1)
}
file.Close()
// Update module data
log.Println("Modifying Puppetfile")
UpdateModuleProperty(modules, conf.Module, conf.Key, conf.Value)
// Create New Files
log.Println("Writing New Modules")
file, err = os.Create(conf.FileName)
if err != nil {
file.Close()
log.Fatal("Could not open new Puppetfile: %s", err)
os.Exit(1)
}
if err := parser.WriteModules(file, modules); err != nil {
file.Close()
log.Fatal("Could write new modules to the Puppetfile: %s", err)
os.Exit(1)
}
file.Close()
}
// UpdateModuleProperty, updates a single property in a single module
func UpdateModuleProperty(modules []*puppet.Module, module, key, value string) {
for _, mod := range modules {
if strings.ToLower(mod.Name) == module {
if strings.ToLower(key) == "version" {
mod.Version = value
} else if err := mod.SetProperty(puppet.KEYWORD(key), value); err != nil {
log.Fatalf("Could not set property: %s", err)
}
}
}
}
// Config contains the command line properties passed into the program
type Config struct {
FileName string
Module string
Key string
Value string
}
// LoadFromFlags loads the configuration from commandline flags
func (c *Config) LoadFromFlags() {
fileName := flag.String("puppetfile", "", "Original PuppetFile")
module := flag.String("module", "", "The module whos properties to update")
key := flag.String("key", "", "The key of the property to change")
value := flag.String("value", "", "Value of the property that will be set")
flag.Parse()
flag.Usage = func() {
fmt.Printf("Usage: %s [OPTIONS] argument ...\n", os.Args[0])
flag.PrintDefaults()
}
if flag.Lookup("help") != nil {
flag.Usage()
os.Exit(1)
}
c.FileName = *fileName
c.Module = *module
c.Key = *key
c.Value = *value
}
// ValidateConfig validates that the config is properly set
func (c *Config) ValidateConfig() error {
if c.FileName == "" {
return errors.New("Puppetfile not provided")
}
if c.Module == "" {
return errors.New("Module not provided")
}
if c.Key == "" {
return errors.New("Key not provided")
}
return nil
}