Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
94 lines (80 sloc) 2.08 KB
package main // import "pathwar.pw"
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"pathwar.pw/hypervisor"
"pathwar.pw/pkg/cli"
"pathwar.pw/server"
"pathwar.pw/sql"
)
func main() {
rand.Seed(time.Now().UnixNano())
rootCmd := newRootCommand()
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func newRootCommand() *cobra.Command {
cmd := &cobra.Command{
// Use: "pathwar.pw",
Use: os.Args[0],
}
cmd.PersistentFlags().BoolP("help", "h", false, "print usage")
//cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
// Add commands
commands := cli.Commands{}
for name, command := range sql.Commands() {
commands[name] = command
}
for name, command := range server.Commands() {
commands[name] = command
}
for name, command := range hypervisor.Commands() {
commands[name] = command
}
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// setup logging
config := zap.NewDevelopmentConfig()
config.Level.SetLevel(zap.DebugLevel)
config.DisableStacktrace = true
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
l, err := config.Build()
if err != nil {
return errors.Wrap(err, "failed to configure logger")
}
zap.ReplaceGlobals(l)
zap.L().Debug("logger initialized")
// setup viper
viper.AddConfigPath(".")
viper.SetConfigName(".pathwar")
if err := viper.MergeInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return errors.Wrap(err, "failed to apply viper config")
}
}
for _, command := range commands {
if err := command.LoadDefaultOptions(); err != nil {
return err
}
}
return nil
}
for name, command := range commands {
if strings.Contains(name, " ") { // do not add commands where level > 1
continue
}
cmd.AddCommand(command.CobraCommand(commands))
}
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
return cmd
}