diff --git a/commands/clear.go b/commands/clear.go index d70d738a..c29048cf 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.DataPath.Database()) + err := os.Remove(storage.Path.Database()) if err == nil || os.IsNotExist(err) { log.Println("Database cleared") } else { log.Println(err) } - err = os.RemoveAll(storage.DataPath.Users()) + err = os.RemoveAll(storage.Path.Users()) if err == nil { log.Println("User data cleared") } else { diff --git a/commands/config.go b/commands/config.go index 78316c6b..7772e29c 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.ConfigPath.Config()) + process := exec.Command(editor, storage.Path.Config()) process.Stdin = os.Stdin process.Stdout = os.Stdout process.Stderr = os.Stderr diff --git a/commands/dispatch.go b/commands/dispatch.go index 4899a144..32851c7b 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() + storage.Initialize(viper.GetString("dir"), viper.GetString("data"), viper.GetString("conf")) - initConfig(storage.ConfigPath.Config(), viper.GetBool("reset-config")) + initConfig(storage.Path.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.DataPath.Root()) + log.Println("Storing data at", storage.Path.DataRoot()) - db, err := boltdb.New(storage.DataPath.Database()) + db, err := boltdb.New(storage.Path.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.DataPath.Log(user.Username)) + return boltdb.New(storage.Path.Log(user.Username)) } dispatch.GetMessageSearchProvider = func(user *storage.User) (storage.MessageSearchProvider, error) { - return bleve.New(storage.DataPath.Index(user.Username)) + return bleve.New(storage.Path.Index(user.Username)) } dispatch.Run() diff --git a/config/config.go b/config/config.go index beb3228a..13448dd1 100644 --- a/config/config.go +++ b/config/config.go @@ -53,7 +53,7 @@ type LetsEncrypt struct { func LoadConfig() (*Config, chan *Config) { viper.SetConfigName("config") - viper.AddConfigPath(storage.ConfigPath.Root()) + viper.AddConfigPath(storage.Path.ConfigRoot()) viper.ReadInConfig() config := &Config{} diff --git a/server/irc_handler_test.go b/server/irc_handler_test.go index 3fa00d05..56d563d2 100644 --- a/server/irc_handler_test.go +++ b/server/irc_handler_test.go @@ -22,7 +22,7 @@ func TestMain(m *testing.M) { log.Fatal(err) } - storage.Initialize(tempdir) + storage.Initialize(tempdir, "", "") db, err := boltdb.New(storage.Path.Database()) if err != nil { diff --git a/server/server.go b/server/server.go index 7cb09091..0231bad3 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.ConfigPath.LetsEncrypt(), + StoragePath: storage.Path.LetsEncrypt(), Domain: cfg.LetsEncrypt.Domain, Email: cfg.LetsEncrypt.Email, diff --git a/storage/directory.go b/storage/directory.go index 8b1a9cdf..0aced016 100644 --- a/storage/directory.go +++ b/storage/directory.go @@ -3,7 +3,7 @@ package storage import ( "path/filepath" - "github.com/mitchellh/go-homedir" + homedir "github.com/mitchellh/go-homedir" ) func DefaultDirectory() string { @@ -11,18 +11,25 @@ func DefaultDirectory() string { return filepath.Join(home, ".dispatch") } -type directory string +type directory struct { + dataRoot string + configRoot string +} -func (d directory) Root() string { - return string(d) +func (d directory) DataRoot() string { + return d.dataRoot +} + +func (d directory) ConfigRoot() string { + return d.configRoot } func (d directory) LetsEncrypt() string { - return filepath.Join(d.Root(), "letsencrypt") + return filepath.Join(d.ConfigRoot(), "letsencrypt") } func (d directory) Users() string { - return filepath.Join(d.Root(), "users") + return filepath.Join(d.DataRoot(), "users") } func (d directory) User(username string) string { @@ -46,9 +53,9 @@ func (d directory) Key(username string) string { } func (d directory) Config() string { - return filepath.Join(d.Root(), "config.toml") + return filepath.Join(d.ConfigRoot(), "config.toml") } func (d directory) Database() string { - return filepath.Join(d.Root(), "dispatch.db") + return filepath.Join(d.DataRoot(), "dispatch.db") } diff --git a/storage/storage.go b/storage/storage.go index 0206245a..a45a0e1e 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -5,22 +5,20 @@ import ( "os" "github.com/khlieng/dispatch/pkg/session" - "github.com/spf13/viper" ) -var DataPath directory -var ConfigPath directory +var Path directory -func Initialize() { - if viper.GetString("dir") != DefaultDirectory() { - DataPath = directory(viper.GetString("dir")) - ConfigPath = directory(viper.GetString("dir")) +func Initialize(root, dataRoot, configRoot string) { + if root != DefaultDirectory() { + Path.dataRoot = root + Path.configRoot = root } else { - DataPath = directory(viper.GetString("data")) - ConfigPath = directory(viper.GetString("conf")) + Path.dataRoot = dataRoot + Path.configRoot = configRoot } - os.MkdirAll(DataPath.Root(), 0700) - os.MkdirAll(ConfigPath.Root(), 0700) + os.MkdirAll(Path.DataRoot(), 0700) + os.MkdirAll(Path.ConfigRoot(), 0700) } var ( diff --git a/storage/user.go b/storage/user.go index c5b83a61..737f94d6 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(DataPath.User(user.Username), 0700) + err = os.MkdirAll(Path.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(DataPath.User(u.Username)) + os.RemoveAll(Path.User(u.Username)) } func (u *User) GetLastIP() []byte { diff --git a/storage/user_cert.go b/storage/user_cert.go index 48887383..c7be127d 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(ConfigPath.Certificate(u.Username), certPEM, 0600) + err = ioutil.WriteFile(Path.Certificate(u.Username), certPEM, 0600) if err != nil { return ErrCouldNotSaveCert } - err = ioutil.WriteFile(ConfigPath.Key(u.Username), keyPEM, 0600) + err = ioutil.WriteFile(Path.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(ConfigPath.Certificate(u.Username)) + certPEM, err := ioutil.ReadFile(Path.Certificate(u.Username)) if err != nil { return err } - keyPEM, err := ioutil.ReadFile(ConfigPath.Key(u.Username)) + keyPEM, err := ioutil.ReadFile(Path.Key(u.Username)) if err != nil { return err } diff --git a/storage/user_test.go b/storage/user_test.go index 3ebcd6bd..f18ccc7c 100644 --- a/storage/user_test.go +++ b/storage/user_test.go @@ -19,7 +19,7 @@ func tempdir() string { } func TestUser(t *testing.T) { - storage.Initialize(tempdir()) + storage.Initialize(tempdir(), "", "") db, err := boltdb.New(storage.Path.Database()) assert.Nil(t, err) @@ -103,7 +103,7 @@ func TestUser(t *testing.T) { } func TestMessages(t *testing.T) { - storage.Initialize(tempdir()) + storage.Initialize(tempdir(), "", "") db, err := boltdb.New(storage.Path.Database()) assert.Nil(t, err)