From ca81475fa5c787099cef3de526dd0fe57b5fb648 Mon Sep 17 00:00:00 2001 From: Pierre-Alain TORET Date: Tue, 10 Mar 2020 23:56:07 +0100 Subject: [PATCH] Add option --config and --data to specify where to store the configuration and the data --- commands/clear.go | 4 ++-- commands/config.go | 2 +- commands/dispatch.go | 14 ++++++++------ config/config.go | 4 ++-- server/server.go | 2 +- storage/storage.go | 17 +++++++++++++---- storage/user.go | 4 ++-- storage/user_cert.go | 8 ++++---- 8 files changed, 33 insertions(+), 22 deletions(-) diff --git a/commands/clear.go b/commands/clear.go index c29048cf..d70d738a 100644 --- a/commands/clear.go +++ b/commands/clear.go @@ -13,14 +13,14 @@ var clearCmd = &cobra.Command{ Use: "clear", Short: "Clear all user data", Run: func(cmd *cobra.Command, args []string) { - err := os.Remove(storage.Path.Database()) + err := os.Remove(storage.DataPath.Database()) if err == nil || os.IsNotExist(err) { log.Println("Database cleared") } else { log.Println(err) } - err = os.RemoveAll(storage.Path.Users()) + err = os.RemoveAll(storage.DataPath.Users()) if err == nil { log.Println("User data cleared") } else { diff --git a/commands/config.go b/commands/config.go index 7772e29c..78316c6b 100644 --- a/commands/config.go +++ b/commands/config.go @@ -16,7 +16,7 @@ var ( Short: "Edit config file", Run: func(cmd *cobra.Command, args []string) { if editor := findEditor(); editor != "" { - process := exec.Command(editor, storage.Path.Config()) + process := exec.Command(editor, storage.ConfigPath.Config()) process.Stdin = os.Stdin process.Stdout = os.Stdout process.Stderr = os.Stderr diff --git a/commands/dispatch.go b/commands/dispatch.go index 8a5a97b1..4899a144 100644 --- a/commands/dispatch.go +++ b/commands/dispatch.go @@ -46,18 +46,18 @@ var rootCmd = &cobra.Command{ fmt.Printf(logo, version.Tag, version.Commit, version.Date, runtime.Version()) } - storage.Initialize(viper.GetString("dir")) + storage.Initialize() - initConfig(storage.Path.Config(), viper.GetBool("reset-config")) + initConfig(storage.ConfigPath.Config(), viper.GetBool("reset-config")) }, Run: func(cmd *cobra.Command, args []string) { if viper.GetBool("dev") { log.Println("Running in development mode, access client at http://localhost:3000") } - log.Println("Storing data at", storage.Path.Root()) + log.Println("Storing data at", storage.DataPath.Root()) - db, err := boltdb.New(storage.Path.Database()) + db, err := boltdb.New(storage.DataPath.Database()) if err != nil { log.Fatal(err) } @@ -77,11 +77,11 @@ var rootCmd = &cobra.Command{ dispatch.SessionStore = db dispatch.GetMessageStore = func(user *storage.User) (storage.MessageStore, error) { - return boltdb.New(storage.Path.Log(user.Username)) + return boltdb.New(storage.DataPath.Log(user.Username)) } dispatch.GetMessageSearchProvider = func(user *storage.User) (storage.MessageSearchProvider, error) { - return bleve.New(storage.Path.Index(user.Username)) + return bleve.New(storage.DataPath.Index(user.Username)) } dispatch.Run() @@ -97,6 +97,8 @@ func init() { rootCmd.AddCommand(configCmd) rootCmd.AddCommand(versionCmd) + rootCmd.PersistentFlags().String("data", storage.DefaultDirectory(), "directory to store data in") + rootCmd.PersistentFlags().String("conf", storage.DefaultDirectory(), "directory to store configuration in") rootCmd.PersistentFlags().String("dir", storage.DefaultDirectory(), "directory to store config and data in") rootCmd.PersistentFlags().Bool("reset-config", false, "reset to the default configuration, overwriting the current one") rootCmd.Flags().StringP("address", "a", "", "interface to which the server will bind") diff --git a/config/config.go b/config/config.go index d25b1afb..beb3228a 100644 --- a/config/config.go +++ b/config/config.go @@ -3,8 +3,8 @@ package config import ( "time" - "github.com/fsnotify/fsnotify" "github.com/khlieng/dispatch/storage" + "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) @@ -53,7 +53,7 @@ type LetsEncrypt struct { func LoadConfig() (*Config, chan *Config) { viper.SetConfigName("config") - viper.AddConfigPath(storage.Path.Root()) + viper.AddConfigPath(storage.ConfigPath.Root()) viper.ReadInConfig() config := &Config{} diff --git a/server/server.go b/server/server.go index 0231bad3..7cb09091 100644 --- a/server/server.go +++ b/server/server.go @@ -147,7 +147,7 @@ func (d *Dispatch) startHTTP() { PortHTTPS: cfg.HTTPS.Port, HTTPOnly: !cfg.HTTPS.Enabled, - StoragePath: storage.Path.LetsEncrypt(), + StoragePath: storage.ConfigPath.LetsEncrypt(), Domain: cfg.LetsEncrypt.Domain, Email: cfg.LetsEncrypt.Email, diff --git a/storage/storage.go b/storage/storage.go index 5b5f3ccd..0206245a 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -5,13 +5,22 @@ import ( "os" "github.com/khlieng/dispatch/pkg/session" + "github.com/spf13/viper" ) -var Path directory +var DataPath directory +var ConfigPath directory -func Initialize(dir string) { - Path = directory(dir) - os.MkdirAll(Path.Root(), 0700) +func Initialize() { + if viper.GetString("dir") != DefaultDirectory() { + DataPath = directory(viper.GetString("dir")) + ConfigPath = directory(viper.GetString("dir")) + } else { + DataPath = directory(viper.GetString("data")) + ConfigPath = directory(viper.GetString("conf")) + } + os.MkdirAll(DataPath.Root(), 0700) + os.MkdirAll(ConfigPath.Root(), 0700) } var ( diff --git a/storage/user.go b/storage/user.go index 737f94d6..c5b83a61 100644 --- a/storage/user.go +++ b/storage/user.go @@ -32,7 +32,7 @@ func NewUser(store Store) (*User, error) { return nil, err } - err = os.MkdirAll(Path.User(user.Username), 0700) + err = os.MkdirAll(DataPath.User(user.Username), 0700) if err != nil { return nil, err } @@ -70,7 +70,7 @@ func (u *User) Remove() { if u.messageIndex != nil { u.messageIndex.Close() } - os.RemoveAll(Path.User(u.Username)) + os.RemoveAll(DataPath.User(u.Username)) } func (u *User) GetLastIP() []byte { diff --git a/storage/user_cert.go b/storage/user_cert.go index c7be127d..48887383 100644 --- a/storage/user_cert.go +++ b/storage/user_cert.go @@ -28,12 +28,12 @@ func (u *User) SetCertificate(certPEM, keyPEM []byte) error { u.certificate = &cert u.lock.Unlock() - err = ioutil.WriteFile(Path.Certificate(u.Username), certPEM, 0600) + err = ioutil.WriteFile(ConfigPath.Certificate(u.Username), certPEM, 0600) if err != nil { return ErrCouldNotSaveCert } - err = ioutil.WriteFile(Path.Key(u.Username), keyPEM, 0600) + err = ioutil.WriteFile(ConfigPath.Key(u.Username), keyPEM, 0600) if err != nil { return ErrCouldNotSaveCert } @@ -42,12 +42,12 @@ func (u *User) SetCertificate(certPEM, keyPEM []byte) error { } func (u *User) loadCertificate() error { - certPEM, err := ioutil.ReadFile(Path.Certificate(u.Username)) + certPEM, err := ioutil.ReadFile(ConfigPath.Certificate(u.Username)) if err != nil { return err } - keyPEM, err := ioutil.ReadFile(Path.Key(u.Username)) + keyPEM, err := ioutil.ReadFile(ConfigPath.Key(u.Username)) if err != nil { return err }