-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (116 loc) · 3.13 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
120
121
122
123
124
125
126
//go:binary-only-package
package main
import (
"fmt"
"github.com/devfacet/gocmd"
"io/ioutil"
"os"
"os/user"
"path"
"strings"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func getScriptByType(which string) string {
if which == "sys" {
return "\nexport PATH=$PATH:"
} else if which == "go" {
return "\nexport GOPATH=$GOPATH:"
}
panic("error type")
}
// 寻找默认Path
func findDefaultPath() *os.File {
userCur, err := user.Current()
check(err)
file, err := os.Open(path.Join(userCur.HomeDir, ".zshrc"))
if err != nil {
file, err = os.Open(path.Join(userCur.HomeDir, ".bashrc"))
if err != nil {
file, err = os.Create(path.Join(userCur.HomeDir, ".bashrc"))
check(err)
}
}
file, err = os.OpenFile(file.Name(), os.O_RDWR|os.O_APPEND, 0666)
check(err)
return file
}
func addPath(file *os.File, value string, which string) {
defer file.Close()
b, err := ioutil.ReadAll(file)
check(err)
pendStr := getScriptByType(which) + value
if strings.Contains(string(b), pendStr) {
fmt.Println(pendStr, "\t已经存在")
} else {
_, err = file.Write([]byte(pendStr))
check(err)
fmt.Println("修改成功,请手动source")
}
}
func Run(args []string, which string) {
var value string
var myPath string
if len(args) == 1 {
value, _ = os.Getwd()
addPath(findDefaultPath(), value, which)
} else if len(args) == 2 {
value = args[1]
addPath(findDefaultPath(), value, which)
} else if len(args) == 3 {
myPath = args[1]
value = args[2]
file, err := os.OpenFile(myPath, os.O_RDWR|os.O_APPEND, 0666)
check(err)
addPath(file, value, which)
}
}
func main() {
flags := struct {
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
Version bool `short:"v" long:"version" description:"Display version"`
Go struct {
ScriptPath string `short:"s" long:"scriptpath" required:"false" description:"script path"`
Path string `short:"p" long:"path" required:"false" description:"path"`
} `command:"go" description:"add current path to gopath"`
Sys struct {
ScriptPath string `short:"s" long:"scriptpath" required:"false" description:"script path"`
Path string `short:"p" long:"path" required:"false" description:"path"`
} `command:"sys" description:"add current path to path"`
}{}
_, _ = gocmd.HandleFlag("Go", func(cmd *gocmd.Cmd, args []string) error {
var runArgs []string
runArgs = append(runArgs, args[0])
if flags.Go.ScriptPath != "" {
runArgs = append(runArgs, flags.Go.ScriptPath)
}
if flags.Go.Path != "" {
runArgs = append(runArgs, flags.Go.Path)
}
Run(runArgs, "go")
return nil
})
_, _ = gocmd.HandleFlag("Sys", func(cmd *gocmd.Cmd, args []string) error {
var runArgs []string
runArgs = append(runArgs, args[0])
if flags.Sys.ScriptPath != "" {
runArgs = append(runArgs, flags.Sys.ScriptPath)
}
if flags.Sys.Path != "" {
runArgs = append(runArgs, flags.Sys.Path)
}
Run(runArgs, "sys")
return nil
})
// Init the app
_, _ = gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "快速添加环境变量和GOPATH变量",
Flags: &flags,
ConfigType: gocmd.ConfigTypeAuto,
})
}