From 2cfd9f146d042a1c30176ad91fea730df439704d Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 10 Dec 2017 15:43:58 +0100 Subject: Eliminate global variables --- main.go | 61 ++++++------------------------------------------------------- 1 file changed, 6 insertions(+), 55 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 5ea627f..b6b541b 100644 --- a/main.go +++ b/main.go @@ -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:]) } -- cgit v1.2.3-54-g00ecf