-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (62 loc) · 1.6 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
package main
import (
"os"
"fmt"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "env-handler"
app.Usage = "This app handles env file."
app.Version = "0.0.1"
app.Commands = []cli.Command {
{
Name: "gen-sample",
Usage: "generate sample file from existing env file",
Action: func(c *cli.Context) error {
filepath := c.String("envfile")
GenerateSampleEnvfile(filepath)
return nil
},
Flags: []cli.Flag {
cli.StringFlag {
Name: "envfile, e",
Value: "./.env",
Usage: "path to envfile",
},
},
},
{
Name: "add",
Usage: "add new key-value line in envfile",
Action: func(c *cli.Context) error {
filepath := c.String("envfile")
if len(c.Args()) == 2 {
key := c.Args().Get(0)
value := c.Args().Get(1)
AddNewEnvToFile(filepath, key, value)
AddNewEnvToFile(filepath + ".sample", key, value)
return nil
} else {
return nil
}
},
Flags: []cli.Flag {
cli.StringFlag {
Name: "envfile, e",
Value: "./.env",
Usage: "path to envfile",
},
},
},
}
app.Run(os.Args)
}
func GenerateSampleEnvfile(filepath string) {
texts := ReadEnvFile(filepath)
WriteEnvFile(filepath + ".sample", BuildValueRemovedLines(texts))
fmt.Printf("generated sample file: %v => %v\n", filepath, filepath + ".sample")
}
func AddNewEnvToFile(filepath string, key string, value string) {
AppendLineToFile(filepath, key + "=" + value)
}