Skip to content

Commit f5a48d1

Browse files
committed
Split out cli code and add version flag
1 parent 1896c75 commit f5a48d1

File tree

3 files changed

+89
-66
lines changed

3 files changed

+89
-66
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
@go build -ldflags "-X main.buildTime=`date -u '+%Y-%m-%d_%I:%M:%S%p'` -X main.buildRev=`git rev-parse --short HEAD` -X main.buildTag=`git describe --tags --long`"

main.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
)
11+
12+
// TODO allow custom log location
13+
const logloc string = "/tmp/porcelain.log"
14+
15+
var (
16+
buildRev string = "invalid"
17+
buildTag string = "invalid"
18+
buildTime string = "invalid"
19+
)
20+
21+
var (
22+
cwd string
23+
noColorFlag bool
24+
debugFlag, fmtFlag bool
25+
bashFmtFlag, zshFmtFlag bool
26+
tmuxFmtFlag bool
27+
versionFlag bool
28+
)
29+
30+
func init() {
31+
flag.BoolVar(&debugFlag, "debug", false, "write logs to file ("+logloc+")")
32+
flag.BoolVar(&fmtFlag, "fmt", true, "print formatted output (default)")
33+
flag.BoolVar(&bashFmtFlag, "bash", false, "escape fmt output for bash")
34+
flag.BoolVar(&noColorFlag, "no-color", false, "print formatted output without color codes")
35+
flag.BoolVar(&zshFmtFlag, "zsh", false, "escape fmt output for zsh")
36+
flag.BoolVar(&tmuxFmtFlag, "tmux", false, "escape fmt output for tmux")
37+
flag.StringVar(&cwd, "path", "", "show output for path instead of the working directory")
38+
flag.BoolVar(&versionFlag, "version", false, "print version and exit")
39+
40+
logtostderr := flag.Bool("logtostderr", false, "write logs to stderr")
41+
flag.Parse()
42+
43+
if versionFlag {
44+
fmt.Printf("porcelain version %s (%s)\nbuilt %s\n", buildTag, buildRev, buildTime)
45+
os.Exit(0)
46+
}
47+
48+
if debugFlag {
49+
var (
50+
err error
51+
logFd io.Writer
52+
)
53+
if *logtostderr {
54+
logFd = os.Stderr
55+
} else {
56+
logFd, err = os.OpenFile(logloc, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModePerm)
57+
if err != nil {
58+
os.Exit(1)
59+
}
60+
}
61+
log.SetOutput(logFd)
62+
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
63+
} else {
64+
log.SetOutput(ioutil.Discard)
65+
}
66+
67+
if cwd == "" {
68+
cwd, _ = os.Getwd()
69+
}
70+
}
71+
72+
func main() {
73+
log.Println("running porcelain...")
74+
log.Println("in directory:", cwd)
75+
76+
var out string
77+
switch {
78+
case fmtFlag:
79+
out = run().Fmt()
80+
default:
81+
flag.Usage()
82+
fmt.Println("\nOutside of a repository there will be no output.")
83+
os.Exit(1)
84+
}
85+
86+
fmt.Fprint(os.Stdout, out)
87+
}

porcelain.go

-66
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"bytes"
5-
"flag"
65
"fmt"
7-
"io"
86
"io/ioutil"
97
"log"
108
"os"
@@ -13,17 +11,6 @@ import (
1311
"github.com/robertgzr/color"
1412
)
1513

16-
// TODO allow custom log location
17-
const logloc string = "/tmp/porcelain.log"
18-
19-
var (
20-
cwd string
21-
noColorFlag bool
22-
debugFlag, fmtFlag bool
23-
bashFmtFlag, zshFmtFlag bool
24-
tmuxFmtFlag bool
25-
)
26-
2714
type GitArea struct {
2815
modified int
2916
added int
@@ -212,56 +199,3 @@ func run() *PorcInfo {
212199

213200
return porcInfo
214201
}
215-
216-
func init() {
217-
flag.BoolVar(&debugFlag, "debug", false, "write logs to file ("+logloc+")")
218-
flag.BoolVar(&fmtFlag, "fmt", true, "print formatted output (default)")
219-
flag.BoolVar(&bashFmtFlag, "bash", false, "escape fmt output for bash")
220-
flag.BoolVar(&noColorFlag, "no-color", false, "print formatted output without color codes")
221-
flag.BoolVar(&zshFmtFlag, "zsh", false, "escape fmt output for zsh")
222-
flag.BoolVar(&tmuxFmtFlag, "tmux", false, "escape fmt output for tmux")
223-
flag.StringVar(&cwd, "path", "", "show output for path instead of the working directory")
224-
225-
logtostderr := flag.Bool("logtostderr", false, "write logs to stderr")
226-
flag.Parse()
227-
228-
if debugFlag {
229-
var (
230-
err error
231-
logFd io.Writer
232-
)
233-
if *logtostderr {
234-
logFd = os.Stderr
235-
} else {
236-
logFd, err = os.OpenFile(logloc, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModePerm)
237-
if err != nil {
238-
os.Exit(1)
239-
}
240-
}
241-
log.SetOutput(logFd)
242-
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
243-
} else {
244-
log.SetOutput(ioutil.Discard)
245-
}
246-
247-
if cwd == "" {
248-
cwd, _ = os.Getwd()
249-
}
250-
}
251-
252-
func main() {
253-
log.Println("running porcelain...")
254-
log.Println("in directory:", cwd)
255-
256-
var out string
257-
switch {
258-
case fmtFlag:
259-
out = run().Fmt()
260-
default:
261-
flag.Usage()
262-
fmt.Println("\nOutside of a repository there will be no output.")
263-
os.Exit(1)
264-
}
265-
266-
fmt.Fprint(os.Stdout, out)
267-
}

0 commit comments

Comments
 (0)