-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
75 lines (63 loc) · 1.75 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
package main
import (
"fmt"
"github.com/alexflint/go-arg"
"log"
"os"
"strings"
)
var (
version string
commit string
date string
)
type keyValue struct {
From, To string
}
func (kv *keyValue) UnmarshalText(b []byte) error {
s := string(b)
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("missing = in %s", s)
}
kv.From = parts[0]
kv.To = parts[1]
return nil
}
type args struct {
File string `arg:"-f,required"`
Property string `arg:"-p"`
EnvVar string `arg:"-e"`
Allowed []string `arg:"-a" help:"If specified, limits the values that can be passed from ENVVAR"`
Map []keyValue `arg:"-m" help:"Defines mappings of a given value into property value to set. Syntax is from=to"`
Bulk string `help:"The name of a bulk definition JSON file"`
}
func (args) Version() string {
return fmt.Sprintf("set-property %s (%s @ %s)", version, commit, date)
}
func (args) Description() string {
return "Conditionally updates a given properties file when the given environment variable is set"
}
func main() {
var args args
argsParser := arg.MustParse(&args)
if args.Bulk != "" {
err := setBulkProperties(args.File, args.Bulk, "")
if err != nil {
log.Fatalf("Failed to bulk-set properties in file: %s", err.Error())
}
} else if args.Property != "" && args.EnvVar != "" {
mappings := make(map[string]string)
for _, entry := range args.Map {
mappings[entry.From] = entry.To
}
err := setSingleProperty(args.File, args.Property, args.EnvVar, mappings, args.Allowed, "")
if err != nil {
log.Fatalf("Failed to set property in file: %s", err.Error())
}
} else {
fmt.Println("Need to pass single property definition or bulk file")
argsParser.WriteHelp(os.Stdout)
os.Exit(1)
}
}