-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (58 loc) · 1.25 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
package main
import (
"os"
"fmt"
"strings"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "eps"
app.Usage = "list up env paths with split style."
// setup flags.
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "path, p",
Value: "PATH",
Usage: "specify the env name.",
},
cli.StringFlag{
Name: "splitter, s",
Value: ":",
Usage: "specify the path splitter.",
},
}
// action settings.
app.Action = func (c *cli.Context) {
path := c.String("path")
splitter := c.String("splitter")
listUpValuesWithSplitter(path, splitter)
}
app.Run(os.Args)
}
func listUpValuesWithSplitter(path string, splitter string) {
fmt.Println("---------------------")
fmt.Printf("target env: %s\n", path)
fmt.Printf("splitter: %s\n", splitter)
fmt.Println("---------------------")
context := getContextWithPath(path)
values := strings.Split(context, splitter)
for _, value := range values {
fmt.Println(value)
}
fmt.Println("---------------------")
}
func getContextWithPath(path string) string {
var retStr string
envs := os.Environ()
for _, val := range envs {
splits := strings.SplitN(val, "=", 2)
key := splits[0]
value := splits[1]
if key == path {
retStr = value
break
}
}
return retStr
}