-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Change default options to more sensible ones Remove `-quiet` and `-fmt` option, replace them with `-dbg` since they were only meant to be used if something went wrong. * In debug mod, prints errors on stderr * cosmetics * Keep -q -fmt deprecated/unused options * Improve README.md
- Loading branch information
Showing
4 changed files
with
92 additions
and
54 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
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
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
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 |
---|---|---|
@@ -1,129 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/arl/gitmux/format/json" | ||
"github.com/arl/gitmux/format/tmux" | ||
"github.com/arl/gitstatus" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func check(err error, quiet bool) { | ||
if err != nil { | ||
if !quiet { | ||
fmt.Println("error:", err) | ||
} | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// Config configures output formatting. | ||
type Config struct{ Tmux tmux.Config } | ||
"github.com/arl/gitmux/format/json" | ||
"github.com/arl/gitmux/format/tmux" | ||
) | ||
|
||
var version = "<<development version>>" | ||
|
||
var usage = `gitmux ` + version + ` | ||
Usage: gitmux [options] [dir] | ||
gitmux prints the status of a Git working tree. | ||
gitmux prints the status of a Git working tree as a tmux format string. | ||
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 use cfgfile when printing git status. | ||
-printcfg prints default configuration file. | ||
-V prints gitmux version and exits. | ||
-dbg outputs Git status as JSON and print errors. | ||
-V prints gitmux version and exits. | ||
` | ||
|
||
// Config configures output formatting. | ||
type Config struct{ Tmux tmux.Config } | ||
|
||
var defaultCfg = Config{Tmux: tmux.DefaultCfg} | ||
|
||
func parseOptions() (dir string, format string, quiet bool, cfg Config) { | ||
fmtOpt := flag.String("fmt", "json", "") | ||
func parseOptions() (dir string, dbg bool, cfg Config) { | ||
dbgOpt := flag.Bool("dbg", false, "") | ||
cfgOpt := flag.String("cfg", "", "") | ||
printCfgOpt := flag.Bool("printcfg", false, "") | ||
quietOpt := flag.Bool("q", false, "") | ||
versionOpt := flag.Bool("V", false, "") | ||
flag.Bool("q", true, "") // unused, kept for retrocompatibility. | ||
flag.String("fmt", "", "") // unused, kept for retrocompatibility. | ||
flag.Usage = func() { | ||
fmt.Println(usage) | ||
} | ||
flag.Parse() | ||
|
||
dir = "." | ||
if flag.NArg() > 0 { | ||
dir = flag.Arg(0) | ||
} | ||
cfg = defaultCfg | ||
|
||
if *versionOpt { | ||
fmt.Println(version) | ||
os.Exit(0) | ||
} | ||
|
||
if *printCfgOpt { | ||
enc := yaml.NewEncoder(os.Stdout) | ||
check(enc.Encode(&defaultCfg), *quietOpt) | ||
check(enc.Encode(&defaultCfg), *dbgOpt) | ||
enc.Close() | ||
os.Exit(0) | ||
} | ||
|
||
cfg = defaultCfg | ||
|
||
if *cfgOpt != "" { | ||
f, err := os.Open(*cfgOpt) | ||
check(err, *quietOpt) | ||
check(err, *dbgOpt) | ||
|
||
dec := yaml.NewDecoder(f) | ||
check(dec.Decode(&cfg), *quietOpt) | ||
check(dec.Decode(&cfg), *dbgOpt) | ||
} | ||
return dir, *fmtOpt, *quietOpt, cfg | ||
} | ||
|
||
type popdir func() error | ||
return dir, *dbgOpt, cfg | ||
} | ||
|
||
func pushdir(dir string) (popdir, error) { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
func pushdir(dir string) (popdir func() error, err error) { | ||
pwd := "" | ||
if pwd, err = os.Getwd(); err != nil { | ||
return nil, err | ||
} | ||
|
||
err = os.Chdir(dir) | ||
if err != nil { | ||
if err = os.Chdir(dir); err != nil { | ||
return nil, err | ||
} | ||
|
||
return func() error { return os.Chdir(pwd) }, nil | ||
} | ||
|
||
var errUnknownOutputFormat = errors.New("unknown output format") | ||
func check(err error, dbg bool) { | ||
if err != nil && dbg { | ||
fmt.Fprintln(os.Stderr, "error:", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func main() { | ||
// parse cli options. | ||
dir, format, quiet, cfg := parseOptions() | ||
dir, dbg, cfg := parseOptions() | ||
|
||
// handle directory change. | ||
if dir != "." { | ||
popDir, err := pushdir(dir) | ||
check(err, quiet) | ||
check(err, dbg) | ||
defer func() { | ||
check(popDir(), quiet) | ||
check(popDir(), dbg) | ||
}() | ||
} | ||
|
||
// retrieve git status. | ||
st, err := gitstatus.New() | ||
check(err, quiet) | ||
|
||
// register formaters | ||
formaters := make(map[string]formater) | ||
formaters["json"] = &json.Formater{} | ||
formaters["tmux"] = &tmux.Formater{Config: cfg.Tmux} | ||
check(err, dbg) | ||
|
||
formater, ok := formaters[format] | ||
if !ok { | ||
check(errUnknownOutputFormat, quiet) | ||
// select defauit formater | ||
var formater formater = &tmux.Formater{Config: cfg.Tmux} | ||
if dbg { | ||
formater = &json.Formater{} | ||
} | ||
|
||
// format and print | ||
err = formater.Format(os.Stdout, st) | ||
check(err, quiet) | ||
check(formater.Format(os.Stdout, st), dbg) | ||
} |