diff --git a/cmd/khepri/Makefile b/cmd/khepri/Makefile new file mode 100644 index 000000000..dab9cbb16 --- /dev/null +++ b/cmd/khepri/Makefile @@ -0,0 +1,18 @@ +# try to get version from git +VERSION = $(shell ./version.sh) +VERSION ?= "unknown version" +LDFLAGS = -X main.version $(VERSION) +TAGS = + +.PHONY: all clean debug + +all: khepri + +khepri: *.go + go build $(TAGS) -ldflags "$(LDFLAGS)" + +debug: TAGS=-tags debug +debug: khepri + +clean: + go clean diff --git a/cmd/khepri/cmd_cat.go b/cmd/khepri/cmd_cat.go index 92a5d8f71..1709be102 100644 --- a/cmd/khepri/cmd_cat.go +++ b/cmd/khepri/cmd_cat.go @@ -128,6 +128,4 @@ func commandCat(be backend.Server, key *khepri.Key, args []string) error { default: return errors.New("invalid type") } - - return nil } diff --git a/cmd/khepri/debug.go b/cmd/khepri/debug.go new file mode 100644 index 000000000..687f0f162 --- /dev/null +++ b/cmd/khepri/debug.go @@ -0,0 +1,37 @@ +// +build debug + +package main + +import ( + "fmt" + "io" + "log" + "os" + "path/filepath" + "time" +) + +var debugLogger = initDebugLogger() + +func initDebugLogger() *log.Logger { + // create new log file + filename := fmt.Sprintf("khepri-debug-%d-%s", + os.Getpid(), time.Now().Format("20060201-150405")) + f, err := os.OpenFile(filepath.Join(os.TempDir(), filename), + os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to create debug log file: %v", err) + os.Exit(2) + } + + // open logger + l := log.New(io.MultiWriter(os.Stderr, f), "DEBUG: ", log.LstdFlags) + fmt.Fprintf(os.Stderr, "logging activated, writing log file %s", filename) + l.Printf("khepri %s", version) + + return l +} + +func debug(fmt string, args ...interface{}) { + debugLogger.Printf(fmt, args...) +} diff --git a/cmd/khepri/debug_release.go b/cmd/khepri/debug_release.go new file mode 100644 index 000000000..77f5c1b76 --- /dev/null +++ b/cmd/khepri/debug_release.go @@ -0,0 +1,5 @@ +// +build !debug + +package main + +func debug(fmt string, args ...interface{}) {} diff --git a/cmd/khepri/main.go b/cmd/khepri/main.go index 823878e25..719a84ee8 100644 --- a/cmd/khepri/main.go +++ b/cmd/khepri/main.go @@ -15,7 +15,9 @@ import ( "github.com/jessevdk/go-flags" ) -var Opts struct { +var version = "compiled manually" + +var opts struct { Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"` } @@ -31,7 +33,7 @@ type commandFunc func(backend.Server, *khepri.Key, []string) error var commands map[string]commandFunc -func read_password(prompt string) string { +func readPassword(prompt string) string { p := os.Getenv("KHEPRI_PASSWORD") if p != "" { return p @@ -48,8 +50,8 @@ func read_password(prompt string) string { } func commandInit(repo string) error { - pw := read_password("enter password for new backend: ") - pw2 := read_password("enter password again: ") + pw := readPassword("enter password for new backend: ") + pw2 := readPassword("enter password again: ") if pw != pw2 { errx(1, "passwords do not match") @@ -85,16 +87,16 @@ func open(u string) (backend.Server, error) { if url.Scheme == "" { return backend.OpenLocal(url.Path) - } else { - args := []string{url.Host} - if url.User != nil && url.User.Username() != "" { - args = append(args, "-l") - args = append(args, url.User.Username()) - } - args = append(args, "-s") - args = append(args, "sftp") - return backend.OpenSFTP(url.Path[1:], "ssh", args...) } + + args := []string{url.Host} + if url.User != nil && url.User.Username() != "" { + args = append(args, "-l") + args = append(args, url.User.Username()) + } + args = append(args, "-s") + args = append(args, "sftp") + return backend.OpenSFTP(url.Path[1:], "ssh", args...) } // Create the backend specified by URI. @@ -106,16 +108,16 @@ func create(u string) (backend.Server, error) { if url.Scheme == "" { return backend.CreateLocal(url.Path) - } else { - args := []string{url.Host} - if url.User != nil && url.User.Username() != "" { - args = append(args, "-l") - args = append(args, url.User.Username()) - } - args = append(args, "-s") - args = append(args, "sftp") - return backend.CreateSFTP(url.Path[1:], "ssh", args...) } + + args := []string{url.Host} + if url.User != nil && url.User.Username() != "" { + args = append(args, "-l") + args = append(args, url.User.Username()) + } + args = append(args, "-s") + args = append(args, "sftp") + return backend.CreateSFTP(url.Path[1:], "ssh", args...) } func init() { @@ -131,12 +133,12 @@ func init() { func main() { log.SetOutput(os.Stdout) - Opts.Repo = os.Getenv("KHEPRI_REPOSITORY") - if Opts.Repo == "" { - Opts.Repo = "khepri-backup" + opts.Repo = os.Getenv("KHEPRI_REPOSITORY") + if opts.Repo == "" { + opts.Repo = "khepri-backup" } - args, err := flags.Parse(&Opts) + args, err := flags.Parse(&opts) if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp { os.Exit(0) } @@ -153,12 +155,16 @@ func main() { cmd := args[0] - if cmd == "init" { - err = commandInit(Opts.Repo) + switch cmd { + case "init": + err = commandInit(opts.Repo) if err != nil { errx(1, "error executing command %q: %v", cmd, err) } + return + case "version": + fmt.Printf("%v\n", version) return } @@ -168,12 +174,12 @@ func main() { } // read_password("enter password: ") - repo, err := open(Opts.Repo) + repo, err := open(opts.Repo) if err != nil { errx(1, "unable to open repo: %v", err) } - key, err := khepri.SearchKey(repo, read_password("Enter Password for Repository: ")) + key, err := khepri.SearchKey(repo, readPassword("Enter Password for Repository: ")) if err != nil { errx(2, "unable to open repo: %v", err) } diff --git a/cmd/khepri/version.sh b/cmd/khepri/version.sh new file mode 100755 index 000000000..6abc6f8ee --- /dev/null +++ b/cmd/khepri/version.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +VERSION=$(git log --max-count=1 --pretty='%ad-%h' --date=short HEAD 2>/dev/null) + +if [ -n "$VERSION" ]; then + if ! sh -c "git diff -s --exit-code && git diff --cached -s --exit-code"; then + VERSION+="+" + fi +else + VERSION="unknown version" +fi + +echo $VERSION