-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out cli code and add version flag
- Loading branch information
Showing
3 changed files
with
89 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
@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`" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
) | ||
|
||
// TODO allow custom log location | ||
const logloc string = "/tmp/porcelain.log" | ||
|
||
var ( | ||
buildRev string = "invalid" | ||
buildTag string = "invalid" | ||
buildTime string = "invalid" | ||
) | ||
|
||
var ( | ||
cwd string | ||
noColorFlag bool | ||
debugFlag, fmtFlag bool | ||
bashFmtFlag, zshFmtFlag bool | ||
tmuxFmtFlag bool | ||
versionFlag bool | ||
) | ||
|
||
func init() { | ||
flag.BoolVar(&debugFlag, "debug", false, "write logs to file ("+logloc+")") | ||
flag.BoolVar(&fmtFlag, "fmt", true, "print formatted output (default)") | ||
flag.BoolVar(&bashFmtFlag, "bash", false, "escape fmt output for bash") | ||
flag.BoolVar(&noColorFlag, "no-color", false, "print formatted output without color codes") | ||
flag.BoolVar(&zshFmtFlag, "zsh", false, "escape fmt output for zsh") | ||
flag.BoolVar(&tmuxFmtFlag, "tmux", false, "escape fmt output for tmux") | ||
flag.StringVar(&cwd, "path", "", "show output for path instead of the working directory") | ||
flag.BoolVar(&versionFlag, "version", false, "print version and exit") | ||
|
||
logtostderr := flag.Bool("logtostderr", false, "write logs to stderr") | ||
flag.Parse() | ||
|
||
if versionFlag { | ||
fmt.Printf("porcelain version %s (%s)\nbuilt %s\n", buildTag, buildRev, buildTime) | ||
os.Exit(0) | ||
} | ||
|
||
if debugFlag { | ||
var ( | ||
err error | ||
logFd io.Writer | ||
) | ||
if *logtostderr { | ||
logFd = os.Stderr | ||
} else { | ||
logFd, err = os.OpenFile(logloc, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModePerm) | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
log.SetOutput(logFd) | ||
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile) | ||
} else { | ||
log.SetOutput(ioutil.Discard) | ||
} | ||
|
||
if cwd == "" { | ||
cwd, _ = os.Getwd() | ||
} | ||
} | ||
|
||
func main() { | ||
log.Println("running porcelain...") | ||
log.Println("in directory:", cwd) | ||
|
||
var out string | ||
switch { | ||
case fmtFlag: | ||
out = run().Fmt() | ||
default: | ||
flag.Usage() | ||
fmt.Println("\nOutside of a repository there will be no output.") | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Fprint(os.Stdout, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters