Add Makefile and version.sh, add 'version' command

This commit is contained in:
Alexander Neumann 2014-11-15 19:08:15 +01:00
parent ebd4f97350
commit 8277daa9e1
6 changed files with 109 additions and 32 deletions

18
cmd/khepri/Makefile Normal file
View File

@ -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

View File

@ -128,6 +128,4 @@ func commandCat(be backend.Server, key *khepri.Key, args []string) error {
default:
return errors.New("invalid type")
}
return nil
}

37
cmd/khepri/debug.go Normal file
View File

@ -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...)
}

View File

@ -0,0 +1,5 @@
// +build !debug
package main
func debug(fmt string, args ...interface{}) {}

View File

@ -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)
}

13
cmd/khepri/version.sh Executable file
View File

@ -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