2015-05-01 20:59:46 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2015-05-28 23:59:08 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
|
2015-05-01 20:59:46 +00:00
|
|
|
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"
|
2015-05-25 02:00:21 +00:00
|
|
|
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/viper"
|
2015-05-01 20:59:46 +00:00
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
"github.com/khlieng/name_pending/assets"
|
2015-05-01 20:59:46 +00:00
|
|
|
"github.com/khlieng/name_pending/server"
|
|
|
|
"github.com/khlieng/name_pending/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
2015-05-01 23:02:21 +00:00
|
|
|
Use: "name_pending",
|
|
|
|
Short: "Web-based IRC client in Go.",
|
2015-05-01 20:59:46 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2015-05-28 23:59:08 +00:00
|
|
|
storage.Initialize(appDir)
|
2015-05-25 02:00:21 +00:00
|
|
|
server.Run(viper.GetInt("port"))
|
2015-05-01 20:59:46 +00:00
|
|
|
},
|
2015-05-28 23:59:08 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
},
|
2015-05-01 20:59:46 +00:00
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
|
|
|
appDir string
|
2015-05-01 20:59:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-05-01 23:02:21 +00:00
|
|
|
rootCmd.AddCommand(clearCmd)
|
2015-05-25 02:00:21 +00:00
|
|
|
rootCmd.AddCommand(configCmd)
|
|
|
|
|
|
|
|
rootCmd.Flags().IntP("port", "p", 1337, "port to listen on")
|
2015-05-28 23:59:08 +00:00
|
|
|
rootCmd.PersistentFlags().String("dir", defaultDir(), "directory to store config and data in")
|
2015-05-01 23:02:21 +00:00
|
|
|
|
2015-05-25 02:00:21 +00:00
|
|
|
viper.BindPFlag("port", rootCmd.Flags().Lookup("port"))
|
2015-05-28 23:59:08 +00:00
|
|
|
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
|
2015-05-01 20:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
rootCmd.Execute()
|
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|