Add dockerfile, add data directory flag
This commit is contained in:
parent
1f5dd7a5e0
commit
284a35ab09
9 changed files with 81 additions and 62 deletions
|
@ -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)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue