1
0
mirror of https://github.com/tianon/gosu.git synced 2025-04-18 19:04:06 +03:00

Add "--help" and "--version" as explicit flags

This commit is contained in:
Tianon Gravi 2018-01-16 13:41:26 -08:00
parent e87cf95808
commit 3414434bef
3 changed files with 43 additions and 12 deletions

View File

@ -1,4 +1,4 @@
FROM golang:1.7-alpine
FROM golang:1.9-alpine
RUN apk add --no-cache ca-certificates file openssl
@ -23,12 +23,13 @@ WORKDIR /go/src/github.com/tianon/gosu
RUN set -x \
&& eval "GOARCH=amd64 go build $BUILD_FLAGS -o /go/bin/gosu-amd64" \
&& file /go/bin/gosu-amd64 \
&& { /go/bin/gosu-amd64 || true; } \
&& /go/bin/gosu-amd64 --version \
&& /go/bin/gosu-amd64 nobody id \
&& /go/bin/gosu-amd64 nobody ls -l /proc/self/fd
RUN set -x \
&& eval "GOARCH=386 go build $BUILD_FLAGS -o /go/bin/gosu-i386" \
&& file /go/bin/gosu-i386 \
&& /go/bin/gosu-i386 --version \
&& /go/bin/gosu-i386 nobody id \
&& /go/bin/gosu-i386 nobody ls -l /proc/self/fd
RUN set -x \

View File

@ -13,4 +13,4 @@ sha256sum gosu* | tee SHA256SUMS
file gosu*
ls -lFh gosu* SHA256SUMS*
"./gosu-$(dpkg --print-architecture)" || :
"./gosu-$(dpkg --print-architecture)" --help

48
main.go
View File

@ -1,6 +1,8 @@
package main // import "github.com/tianon/gosu"
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
@ -8,6 +10,7 @@ import (
"runtime"
"strings"
"syscall"
"text/template"
)
func init() {
@ -16,19 +19,46 @@ func init() {
runtime.LockOSThread()
}
func version() string {
return fmt.Sprintf(`%s (%s on %s/%s; %s)`, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
}
func usage() string {
t := template.Must(template.New("usage").Parse(`
Usage: {{ .Self }} user-spec command [args]
ie: {{ .Self }} tianon bash
{{ .Self }} nobody:root bash -c 'whoami && id'
{{ .Self }} 1000:1 id
{{ .Self }} version: {{ .Version }}
{{ .Self }} license: GPL-3 (full text at https://github.com/tianon/gosu)
`))
var b bytes.Buffer
template.Must(t, t.Execute(&b, struct {
Self string
Version string
}{
Self: filepath.Base(os.Args[0]),
Version: version(),
}))
return strings.TrimSpace(b.String()) + "\n"
}
func main() {
log.SetFlags(0) // no timestamps on our logs
if len(os.Args) >= 2 {
switch os.Args[1] {
case "--help", "-h", "-?":
fmt.Println(usage())
os.Exit(0)
case "--version", "-v":
fmt.Println(version())
os.Exit(0)
}
}
if len(os.Args) <= 2 {
self := filepath.Base(os.Args[0])
log.Printf("Usage: %s user-spec command [args]", self)
log.Printf(" ie: %s tianon bash", self)
log.Printf(" %s nobody:root bash -c 'whoami && id'", self)
log.Printf(" %s 1000:1 id", self)
log.Println()
log.Printf("%s version: %s (%s on %s/%s; %s)", self, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
log.Printf("%s license: GPL-3 (full text at https://github.com/tianon/gosu)\n", strings.Repeat(" ", len(self)))
log.Println()
log.Println(usage())
os.Exit(1)
}