2015-05-01 20:59:46 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2016-01-04 18:26:32 +00:00
|
|
|
"fmt"
|
2015-05-28 23:59:08 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/cobra"
|
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/viper"
|
2015-05-01 20:59:46 +00:00
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/assets"
|
|
|
|
"github.com/khlieng/dispatch/server"
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
2015-05-01 20:59:46 +00:00
|
|
|
)
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
const logo = `
|
|
|
|
____ _ _ _
|
|
|
|
| _ \ (_) ___ _ __ __ _ | |_ ___ | |__
|
|
|
|
| | | || |/ __|| '_ \ / _ || __|/ __|| '_ \
|
|
|
|
| |_| || |\__ \| |_) || (_| || |_| (__ | | | |
|
|
|
|
|____/ |_||___/| .__/ \__,_| \__|\___||_| |_|
|
|
|
|
|_|
|
|
|
|
v0.1
|
|
|
|
`
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "dispatch",
|
|
|
|
Short: "Web-based IRC client in Go.",
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
if cmd.Use == "dispatch" {
|
|
|
|
fmt.Println(logo)
|
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2016-01-07 22:59:38 +00:00
|
|
|
storage.Initialize(viper.GetString("dir"))
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2016-01-07 22:59:38 +00:00
|
|
|
initConfig(storage.Path.Config())
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.AddConfigPath(storage.Path.Root())
|
|
|
|
viper.ReadInConfig()
|
|
|
|
},
|
2016-01-07 22:59:38 +00:00
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
log.Println("Storing data at", storage.Path.Root())
|
|
|
|
|
|
|
|
storage.Open()
|
|
|
|
server.Run()
|
|
|
|
},
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
func Execute() {
|
|
|
|
rootCmd.Execute()
|
|
|
|
}
|
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)
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
rootCmd.Flags().IntP("port", "p", 80, "port to listen on")
|
|
|
|
rootCmd.PersistentFlags().String("dir", storage.DefaultDirectory(), "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"))
|
2016-01-14 05:35:26 +00:00
|
|
|
|
|
|
|
viper.SetDefault("verify_client_certificates", true)
|
2015-05-01 20:59:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 22:59:38 +00:00
|
|
|
func initConfig(configPath string) {
|
2015-05-28 23:59:08 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
config, err := assets.Asset("config.default.toml")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
log.Println("Writing default config to", configPath)
|
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
err = ioutil.WriteFile(configPath, config, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|