Add user storage test
This commit is contained in:
parent
0c5b4bb04e
commit
137cf3224d
6 changed files with 105 additions and 33 deletions
|
@ -1,6 +1,9 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/cobra"
|
||||
|
||||
"github.com/khlieng/dispatch/storage"
|
||||
|
@ -8,8 +11,20 @@ import (
|
|||
|
||||
var clearCmd = &cobra.Command{
|
||||
Use: "clear",
|
||||
Short: "Clear all application data",
|
||||
Short: "Clear database and message logs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
storage.Clear()
|
||||
err := os.Remove(storage.Path.Database())
|
||||
if err == nil || os.IsNotExist(err) {
|
||||
log.Println("Database cleared")
|
||||
} else {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
err = os.RemoveAll(storage.Path.Logs())
|
||||
if err == nil {
|
||||
log.Println("Logs cleared")
|
||||
} else {
|
||||
log.Println(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
@ -27,29 +27,26 @@ const logo = `
|
|||
var rootCmd = &cobra.Command{
|
||||
Use: "dispatch",
|
||||
Short: "Web-based IRC client in Go.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
storage.Initialize()
|
||||
server.Run()
|
||||
},
|
||||
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
if cmd.Use == "dispatch" {
|
||||
fmt.Println(logo)
|
||||
}
|
||||
|
||||
storage.SetDirectory(viper.GetString("dir"))
|
||||
storage.Initialize(viper.GetString("dir"))
|
||||
|
||||
err := os.MkdirAll(storage.Path.Logs(), 0700)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
initConfig()
|
||||
initConfig(storage.Path.Config())
|
||||
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath(storage.Path.Root())
|
||||
viper.ReadInConfig()
|
||||
},
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
log.Println("Storing data at", storage.Path.Root())
|
||||
|
||||
storage.Open()
|
||||
server.Run()
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
|
@ -67,9 +64,7 @@ func init() {
|
|||
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
configPath := storage.Path.Config()
|
||||
|
||||
func initConfig(configPath string) {
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
config, err := assets.Asset("config.default.toml")
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue