aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2017-08-03 08:08:53 +0200
committerLaria Carolin Chabowski <laria@laria.me>2017-09-26 21:35:40 +0200
commita845956238e6a41479a7abd59f5c39e71f3f85f4 (patch)
treec611d5bebf7659e0d52bae4d3693957856eb1014
parent9f8ced40c574054de052849c43f69b66e1e91640 (diff)
downloadpetrific-a845956238e6a41479a7abd59f5c39e71f3f85f4.tar.gz
petrific-a845956238e6a41479a7abd59f5c39e71f3f85f4.tar.bz2
petrific-a845956238e6a41479a7abd59f5c39e71f3f85f4.zip
Implement write-dir
-rw-r--r--fs/os.go8
-rw-r--r--main.go12
-rw-r--r--write_dir.go49
3 files changed, 64 insertions, 5 deletions
diff --git a/fs/os.go b/fs/os.go
index 0a4f614..f41d111 100644
--- a/fs/os.go
+++ b/fs/os.go
@@ -11,7 +11,7 @@ func pathJoin(parts ...string) string {
return strings.Join(parts, string(os.PathSeparator))
}
-func openOSFile(path string) (osFile, error) {
+func OpenOSFile(path string) (osFile, error) {
fi, err := os.Lstat(path)
if err != nil {
return osFile{}, err
@@ -118,7 +118,7 @@ func (f osFile) CreateChildFile(name string, exec bool) (RegularFile, error) {
}
fh.Close()
- return openOSFile(p)
+ return OpenOSFile(p)
}
func (f osFile) CreateChildDir(name string) (Dir, error) {
@@ -128,7 +128,7 @@ func (f osFile) CreateChildDir(name string) (Dir, error) {
return nil, err
}
- return openOSFile(p)
+ return OpenOSFile(p)
}
func (f osFile) CreateChildSymlink(name string, target string) (Symlink, error) {
@@ -139,7 +139,7 @@ func (f osFile) CreateChildSymlink(name string, target string) (Symlink, error)
return nil, err
}
- return openOSFile(p)
+ return OpenOSFile(p)
}
func (f osFile) RenameChild(oldname, newname string) error {
diff --git a/main.go b/main.go
index c753a01..9057231 100644
--- a/main.go
+++ b/main.go
@@ -11,13 +11,23 @@ import (
type subcmd func(args []string) int
var subcmds = map[string]subcmd{
- "write-dir": notImplementedYet,
+ "write-dir": WriteDir,
"restore-dir": notImplementedYet,
"take-snapshot": notImplementedYet,
"create-snapshot": notImplementedYet,
"list-snapshots": notImplementedYet,
}
+func subcmdUsage(name string, usage string, flags *flag.FlagSet) func() {
+ return func() {
+ fmt.Fprintf(os.Stderr, "Usage: %s %s %s\n", os.Args[0], name, usage)
+ if flags != nil {
+ fmt.Fprintln(os.Stderr, "\nFlags:")
+ flags.PrintDefaults()
+ }
+ }
+}
+
// Global flags
var (
flagConfPath = flag.String("config", "", "Use this config file instead of the default")
diff --git a/write_dir.go b/write_dir.go
new file mode 100644
index 0000000..cc148c2
--- /dev/null
+++ b/write_dir.go
@@ -0,0 +1,49 @@
+package main
+
+import (
+ "code.laria.me/petrific/backup"
+ "code.laria.me/petrific/cache"
+ "code.laria.me/petrific/fs"
+ "fmt"
+ "os"
+ "path"
+)
+
+func WriteDir(args []string) int {
+ if len(args) != 1 || len(args[0]) == 0 {
+ subcmdUsage("write-dir", "directory", nil)()
+ return 2
+ }
+
+ dir_path := args[0]
+ // Make path absolute
+ if dir_path[0] != '/' {
+ pwd, err := os.Getwd()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "write-dir: %s", err)
+ return 1
+ }
+ dir_path = pwd + "/" + dir_path
+ }
+ dir_path = path.Clean(dir_path)
+
+ d, err := fs.OpenOSFile(dir_path)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "write-dir: %s", err)
+ return 1
+ }
+
+ if d.Type() != fs.FDir {
+ fmt.Fprintf(os.Stderr, "write-dir: %s is not a directory", dir_path)
+ return 1
+ }
+
+ id, err := backup.WriteDir(objectstore, dir_path, d, cache.NopCache{})
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "write-dir: %s", err)
+ return 1
+ }
+
+ fmt.Println(id)
+ return 0
+}