Skip to content

Commit

Permalink
Split out cli code and add version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgzr committed Oct 11, 2018
1 parent 1896c75 commit f5a48d1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 66 deletions.
2 changes: 2 additions & 0 deletions Makefile
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`"
87 changes: 87 additions & 0 deletions main.go
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)
}
66 changes: 0 additions & 66 deletions porcelain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -13,17 +11,6 @@ import (
"github.com/robertgzr/color"
)

// TODO allow custom log location
const logloc string = "/tmp/porcelain.log"

var (
cwd string
noColorFlag bool
debugFlag, fmtFlag bool
bashFmtFlag, zshFmtFlag bool
tmuxFmtFlag bool
)

type GitArea struct {
modified int
added int
Expand Down Expand Up @@ -212,56 +199,3 @@ func run() *PorcInfo {

return porcInfo
}

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")

logtostderr := flag.Bool("logtostderr", false, "write logs to stderr")
flag.Parse()

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)
}

0 comments on commit f5a48d1

Please sign in to comment.