Skip to content

Commit faf1833

Browse files
committed
Add version subcommand using embedded VCS info
1 parent 5ecf58c commit faf1833

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

cmd/shellshape/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"os"
7+
"runtime/debug"
78
"strings"
89

910
shellshape "github.com/micthiesen/shellshape"
@@ -12,6 +13,10 @@ import (
1213

1314
func main() {
1415
if len(os.Args) > 1 {
16+
if os.Args[1] == "version" {
17+
fmt.Println(versionString())
18+
return
19+
}
1520
if os.Args[1] == "list" {
1621
for _, name := range shellshape.RegisteredHandlers() {
1722
fmt.Println(name)
@@ -46,3 +51,34 @@ func main() {
4651
os.Exit(1)
4752
}
4853
}
54+
55+
func versionString() string {
56+
info, ok := debug.ReadBuildInfo()
57+
if !ok {
58+
return "shellshape (unknown)"
59+
}
60+
var revision, time string
61+
var dirty bool
62+
for _, s := range info.Settings {
63+
switch s.Key {
64+
case "vcs.revision":
65+
revision = s.Value
66+
case "vcs.time":
67+
time = s.Value
68+
case "vcs.modified":
69+
dirty = s.Value == "true"
70+
}
71+
}
72+
if revision == "" {
73+
return "shellshape (dev)"
74+
}
75+
short := revision
76+
if len(short) > 7 {
77+
short = short[:7]
78+
}
79+
v := "shellshape " + short + " (" + time + ")"
80+
if dirty {
81+
v += " dirty"
82+
}
83+
return v
84+
}

handlers/shellshape.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func init() {
1313
func handleShellshape(subcommand string, tokens []string) []string {
1414
args, redirects := shellshape.SplitRedirects(tokens)
1515

16-
// "list" subcommand has no args to normalize.
17-
if len(args) > 0 && args[0] == "list" {
18-
return append([]string{"list"}, redirects...)
16+
// Subcommands with no args to normalize.
17+
if len(args) > 0 && (args[0] == "list" || args[0] == "version") {
18+
return append([]string{args[0]}, redirects...)
1919
}
2020

2121
var result []string

handlers/shellshape_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ func TestShellshape(t *testing.T) {
2121
{"command with pipes as single arg", `shellshape 'echo hello | grep h'`, "shellshape <command>"},
2222
{"multiple words", "shellshape docker run -it ubuntu bash", "shellshape <command>"},
2323

24-
// List subcommand
24+
// Subcommands
2525
{"list subcommand", "shellshape list", "shellshape list"},
26+
{"version subcommand", "shellshape version", "shellshape version"},
2627

2728
// With redirect
2829
{"with redirect", "shellshape echo hi > /tmp/out.txt", "shellshape <command> > <path>"},

0 commit comments

Comments
 (0)