mirror of
https://github.com/octoleo/restic.git
synced 2025-01-04 23:55:30 +00:00
Add Makefile and version.sh, add 'version' command
This commit is contained in:
parent
ebd4f97350
commit
8277daa9e1
18
cmd/khepri/Makefile
Normal file
18
cmd/khepri/Makefile
Normal 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
|
@ -128,6 +128,4 @@ func commandCat(be backend.Server, key *khepri.Key, args []string) error {
|
|||||||
default:
|
default:
|
||||||
return errors.New("invalid type")
|
return errors.New("invalid type")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
37
cmd/khepri/debug.go
Normal file
37
cmd/khepri/debug.go
Normal 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...)
|
||||||
|
}
|
5
cmd/khepri/debug_release.go
Normal file
5
cmd/khepri/debug_release.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// +build !debug
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func debug(fmt string, args ...interface{}) {}
|
@ -15,7 +15,9 @@ import (
|
|||||||
"github.com/jessevdk/go-flags"
|
"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"`
|
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
|
var commands map[string]commandFunc
|
||||||
|
|
||||||
func read_password(prompt string) string {
|
func readPassword(prompt string) string {
|
||||||
p := os.Getenv("KHEPRI_PASSWORD")
|
p := os.Getenv("KHEPRI_PASSWORD")
|
||||||
if p != "" {
|
if p != "" {
|
||||||
return p
|
return p
|
||||||
@ -48,8 +50,8 @@ func read_password(prompt string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func commandInit(repo string) error {
|
func commandInit(repo string) error {
|
||||||
pw := read_password("enter password for new backend: ")
|
pw := readPassword("enter password for new backend: ")
|
||||||
pw2 := read_password("enter password again: ")
|
pw2 := readPassword("enter password again: ")
|
||||||
|
|
||||||
if pw != pw2 {
|
if pw != pw2 {
|
||||||
errx(1, "passwords do not match")
|
errx(1, "passwords do not match")
|
||||||
@ -85,7 +87,8 @@ func open(u string) (backend.Server, error) {
|
|||||||
|
|
||||||
if url.Scheme == "" {
|
if url.Scheme == "" {
|
||||||
return backend.OpenLocal(url.Path)
|
return backend.OpenLocal(url.Path)
|
||||||
} else {
|
}
|
||||||
|
|
||||||
args := []string{url.Host}
|
args := []string{url.Host}
|
||||||
if url.User != nil && url.User.Username() != "" {
|
if url.User != nil && url.User.Username() != "" {
|
||||||
args = append(args, "-l")
|
args = append(args, "-l")
|
||||||
@ -94,7 +97,6 @@ func open(u string) (backend.Server, error) {
|
|||||||
args = append(args, "-s")
|
args = append(args, "-s")
|
||||||
args = append(args, "sftp")
|
args = append(args, "sftp")
|
||||||
return backend.OpenSFTP(url.Path[1:], "ssh", args...)
|
return backend.OpenSFTP(url.Path[1:], "ssh", args...)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the backend specified by URI.
|
// Create the backend specified by URI.
|
||||||
@ -106,7 +108,8 @@ func create(u string) (backend.Server, error) {
|
|||||||
|
|
||||||
if url.Scheme == "" {
|
if url.Scheme == "" {
|
||||||
return backend.CreateLocal(url.Path)
|
return backend.CreateLocal(url.Path)
|
||||||
} else {
|
}
|
||||||
|
|
||||||
args := []string{url.Host}
|
args := []string{url.Host}
|
||||||
if url.User != nil && url.User.Username() != "" {
|
if url.User != nil && url.User.Username() != "" {
|
||||||
args = append(args, "-l")
|
args = append(args, "-l")
|
||||||
@ -115,7 +118,6 @@ func create(u string) (backend.Server, error) {
|
|||||||
args = append(args, "-s")
|
args = append(args, "-s")
|
||||||
args = append(args, "sftp")
|
args = append(args, "sftp")
|
||||||
return backend.CreateSFTP(url.Path[1:], "ssh", args...)
|
return backend.CreateSFTP(url.Path[1:], "ssh", args...)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -131,12 +133,12 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
Opts.Repo = os.Getenv("KHEPRI_REPOSITORY")
|
opts.Repo = os.Getenv("KHEPRI_REPOSITORY")
|
||||||
if Opts.Repo == "" {
|
if opts.Repo == "" {
|
||||||
Opts.Repo = "khepri-backup"
|
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 {
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
@ -153,12 +155,16 @@ func main() {
|
|||||||
|
|
||||||
cmd := args[0]
|
cmd := args[0]
|
||||||
|
|
||||||
if cmd == "init" {
|
switch cmd {
|
||||||
err = commandInit(Opts.Repo)
|
case "init":
|
||||||
|
err = commandInit(opts.Repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errx(1, "error executing command %q: %v", cmd, err)
|
errx(1, "error executing command %q: %v", cmd, err)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
|
||||||
|
case "version":
|
||||||
|
fmt.Printf("%v\n", version)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,12 +174,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read_password("enter password: ")
|
// read_password("enter password: ")
|
||||||
repo, err := open(Opts.Repo)
|
repo, err := open(opts.Repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errx(1, "unable to open repo: %v", err)
|
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 {
|
if err != nil {
|
||||||
errx(2, "unable to open repo: %v", err)
|
errx(2, "unable to open repo: %v", err)
|
||||||
}
|
}
|
||||||
|
13
cmd/khepri/version.sh
Executable file
13
cmd/khepri/version.sh
Executable 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
|
Loading…
Reference in New Issue
Block a user