From 79095154ca54e7cfac57e7a73db457627d060496 Mon Sep 17 00:00:00 2001 From: khlieng Date: Sat, 2 May 2015 01:02:21 +0200 Subject: [PATCH] Add clear command and get rid of the development flag --- commands/clear.go | 15 +++++++++++++++ commands/name_pending.go | 14 +++++++------- server/server.go | 8 +++----- storage/storage.go | 23 +++++++++++++++-------- 4 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 commands/clear.go diff --git a/commands/clear.go b/commands/clear.go new file mode 100644 index 00000000..b4811b7a --- /dev/null +++ b/commands/clear.go @@ -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() + }, +} diff --git a/commands/name_pending.go b/commands/name_pending.go index 2ebb3fe2..794c44fc 100644 --- a/commands/name_pending.go +++ b/commands/name_pending.go @@ -9,20 +9,20 @@ import ( var ( rootCmd = &cobra.Command{ - Use: "name_pending", - Long: "Web-based IRC client in Go.", + Use: "name_pending", + Short: "Web-based IRC client in Go.", Run: func(cmd *cobra.Command, args []string) { - storage.Initialize(development) - server.Run(port, development) + storage.Initialize() + server.Run(port) }, } - development bool - port int + port int ) 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") } diff --git a/server/server.go b/server/server.go index b4323e6c..a72d4a76 100644 --- a/server/server.go +++ b/server/server.go @@ -31,8 +31,8 @@ type File struct { ContentType string } -func Run(port int, development bool) { - defer storage.Cleanup() +func Run(port int) { + defer storage.Close() channelStore = storage.NewChannelStore() sessions = make(map[string]*Session) @@ -48,9 +48,7 @@ func Run(port int, development bool) { File{"/font/fontello.woff", "application/font-woff"}, } - if !development { - reconnect() - } + reconnect() router := httprouter.New() diff --git a/storage/storage.go b/storage/storage.go index 00b5ddd0..27959b05 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -20,15 +20,18 @@ var ( bucketMessages = []byte("Messages") ) -func Initialize(clean bool) { - var err error - currentUser, _ := user.Current() - appDir = path.Join(currentUser.HomeDir, ".name_pending") - - if clean { - os.RemoveAll(appDir) +func init() { + currentUser, err := user.Current() + if err != nil { + log.Fatal(err) } + appDir = path.Join(currentUser.HomeDir, ".name_pending") +} + +func Initialize() { + var err error + os.Mkdir(appDir, 0777) os.Mkdir(path.Join(appDir, "logs"), 0777) @@ -46,6 +49,10 @@ func Initialize(clean bool) { }) } -func Cleanup() { +func Close() { db.Close() } + +func Clear() { + os.RemoveAll(appDir) +}