2018-02-02 22:48:57 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-05-15 02:55:45 +02:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2018-02-02 22:48:57 +01:00
|
|
|
"os"
|
|
|
|
|
2018-05-29 23:19:40 +02:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-05-15 02:55:45 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-02 22:48:57 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Config is the base configuration for Prism when no sub commands are used.
|
2018-02-02 22:48:57 +01:00
|
|
|
type Config struct {
|
|
|
|
AwsID string `json:"aws_id"`
|
|
|
|
AwsSecret string `json:"aws_secret"`
|
|
|
|
BucketRegion string `json:"bucket_region"`
|
|
|
|
BucketName string `json:"bucket_name"`
|
|
|
|
DBConn string `json:"db_conn"`
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
var verbose bool
|
|
|
|
var conf string
|
|
|
|
var globalConfig Config
|
2018-02-02 22:48:57 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
var rootCmd = &cobra.Command{
|
2018-02-02 22:48:57 +01:00
|
|
|
Use: "reflector",
|
|
|
|
Short: "Reflector accepts blobs, stores them securely, and hosts them on the network",
|
2018-05-15 02:55:45 +02:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2018-05-30 03:38:55 +02:00
|
|
|
if verbose {
|
2018-05-15 02:55:45 +02:00
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2018-05-30 03:38:55 +02:00
|
|
|
if conf == "" {
|
2018-05-15 02:55:45 +02:00
|
|
|
log.Errorln("--conf flag required")
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
2018-05-30 03:38:55 +02:00
|
|
|
globalConfig, err = loadConfig(conf)
|
2018-05-15 02:55:45 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-02 22:48:57 +01:00
|
|
|
// Uncomment the following line if your bare application
|
|
|
|
// has an action associated with it:
|
|
|
|
// Run: func(cmd *cobra.Command, args []string) { },
|
|
|
|
}
|
|
|
|
|
2018-05-15 02:55:45 +02:00
|
|
|
func init() {
|
2018-05-30 03:38:55 +02:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
|
|
|
|
rootCmd.PersistentFlags().StringVar(&conf, "conf", "config.json", "Path to config")
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 22:48:57 +01:00
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
2018-05-30 03:38:55 +02:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2018-05-15 02:55:45 +02:00
|
|
|
log.Errorln(err)
|
2018-02-02 22:48:57 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 19:33:52 +01:00
|
|
|
|
|
|
|
func argFuncs(funcs ...cobra.PositionalArgs) cobra.PositionalArgs {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
for _, f := range funcs {
|
|
|
|
err := f(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 02:55:45 +02:00
|
|
|
|
|
|
|
func loadConfig(path string) (Config, error) {
|
|
|
|
var c Config
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return c, errors.Err("config file not found")
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw, &c)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|