forked from tcnksm/docc
/
docc.go
225 lines (174 loc) · 4.5 KB
/
docc.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
flag "github.com/dotcloud/docker/pkg/mflag"
)
func main() {
var (
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
flHelp = flag.Bool([]string{"h", "-help"}, false, "Print this message")
flDebug = flag.Bool([]string{"-debug"}, false, "Run as DEBUG mode")
flEditor = flag.Bool([]string{"e", "-editor"}, false, "Open README by editor")
flForce = flag.Bool([]string{"f", "-force"}, false, "Create README file without prompting")
flCommand = flag.Bool([]string{"c", "-command"}, false, "Open README by command which configured in .gitcofing by default")
argCommand = flag.String([]string{"-open-with"}, "", "Set command to open ")
)
flag.Parse()
if *flDebug {
os.Setenv("DEBUG", "1")
}
if *flVersion {
showVersion()
os.Exit(0)
}
if *flHelp {
flag.Usage()
os.Exit(0)
}
cmd := ""
if *flCommand {
cmd = retrieveCmd()
}
if *argCommand != "" {
cmd = *argCommand
}
if *flEditor {
cmd = os.Getenv("EDITOR")
}
debug("cmd:", cmd)
path := "."
if len(os.Args) > 1 {
path = flag.Arg(0)
}
debug("path:", path)
absPath, err := filepath.Abs(path)
if err != nil {
panic(err)
}
debug("absPath:", absPath)
if _, err := os.Stat(absPath); os.IsNotExist(err) {
fmt.Printf("no such file or directory: %s\n", absPath)
os.Exit(1)
}
os.Chdir(absPath)
url := retrieveURL()
if cmd == "" && url != "" {
debug("url:", url)
openByBrowser(url)
}
if cmd == "" {
fmt.Printf("Could not retrieve project url from %s\n", absPath)
os.Exit(0)
}
readmeFile := retrieveReadmeFile(*flForce)
if readmeFile == "" {
fmt.Println("Could not retrieve README from %s\n", absPath)
}
execOpen(cmd, readmeFile)
}
func showVersion() {
fmt.Println("docc version 0.1.0")
}
func debug(v ...interface{}) {
if os.Getenv("DEBUG") != "" {
log.Println(v...)
}
}
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func retrieveCmd() string {
cmd, err := gitConfig("docc.cmd")
if err != nil {
panic(err)
}
return cmd
}
func retrieveURL() string {
urlPattern := "remote.origin.url" // Should be configurable
gitConfigCmd := exec.Command("git", "config", "--local", urlPattern)
url, err := gitConfigCmd.Output()
if err != nil || len(url) == 0 {
return ""
}
trimedURL := strings.TrimRight(string(url), "\n")
return convertURLToHTTPS(trimedURL)
}
func convertURLToHTTPS(ref string) string {
hasSchemePattern := regexp.MustCompile("^[^:]+://")
scpLikeUrlPattern := regexp.MustCompile("^([^@]+@)?([^:]+):(/~[^/]*/)?(.+).git$")
if !hasSchemePattern.MatchString(ref) && scpLikeUrlPattern.MatchString(ref) {
matched := scpLikeUrlPattern.FindStringSubmatch(ref)
host := matched[2]
path := matched[4]
return fmt.Sprintf("https://%s/%s", host, path)
}
urlWithSchemePattern := regexp.MustCompile("^[^:]+://([^@]+@)?([^:/]+)(:[0-9]+)?(/~[^/]*)?/(.+).git$")
if hasSchemePattern.MatchString(ref) && urlWithSchemePattern.MatchString(ref) {
matched := urlWithSchemePattern.FindStringSubmatch(ref)
host := matched[2]
path := matched[5]
return fmt.Sprintf("https://%s/%s", host, path)
}
return ""
}
func retrieveReadmeFile(forceCreate bool) string {
readmeFileName := "README.md" // Should be configurable
readmeFile, err := filepath.Abs(readmeFileName)
if err != nil {
panic(err)
}
debug("readme:", readmeFile)
if _, err := os.Stat(readmeFile); os.IsNotExist(err) && !forceCreate {
fmt.Printf("%s is not found, create it ? [Y/n]: ", readmeFile)
var ans string
_, err := fmt.Scanf("%s", &ans)
if err != nil {
panic(err)
}
if ans != "Y" {
return ""
}
}
return readmeFile
}
func openByBrowser(url string) {
openCmd := "open" // Only OSX or Linux
execOpen(openCmd, url)
}
func execOpen(cmd string, target string) {
binary, lookErr := exec.LookPath(cmd)
if lookErr != nil {
fmt.Printf("open command '%s' not found in $PATH\n", cmd)
os.Exit(1)
}
args := []string{cmd, target}
env := os.Environ()
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
}
func gitConfig(key string) (string, error) {
cmd := exec.Command("git", "config", "--path", "--null", "--get", key)
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if exitError, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
if waitStatus.ExitStatus() == 1 {
return "", nil
}
}
return "", err
}
return strings.TrimRight(string(buf), "\000"), nil
}