-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (113 loc) · 3.01 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"github.com/legend80s/go-change-dir/cmd"
"github.com/legend80s/go-change-dir/utils"
)
var println = fmt.Println
// var print = fmt.Print
func myUsage() {
fmt.Printf("Usage: %s [OPTIONS] dirname_you_want_cd_into\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
// In nodejs I use __dirname . What is the equivalent of this in Golang?
// func getRuntimeDirname(dir string) string {
// dir, err := filepath.Abs(filepath.Dir(dir))
// if err != nil {
// panic(err)
// }
// return dir
// }
func main() {
flag.Usage = myUsage
verbose := flag.Bool("verbose", false, "Show more information")
walk := flag.Bool("walk", false, "Should walk directory tree")
fallback := flag.Bool("fallback", false, "Output fallback dir when no dir found")
// stat cmd
statCmd := flag.NewFlagSet("stat", flag.ExitOnError)
// clear cache
clearCache := statCmd.Bool("clear", false, "Clear cache")
// set search dir cmd
setSearchDirCmd := flag.NewFlagSet("set-root", flag.ExitOnError)
switch os.Args[1] {
case "stat":
statCmd.Parse(os.Args[2:])
if *clearCache {
cmd.ClearDB(utils.DBFilepath, *verbose)
} else {
cmd.Stat(utils.DBFilepath)
}
os.Exit(0)
case "set-root":
if len(os.Args) != 3 {
fmt.Println("`search-dir` required. Example: cdi set-root <search-dir>")
os.Exit(1)
}
setSearchDirCmd.Parse(os.Args[2:])
cmd.SetSearchDir(utils.DBFilepath, os.Args[2], *verbose)
os.Exit(0)
}
flag.Parse()
if *verbose {
// https://bash.cyberciti.biz/guide/$@
fmt.Println("os.Args", os.Args)
}
if flag.NArg() == 0 {
println("No target dirname passed.")
flag.Usage()
os.Exit(1)
}
// golang如何判断是从源码运行还是从二进制文件运行?
// runtimeDirname := getRuntimeDirname(os.Args[0])
// if *verbose {
// fmt.Println("__dirname", runtimeDirname)
// }
// 如何获取 positional arguments
dirname := os.Args[flag.NFlag()+1]
searchRoot := cmd.GetSearchDir()
// println("verbose", *verbose)
// TODO: 测试下 --walk
target, db := "", utils.ReadDB(utils.DBFilepath, *verbose)
if !*walk {
target = utils.FindBestMatchFromDB(db.Shortcuts, dirname, *verbose)
// println("target from db:", target)
if target != "" {
if *verbose {
println("From DB")
}
existing := utils.IsFileExisting(target)
if existing {
cd(target)
return
}
}
}
target = utils.FindBestMatch(searchRoot, dirname, *verbose)
if target != "" {
if *verbose {
println("From walk dir")
}
utils.SaveShortcutsToDB(utils.DBFilepath, db, dirname, target, *verbose)
cd(target)
return
}
if *fallback {
fmt.Print(path.Join(searchRoot, dirname))
} else {
fmt.Printf("no such dirname like \"%s\" found in %s\n", dirname, searchRoot)
os.Exit(1)
}
}
func cd(dir string) {
// cd /Users/liuchuanzong/workspace/alipay/infrastructure/eslint-config-paytm-cli/test/mini-balance
// 应该找到最短的
// 深度优先导致不行哦
fmt.Print(dir)
// if err := os.Chdir(dir); err != nil {
// log.Fatal(err)
// }
}