Files
crawlab/backend/cmd/root.go
2021-12-24 15:07:04 +08:00

72 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"github.com/apex/log"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// Used for flags.
cfgFile string
rootCmd = &cobra.Command{
Use: "crawlab",
Short: "CLI tool for Crawlab",
Long: `The CLI tool is for controlling against Crawlab.
Crawlab is a distributed web crawler and task admin platform
aimed at making web crawling and task management easier.
`,
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func init() {
cobra.OnInitialize(initConfig)
}
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("./conf")
viper.SetConfigName("config")
}
// file format as yaml
viper.SetConfigType("yaml")
// auto load env
viper.AutomaticEnv()
// env prefix as CRAWLAB
viper.SetEnvPrefix("CRAWLAB")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// read config file
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
// initialize log level
initLogLevel()
}
func initLogLevel() {
// set log level
logLevel := viper.GetString("log.level")
l, err := log.ParseLevel(logLevel)
if err != nil {
l = log.InfoLevel
}
log.SetLevel(l)
}