Add clear command and get rid of the development flag

This commit is contained in:
khlieng 2015-05-02 01:02:21 +02:00
parent d1a82a65b5
commit 79095154ca
4 changed files with 40 additions and 20 deletions

15
commands/clear.go Normal file
View File

@ -0,0 +1,15 @@
package commands
import (
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/khlieng/name_pending/storage"
)
var clearCmd = &cobra.Command{
Use: "clear",
Short: "Clear all application data",
Run: func(cmd *cobra.Command, args []string) {
storage.Clear()
},
}

View File

@ -9,20 +9,20 @@ import (
var ( var (
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "name_pending", Use: "name_pending",
Long: "Web-based IRC client in Go.", Short: "Web-based IRC client in Go.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
storage.Initialize(development) storage.Initialize()
server.Run(port, development) server.Run(port)
}, },
} }
development bool port int
port int
) )
func init() { func init() {
rootCmd.Flags().BoolVarP(&development, "dev", "d", false, "development mode") rootCmd.AddCommand(clearCmd)
rootCmd.Flags().IntVarP(&port, "port", "p", 1337, "port to listen on") rootCmd.Flags().IntVarP(&port, "port", "p", 1337, "port to listen on")
} }

View File

@ -31,8 +31,8 @@ type File struct {
ContentType string ContentType string
} }
func Run(port int, development bool) { func Run(port int) {
defer storage.Cleanup() defer storage.Close()
channelStore = storage.NewChannelStore() channelStore = storage.NewChannelStore()
sessions = make(map[string]*Session) sessions = make(map[string]*Session)
@ -48,9 +48,7 @@ func Run(port int, development bool) {
File{"/font/fontello.woff", "application/font-woff"}, File{"/font/fontello.woff", "application/font-woff"},
} }
if !development { reconnect()
reconnect()
}
router := httprouter.New() router := httprouter.New()

View File

@ -20,15 +20,18 @@ var (
bucketMessages = []byte("Messages") bucketMessages = []byte("Messages")
) )
func Initialize(clean bool) { func init() {
var err error currentUser, err := user.Current()
currentUser, _ := user.Current() if err != nil {
appDir = path.Join(currentUser.HomeDir, ".name_pending") log.Fatal(err)
if clean {
os.RemoveAll(appDir)
} }
appDir = path.Join(currentUser.HomeDir, ".name_pending")
}
func Initialize() {
var err error
os.Mkdir(appDir, 0777) os.Mkdir(appDir, 0777)
os.Mkdir(path.Join(appDir, "logs"), 0777) os.Mkdir(path.Join(appDir, "logs"), 0777)
@ -46,6 +49,10 @@ func Initialize(clean bool) {
}) })
} }
func Cleanup() { func Close() {
db.Close() db.Close()
} }
func Clear() {
os.RemoveAll(appDir)
}