bashhub-server/cmd/root.go

55 lines
924 B
Go
Raw Normal View History

2020-02-07 16:02:18 +08:00
package cmd
import (
"fmt"
2020-02-08 00:14:22 +08:00
"log"
2020-02-07 16:02:18 +08:00
"os"
2020-02-08 00:14:22 +08:00
"path/filepath"
2020-02-07 16:02:18 +08:00
2020-02-08 00:14:22 +08:00
"github.com/nicksherron/bashhub-server/internal"
2020-02-07 16:02:18 +08:00
"github.com/spf13/cobra"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
2020-02-08 00:14:22 +08:00
Run: func(cmd *cobra.Command, args []string) {
cmd.Flags().Parse(args)
internal.Run()
},
2020-02-07 16:02:18 +08:00
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize()
2020-02-08 00:14:22 +08:00
rootCmd.PersistentFlags().StringVar(&internal.DbPath, "db", dbPath(), "DB location (sqlite or postgres)")
2020-02-07 16:02:18 +08:00
2020-02-08 00:14:22 +08:00
}
func dbPath() string {
dbFile := "data.db"
f := filepath.Join(appDir(), dbFile)
return f
}
func appDir() string {
cfgDir, err := os.UserConfigDir()
if err != nil {
log.Fatal(err)
}
2020-02-08 07:48:32 +08:00
ch := filepath.Join(cfgDir, "bashhub-server")
2020-02-08 00:14:22 +08:00
err = os.MkdirAll(ch, 0755)
if err != nil {
log.Fatal(err)
}
2020-02-07 16:02:18 +08:00
2020-02-08 00:14:22 +08:00
return ch
2020-02-07 16:02:18 +08:00
}