This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (79 loc) · 2.52 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
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/alecthomas/kingpin"
)
func main() {
switch kingpin.Parse() {
case "create":
if *createStashContent == "." {
*createStashContent = _wd
}
if err := createStash(*createStashName, *createStashContent, _appHome); err != nil {
fmt.Println(err)
return
}
case "expand":
if *expandDstDir == "." {
*expandDstDir = _wd
}
var templatesData map[string]string
if expandData != nil {
templatesData = *expandData
}
if err := expandStash(*expandStashName, _appHome, *expandDstDir, templatesData); err != nil {
fmt.Println(err)
return
}
case "list":
l, err := listDepth(_appHome, 5)
if err != nil {
fmt.Println(err)
return
}
var items []interface{}
for _, v := range l {
items = append(items, v)
}
fmt.Println(items...)
case "delete":
if err := deleteStash(*deleteStashName, _appHome); err != nil {
fmt.Println(err)
return
}
}
}
var (
createCommand = kingpin.Command("create", "creating stash based on the content of a directory")
createStashName = createCommand.Flag("stash-name", "name of this stash, lower case, only numbers, alphabet and - and _").Short('n').Required().String()
createStashContent = createCommand.Flag("stash-content", "the directory that its content will be used to create the stash").Short('c').Default(".").String()
expandCommand = kingpin.Command("expand", "expand stash and expand it into a directory")
expandStashName = expandCommand.Flag("stash-name", "name of this stash, lower case, only numbers, alphabet and - and _").Short('n').Required().String()
expandDstDir = expandCommand.Flag("destination", "the directory that its content will be expanded to").Short('d').Default(".").String()
expandData = expandCommand.Arg("data", "json data for template files, multiple ones with format filename1=JSON filename2=JSON").StringMap()
listCommand = kingpin.Command("list", "lists existing file stashes")
deleteCommand = kingpin.Command("delete", "delete existing file stashe")
deleteStashName = deleteCommand.Flag("stash-name", "name of the file stash to delete, lower case, only numbers, alphabet and - and _").Short('n').Required().String()
)
func init() {
usr, err := user.Current()
if err != nil {
panic(err)
}
_appHome = filepath.Join(usr.HomeDir, ".fstash")
if err := os.MkdirAll(_appHome, 0777); err != nil {
panic(err)
}
_wd, err = os.Getwd()
if err != nil {
panic(err)
}
kingpin.CommandLine.HelpFlag.Short('h')
}
var (
_appHome string
_wd string
)