forked from driusan/bug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (101 loc) · 2.53 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
package main
import (
"fmt"
"github.com/driusan/bug/bugapp"
"github.com/driusan/bug/bugs"
"os"
"os/exec"
"runtime"
// "bytes"
// "io"
)
func main() {
if bugs.GetRootDir() == "" {
fmt.Printf("Could not find issues directory.\n")
fmt.Printf("Make sure either the PMIT environment variable is set, or a parent directory of your working directory has an issues folder.\n")
fmt.Println("(If you just started new repo, you probably want to create directory named `issues`).")
fmt.Printf("Aborting.\n")
os.Exit(2)
}
// Create a pipe for a pager to use
r, w, err := os.Pipe()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Capture STDOUT for the Pager
stdout := os.Stdout
// Don't capture the output on MacOS, because for some reason
// it doesn't work and results in nothing getting printed
if runtime.GOOS != "darwin" {
os.Stdout = w
}
// Invoke less -RF attached to the pipe
// we created
cmd := exec.Command("less", "-RF")
cmd.Stdin = r
cmd.Stdout = stdout
cmd.Stderr = os.Stderr
// Make sure the pipe is closed after we
// finish, then restore STDOUT
defer func() {
w.Close()
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Stdout = stdout
}()
if len(os.Args) > 1 {
if len(os.Args) >= 3 && os.Args[2] == "--help" {
os.Args[1], os.Args[2] = "help", os.Args[1]
}
switch os.Args[1] {
case "add", "new", "create":
os.Stdout = stdout
bugapp.Create(os.Args[2:])
case "view", "list":
// bug list with no parameters shouldn't autopage,
// bug list with bugs to view should. So the original
// stdout is passed as a parameter.
bugapp.List(os.Args[2:], stdout)
case "priority":
bugapp.Priority(os.Args[2:])
case "status":
bugapp.Status(os.Args[2:])
case "milestone":
bugapp.Milestone(os.Args[2:])
case "id", "identifier":
bugapp.Identifier(os.Args[2:])
case "tag":
bugapp.Tag(os.Args[2:])
case "mv", "rename", "retitle", "relabel":
bugapp.Relabel(os.Args[2:])
case "purge":
// This shouldn't autopage
os.Stdout = stdout
bugapp.Purge()
case "rm", "close":
bugapp.Close(os.Args[2:])
case "edit":
// Edit needs the original Stdout since it
// invokes an editor
os.Stdout = stdout
bugapp.Edit(os.Args[2:])
case "--version", "version":
bugapp.Version()
case "env":
bugapp.Env()
case "dir", "pwd":
bugapp.Pwd()
case "commit":
bugapp.Commit(os.Args[2:])
case "roadmap":
bugapp.Roadmap(os.Args[2:])
case "help":
fallthrough
default:
bugapp.Help(os.Args[1:]...)
}
} else {
bugapp.Help()
}
}