Initial version

This commit is contained in:
Aurélien Rainone 2019-10-28 08:45:14 +01:00
parent 6c470f62e6
commit d0f6255a1f
6 changed files with 160 additions and 0 deletions

66
cli.go Normal file
View file

@ -0,0 +1,66 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/arl/gitstatus/format/tmux"
"gopkg.in/yaml.v2"
)
func check(err error, quiet bool) {
if err != nil {
if !quiet {
fmt.Println("error:", err)
}
os.Exit(1)
}
}
const version = "0.1"
const usage = `gitstatus ` + version + `
Usage: gitstatus [options] [dir]
gitstatus prints the status of a Git working tree.
If directory is not given, it default to the working directory.
Options:
-q be quiet. In case of errors, don't print nothing.
-fmt output format, defaults to json.
json prints status as a JSON object.
tmux prints status as a tmux format string.
-cfg cfgfile gitstatus output configuration file.
-printcfg prints default configuration file.
`
var defaultCfg = Config{Tmux: tmux.DefaultCfg}
func parseOptions() (dir string, format string, quiet bool, cfg Config) {
fmtOpt := flag.String("fmt", "json", "")
cfgOpt := flag.String("cfg", "", "")
printCfgOpt := flag.Bool("printcfg", false, "")
quietOpt := flag.Bool("q", false, "")
flag.Usage = func() {
fmt.Println(usage)
}
flag.Parse()
dir = "."
if flag.NArg() > 0 {
dir = flag.Arg(0)
}
cfg = defaultCfg
if *printCfgOpt {
enc := yaml.NewEncoder(os.Stdout)
check(enc.Encode(&defaultCfg), *quietOpt)
enc.Close()
os.Exit(0)
}
if *cfgOpt != "" {
f, err := os.Open(*cfgOpt)
check(err, *quietOpt)
dec := yaml.NewDecoder(f)
check(dec.Decode(&cfg), *quietOpt)
}
return dir, *fmtOpt, *quietOpt, cfg
}

8
config.go Normal file
View file

@ -0,0 +1,8 @@
package main
import "github.com/arl/gitstatus/format/tmux"
// Config configures output formatting.
type Config struct {
Tmux tmux.Config
}

8
go.mod Normal file
View file

@ -0,0 +1,8 @@
module github.com/arl/gitmux
go 1.10
require (
github.com/arl/gitstatus v0.3.1
gopkg.in/yaml.v2 v2.2.4
)

14
go.sum Normal file
View file

@ -0,0 +1,14 @@
github.com/arl/gitstatus v0.3.1 h1:KIv92sf+Ce6E6qGXrx/WEl76lDwmR/ibtsExvCp8yPM=
github.com/arl/gitstatus v0.3.1/go.mod h1:6QjgTVY8epnMyZyJN4QjEY5WZZUCNfJsCFSjkfzaLFA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

45
main.go Normal file
View file

@ -0,0 +1,45 @@
package main
import (
"errors"
"fmt"
"github.com/arl/gitstatus"
"github.com/arl/gitstatus/format/json"
"github.com/arl/gitstatus/format/tmux"
)
var errUnknownOutputFormat = errors.New("unknown output format")
func main() {
// parse cli options.
dir, format, quiet, cfg := parseOptions()
// handle directory change.
if dir != "." {
popDir, err := pushdir(dir)
check(err, quiet)
defer func() {
check(popDir(), quiet)
}()
}
// retrieve git status.
st, err := gitstatus.New()
check(err, quiet)
// register formaters
formaters := make(map[string]gitstatus.Formater)
formaters["json"] = &json.Formater{}
formaters["tmux"] = &tmux.Formater{Config: cfg.Tmux}
formater, ok := formaters[format]
if !ok {
check(errUnknownOutputFormat, quiet)
}
// format and print
out, err := formater.Format(st)
check(err, quiet)
fmt.Print(out)
}

19
pushd.go Normal file
View file

@ -0,0 +1,19 @@
package main
import "os"
type popdir func() error
func pushdir(dir string) (popdir, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
err = os.Chdir(dir)
if err != nil {
return nil, err
}
return func() error { return os.Chdir(pwd) }, nil
}