From a845956238e6a41479a7abd59f5c39e71f3f85f4 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Thu, 3 Aug 2017 08:08:53 +0200 Subject: Implement write-dir --- fs/os.go | 8 ++++---- main.go | 12 +++++++++++- write_dir.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 write_dir.go 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 +} -- cgit v1.2.3-70-g09d2