11package main // import "github.com/tianon/gosu"
22
33import (
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
1316func 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+
1947func 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