diff options
-rw-r--r-- | env.go | 74 | ||||
-rw-r--r-- | main.go | 61 | ||||
-rw-r--r-- | restore_dir.go | 4 | ||||
-rw-r--r-- | snapshot.go | 30 | ||||
-rw-r--r-- | write_dir.go | 4 |
5 files changed, 99 insertions, 74 deletions
@@ -0,0 +1,74 @@ +package main + +import ( + "code.laria.me/petrific/cache" + "code.laria.me/petrific/config" + "code.laria.me/petrific/storage" + "code.laria.me/petrific/storage/registry" + "fmt" +) + +// Env provides commonly used objects for subcommands +type Env struct { + Conf config.Config + Store storage.Storage + IdCache cache.Cache +} + +func (e *Env) Close() { + if e.IdCache != nil { + e.IdCache.Close() + } + + if e.Store != nil { + e.Store.Close() + } +} + +func (env *Env) loadCache() error { + path := env.Conf.CachePath + if path == "" { + return nil + } + + file_cache := cache.NewFileCache(config.ExpandTilde(path)) + if err := file_cache.Load(); err != nil { + return fmt.Errorf("Loading cache %s: %s", path, err) + } + + env.IdCache = file_cache + return nil +} + +func NewEnv(confPath, storageName string) (*Env, error) { + env := new(Env) + + var err error + + // Load config + + env.Conf, err = config.LoadConfig(confPath) + if err != nil { + return nil, err + } + + // load storage + + if storageName == "" { + storageName = env.Conf.DefaultStorage + } + + env.Store, err = registry.LoadStorage(env.Conf, storageName) + if err != nil { + return nil, err + } + + // Load cache + + if err = env.loadCache(); err != nil { + env.Close() + return nil, err + } + + return env, nil +} @@ -1,16 +1,12 @@ package main import ( - "code.laria.me/petrific/cache" - "code.laria.me/petrific/config" - "code.laria.me/petrific/storage" - "code.laria.me/petrific/storage/registry" "flag" "fmt" "os" ) -type subcmd func(args []string) int +type subcmd func(env *Env, args []string) int var subcmds = map[string]subcmd{ "write-dir": WriteDir, @@ -43,10 +39,6 @@ var ( flagStorage = flag.String("storage", "", "Operate on this storage instead of the default one") ) -var conf config.Config -var objectstore storage.Storage -var id_cache cache.Cache = cache.NopCache{} - func main() { os.Exit(Main()) } @@ -61,15 +53,13 @@ func Main() int { flag.PrintDefaults() } flag.Parse() - if !loadConfig() { - return 1 - } - defer objectstore.Close() - if !loadCache(conf) { + env, err := NewEnv(*flagConfPath, *flagStorage) + if err != nil { + fmt.Fprintln(os.Stderr, err) return 1 } - defer id_cache.Close() + defer env.Close() remaining := make([]string, 0) for _, arg := range flag.Args() { @@ -88,44 +78,5 @@ func Main() int { return 1 } - return cmd(remaining[1:]) -} - -func loadCache(conf config.Config) bool { - if conf.CachePath == "" { - return true - } - - file_cache := cache.NewFileCache(config.ExpandTilde(conf.CachePath)) - if err := file_cache.Load(); err != nil { - fmt.Fprintf(os.Stderr, "Loading cache %s: %s", conf.CachePath, err) - return false - } - - id_cache = file_cache - - return true -} - -func loadConfig() bool { - var err error - conf, err = config.LoadConfig(*flagConfPath) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed reading config: %s\n", err) - return false - } - - storageName := *flagStorage - if storageName == "" { - storageName = conf.DefaultStorage - } - - s, err := registry.LoadStorage(conf, storageName) - if err != nil { - fmt.Fprintln(os.Stderr, err) - return false - } - - objectstore = s - return true + return cmd(env, remaining[1:]) } diff --git a/restore_dir.go b/restore_dir.go index 2961b70..4e45fae 100644 --- a/restore_dir.go +++ b/restore_dir.go @@ -7,7 +7,7 @@ import ( "fmt" ) -func RestoreDir(args []string) int { +func RestoreDir(env *Env, args []string) int { usage := subcmdUsage("restore-dir", "directory object-id", nil) errout := subcmdErrout("restore-dir") @@ -39,7 +39,7 @@ func RestoreDir(args []string) int { return 1 } - if err := backup.RestoreDir(objectstore, id, d); err != nil { + if err := backup.RestoreDir(env.Store, id, d); err != nil { errout(err) return 1 } diff --git a/snapshot.go b/snapshot.go index db181ba..ff4c0be 100644 --- a/snapshot.go +++ b/snapshot.go @@ -14,7 +14,7 @@ import ( "time" ) -func createSnapshot(archive, comment string, tree_id objects.ObjectId, nosign bool) (objects.ObjectId, error) { +func createSnapshot(env *Env, archive, comment string, tree_id objects.ObjectId, nosign bool) (objects.ObjectId, error) { snapshot := objects.Snapshot{ Archive: archive, Comment: comment, @@ -28,7 +28,7 @@ func createSnapshot(archive, comment string, tree_id objects.ObjectId, nosign bo payload = snapshot.Payload() } else { var err error - payload, err = snapshot.SignedPayload(conf.GPGSigner()) + payload, err = snapshot.SignedPayload(env.Conf.GPGSigner()) if err != nil { return objects.ObjectId{}, fmt.Errorf("could not sign: %s", err) } @@ -39,10 +39,10 @@ func createSnapshot(archive, comment string, tree_id objects.ObjectId, nosign bo Payload: payload, } - return storage.SetObject(objectstore, obj) + return storage.SetObject(env.Store, obj) } -func CreateSnapshot(args []string) int { +func CreateSnapshot(env *Env, args []string) int { flags := flag.NewFlagSet(os.Args[0]+" create-snapshot", flag.ContinueOnError) nosign := flags.Bool("nosign", false, "don't sign the snapshot (not recommended)") comment := flags.String("comment", "", "comment for the snapshot") @@ -68,7 +68,7 @@ func CreateSnapshot(args []string) int { return 1 } - snapshot_id, err := createSnapshot(args[0], *comment, tree_id, *nosign) + snapshot_id, err := createSnapshot(env, args[0], *comment, tree_id, *nosign) if err != nil { errout(err) return 1 @@ -78,7 +78,7 @@ func CreateSnapshot(args []string) int { return 0 } -func TakeSnapshot(args []string) int { +func TakeSnapshot(env *Env, args []string) int { flags := flag.NewFlagSet(os.Args[0]+" take-snapshot", flag.ContinueOnError) nosign := flags.Bool("nosign", false, "don't sign the snapshot (not recommended)") comment := flags.String("comment", "", "comment for the snapshot") @@ -114,13 +114,13 @@ func TakeSnapshot(args []string) int { return 1 } - tree_id, err := backup.WriteDir(objectstore, dir_path, d, id_cache) + tree_id, err := backup.WriteDir(env.Store, dir_path, d, env.IdCache) if err != nil { errout(err) return 1 } - snapshot_id, err := createSnapshot(args[0], *comment, tree_id, *nosign) + snapshot_id, err := createSnapshot(env, args[0], *comment, tree_id, *nosign) if err != nil { errout(err) fmt.Fprintf(os.Stderr, "You can try again by running `%s create-snapshot -c '%s' '%s' '%s'\n`", os.Args[0], *comment, args[0], tree_id) @@ -142,7 +142,7 @@ func (s sortableSnapshots) Len() int { return len(s) } func (s sortableSnapshots) Less(i, j int) bool { return s[i].snapshot.Date.After(s[j].snapshot.Date) } func (s sortableSnapshots) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func ListSnapshots(args []string) int { +func ListSnapshots(env *Env, args []string) int { // usage := subcmdUsage("list-snapshots", "[archive]", nil) errout := subcmdErrout("list-snapshots") @@ -154,7 +154,7 @@ func ListSnapshots(args []string) int { } } - objids, err := objectstore.List(objects.OTSnapshot) + objids, err := env.Store.List(objects.OTSnapshot) if err != nil { errout(err) return 1 @@ -164,7 +164,7 @@ func ListSnapshots(args []string) int { failed := false for _, objid := range objids { - _snapshot, err := storage.GetObjectOfType(objectstore, objid, objects.OTSnapshot) + _snapshot, err := storage.GetObjectOfType(env.Store, objid, objects.OTSnapshot) if err != nil { fmt.Fprintf(os.Stderr, "warning: list-snapshots: could not get snapshot %s: %s\n", objid, err) failed = true @@ -192,7 +192,7 @@ func ListSnapshots(args []string) int { return 0 } -func RestoreSnapshot(args []string) int { +func RestoreSnapshot(env *Env, args []string) int { var snapshotId objects.ObjectId flags := flag.NewFlagSet("restore-snapshot", flag.ContinueOnError) flags.Var(&snapshotId, "id", "Object id of a snapshot") @@ -247,13 +247,13 @@ func RestoreSnapshot(args []string) int { var snapshot *objects.Snapshot if *archive != "" { - snapshot, err = storage.FindLatestSnapshot(objectstore, *archive) + snapshot, err = storage.FindLatestSnapshot(env.Store, *archive) if err != nil { errout(err) return 1 } } else { - _snapshot, err := storage.GetObjectOfType(objectstore, snapshotId, objects.OTSnapshot) + _snapshot, err := storage.GetObjectOfType(env.Store, snapshotId, objects.OTSnapshot) if err != nil { errout(err) return 1 @@ -266,7 +266,7 @@ func RestoreSnapshot(args []string) int { return 1 } - if err := backup.RestoreDir(objectstore, snapshot.Tree, root); err != nil { + if err := backup.RestoreDir(env.Store, snapshot.Tree, root); err != nil { errout(err) return 1 } diff --git a/write_dir.go b/write_dir.go index 3ee5a97..d7c78e6 100644 --- a/write_dir.go +++ b/write_dir.go @@ -19,7 +19,7 @@ func abspath(p string) (string, error) { return path.Clean(p), nil } -func WriteDir(args []string) int { +func WriteDir(env *Env, args []string) int { usage := subcmdUsage("write-dir", "directory", nil) errout := subcmdErrout("write-dir") @@ -45,7 +45,7 @@ func WriteDir(args []string) int { return 1 } - id, err := backup.WriteDir(objectstore, dir_path, d, id_cache) + id, err := backup.WriteDir(env.Store, dir_path, d, env.IdCache) if err != nil { errout(err) return 1 |