From 284a35ab091b2c2ec7b0bb58f4a8f28aeb8f6f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Fri, 29 May 2015 01:59:08 +0200 Subject: [PATCH] Add dockerfile, add data directory flag --- .dockerignore | 3 +++ .gitignore | 4 +-- Dockerfile | 10 +++++++ commands/clear.go | 2 +- commands/config.go | 8 +++--- commands/name_pending.go | 56 ++++++++++++++++++++++++++++++++++++---- main.go | 25 ------------------ storage/storage.go | 31 +++++++--------------- storage/user.go | 4 +-- 9 files changed, 81 insertions(+), 62 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..139de497 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +client/dist +client/node_modules \ No newline at end of file diff --git a/.gitignore b/.gitignore index d9f28a1b..d6aca9ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -client/dist/ -client/node_modules/ \ No newline at end of file +client/dist +client/node_modules \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c7f82981 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM golang + +ADD . /go/src/github.com/khlieng/name_pending + +RUN go install github.com/khlieng/name_pending + +VOLUME ["/data"] + +ENTRYPOINT ["/go/bin/name_pending"] +CMD ["-p=8080", "--dir=/data"] \ No newline at end of file diff --git a/commands/clear.go b/commands/clear.go index b4811b7a..49a7d9c8 100644 --- a/commands/clear.go +++ b/commands/clear.go @@ -10,6 +10,6 @@ var clearCmd = &cobra.Command{ Use: "clear", Short: "Clear all application data", Run: func(cmd *cobra.Command, args []string) { - storage.Clear() + storage.Clear(appDir) }, } diff --git a/commands/config.go b/commands/config.go index 6562a85b..92666dcb 100644 --- a/commands/config.go +++ b/commands/config.go @@ -7,19 +7,15 @@ import ( "path" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra" - - "github.com/khlieng/name_pending/storage" ) var ( - editors = []string{"nano", "notepad", "vi", "emacs"} - configCmd = &cobra.Command{ Use: "config", Short: "Edit config file", Run: func(cmd *cobra.Command, args []string) { if editor := findEditor(); editor != "" { - process := exec.Command(editor, path.Join(storage.AppDir, "config.toml")) + process := exec.Command(editor, path.Join(appDir, "config.toml")) process.Stdin = os.Stdin process.Stdout = os.Stdout process.Stderr = os.Stderr @@ -29,6 +25,8 @@ var ( } }, } + + editors = []string{"nano", "notepad", "vi", "emacs"} ) func findEditor() string { diff --git a/commands/name_pending.go b/commands/name_pending.go index cc4dc2dd..14211285 100644 --- a/commands/name_pending.go +++ b/commands/name_pending.go @@ -1,9 +1,16 @@ package commands import ( + "io/ioutil" + "log" + "os" + "os/user" + "path" + "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/viper" + "github.com/khlieng/name_pending/assets" "github.com/khlieng/name_pending/server" "github.com/khlieng/name_pending/storage" ) @@ -13,10 +20,25 @@ var ( Use: "name_pending", Short: "Web-based IRC client in Go.", Run: func(cmd *cobra.Command, args []string) { - storage.Initialize() + storage.Initialize(appDir) server.Run(viper.GetInt("port")) }, + + PersistentPreRun: func(cmd *cobra.Command, args []string) { + appDir = viper.GetString("dir") + + os.Mkdir(appDir, 0777) + os.Mkdir(path.Join(appDir, "logs"), 0777) + + initConfig() + + viper.SetConfigName("config") + viper.AddConfigPath(appDir) + viper.ReadInConfig() + }, } + + appDir string ) func init() { @@ -24,14 +46,38 @@ func init() { rootCmd.AddCommand(configCmd) rootCmd.Flags().IntP("port", "p", 1337, "port to listen on") - - viper.SetConfigName("config") - viper.AddConfigPath(storage.AppDir) - viper.ReadInConfig() + rootCmd.PersistentFlags().String("dir", defaultDir(), "directory to store config and data in") viper.BindPFlag("port", rootCmd.Flags().Lookup("port")) + viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir")) } func Execute() { rootCmd.Execute() } + +func initConfig() { + configPath := path.Join(appDir, "config.toml") + + if _, err := os.Stat(configPath); os.IsNotExist(err) { + config, err := assets.Asset("config.default.toml") + if err != nil { + log.Println(err) + return + } + + err = ioutil.WriteFile(configPath, config, 0600) + if err != nil { + log.Println(err) + } + } +} + +func defaultDir() string { + currentUser, err := user.Current() + if err != nil { + log.Fatal(err) + } + + return path.Join(currentUser.HomeDir, ".name_pending") +} diff --git a/main.go b/main.go index 27926c4c..6f85b06a 100644 --- a/main.go +++ b/main.go @@ -1,34 +1,9 @@ package main import ( - "io/ioutil" - "log" - "os" - "path" - - "github.com/khlieng/name_pending/assets" "github.com/khlieng/name_pending/commands" - "github.com/khlieng/name_pending/storage" ) func main() { - initConfig() commands.Execute() } - -func initConfig() { - configPath := path.Join(storage.AppDir, "config.toml") - - if _, err := os.Stat(configPath); os.IsNotExist(err) { - config, err := assets.Asset("config.default.toml") - if err != nil { - log.Println(err) - return - } - - err = ioutil.WriteFile(configPath, config, 0600) - if err != nil { - log.Println(err) - } - } -} diff --git a/storage/storage.go b/storage/storage.go index 8dc5e6d5..15364007 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -3,16 +3,14 @@ package storage import ( "log" "os" - "os/user" "path" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/boltdb/bolt" ) var ( - AppDir string - - db *bolt.DB + appDir string + db *bolt.DB bucketUsers = []byte("Users") bucketServers = []byte("Servers") @@ -20,24 +18,13 @@ var ( bucketMessages = []byte("Messages") ) -func init() { - currentUser, err := user.Current() - if err != nil { - log.Fatal(err) - } - - AppDir = path.Join(currentUser.HomeDir, ".name_pending") - - os.Mkdir(AppDir, 0777) - os.Mkdir(path.Join(AppDir, "logs"), 0777) -} - -func Initialize() { +func Initialize(dir string) { var err error + appDir = dir - log.Println("Storing data at", AppDir) + log.Println("Storing data at", dir) - db, err = bolt.Open(path.Join(AppDir, "data.db"), 0600, nil) + db, err = bolt.Open(path.Join(dir, "data.db"), 0600, nil) if err != nil { log.Fatal("Could not open database file") } @@ -55,7 +42,7 @@ func Close() { db.Close() } -func Clear() { - os.RemoveAll(path.Join(AppDir, "logs")) - os.Remove(path.Join(AppDir, "data.db")) +func Clear(dir string) { + os.RemoveAll(path.Join(dir, "logs")) + os.Remove(path.Join(dir, "data.db")) } diff --git a/storage/user.go b/storage/user.go index 2ac7fe96..1afb66f4 100644 --- a/storage/user.go +++ b/storage/user.go @@ -304,7 +304,7 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error) func (u *User) openMessageLog() { var err error - u.messageLog, err = bolt.Open(path.Join(AppDir, "logs", u.UUID+"_log"), 0600, nil) + u.messageLog, err = bolt.Open(path.Join(appDir, "logs", u.UUID+"_log"), 0600, nil) if err != nil { log.Fatal(err) } @@ -315,7 +315,7 @@ func (u *User) openMessageLog() { return nil }) - indexPath := path.Join(AppDir, "logs", u.UUID+"_index") + indexPath := path.Join(appDir, "logs", u.UUID+"_index") u.messageIndex, err = bleve.Open(indexPath) if err == bleve.ErrorIndexPathDoesNotExist { mapping := bleve.NewIndexMapping()