aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
+}