Skip to content

Commit c883478

Browse files
authored
Merge pull request #44 from tianon/help-me-mr-wizard
Add "--help" and "--version" as explicit flags
2 parents e87cf95 + 3414434 commit c883478

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.7-alpine
1+
FROM golang:1.9-alpine
22

33
RUN apk add --no-cache ca-certificates file openssl
44

@@ -23,12 +23,13 @@ WORKDIR /go/src/github.com/tianon/gosu
2323
RUN set -x \
2424
&& eval "GOARCH=amd64 go build $BUILD_FLAGS -o /go/bin/gosu-amd64" \
2525
&& file /go/bin/gosu-amd64 \
26-
&& { /go/bin/gosu-amd64 || true; } \
26+
&& /go/bin/gosu-amd64 --version \
2727
&& /go/bin/gosu-amd64 nobody id \
2828
&& /go/bin/gosu-amd64 nobody ls -l /proc/self/fd
2929
RUN set -x \
3030
&& eval "GOARCH=386 go build $BUILD_FLAGS -o /go/bin/gosu-i386" \
3131
&& file /go/bin/gosu-i386 \
32+
&& /go/bin/gosu-i386 --version \
3233
&& /go/bin/gosu-i386 nobody id \
3334
&& /go/bin/gosu-i386 nobody ls -l /proc/self/fd
3435
RUN set -x \

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ sha256sum gosu* | tee SHA256SUMS
1313
file gosu*
1414
ls -lFh gosu* SHA256SUMS*
1515

16-
"./gosu-$(dpkg --print-architecture)" || :
16+
"./gosu-$(dpkg --print-architecture)" --help

main.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package main // import "github.com/tianon/gosu"
22

33
import (
4+
"bytes"
5+
"fmt"
46
"log"
57
"os"
68
"os/exec"
79
"path/filepath"
810
"runtime"
911
"strings"
1012
"syscall"
13+
"text/template"
1114
)
1215

1316
func init() {
@@ -16,19 +19,46 @@ func init() {
1619
runtime.LockOSThread()
1720
}
1821

22+
func version() string {
23+
return fmt.Sprintf(`%s (%s on %s/%s; %s)`, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
24+
}
25+
26+
func usage() string {
27+
t := template.Must(template.New("usage").Parse(`
28+
Usage: {{ .Self }} user-spec command [args]
29+
ie: {{ .Self }} tianon bash
30+
{{ .Self }} nobody:root bash -c 'whoami && id'
31+
{{ .Self }} 1000:1 id
32+
33+
{{ .Self }} version: {{ .Version }}
34+
{{ .Self }} license: GPL-3 (full text at https://github.com/tianon/gosu)
35+
`))
36+
var b bytes.Buffer
37+
template.Must(t, t.Execute(&b, struct {
38+
Self string
39+
Version string
40+
}{
41+
Self: filepath.Base(os.Args[0]),
42+
Version: version(),
43+
}))
44+
return strings.TrimSpace(b.String()) + "\n"
45+
}
46+
1947
func main() {
2048
log.SetFlags(0) // no timestamps on our logs
2149

50+
if len(os.Args) >= 2 {
51+
switch os.Args[1] {
52+
case "--help", "-h", "-?":
53+
fmt.Println(usage())
54+
os.Exit(0)
55+
case "--version", "-v":
56+
fmt.Println(version())
57+
os.Exit(0)
58+
}
59+
}
2260
if len(os.Args) <= 2 {
23-
self := filepath.Base(os.Args[0])
24-
log.Printf("Usage: %s user-spec command [args]", self)
25-
log.Printf(" ie: %s tianon bash", self)
26-
log.Printf(" %s nobody:root bash -c 'whoami && id'", self)
27-
log.Printf(" %s 1000:1 id", self)
28-
log.Println()
29-
log.Printf("%s version: %s (%s on %s/%s; %s)", self, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
30-
log.Printf("%s license: GPL-3 (full text at https://github.com/tianon/gosu)\n", strings.Repeat(" ", len(self)))
31-
log.Println()
61+
log.Println(usage())
3262
os.Exit(1)
3363
}
3464

0 commit comments

Comments
 (0)