Skip to content

Commit

Permalink
feature(cmd): add default config files for tunasynctl
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerning authored and bigeagle committed Apr 30, 2016
1 parent 56459f2 commit 2ea22ec
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
8 changes: 4 additions & 4 deletions cmd/tunasync/tunasync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func startManager(c *cli.Context) {

cfg, err := manager.LoadConfig(c.String("config"), c)
if err != nil {
logger.Error("Error loading config: %s", err.Error())
logger.Errorf("Error loading config: %s", err.Error())
os.Exit(1)
}
if !cfg.Debug {
Expand All @@ -28,7 +28,7 @@ func startManager(c *cli.Context) {

m := manager.GetTUNASyncManager(cfg)
if m == nil {
logger.Error("Error intializing TUNA sync worker.")
logger.Errorf("Error intializing TUNA sync worker.")
os.Exit(1)
}

Expand All @@ -44,13 +44,13 @@ func startWorker(c *cli.Context) {

cfg, err := worker.LoadConfig(c.String("config"))
if err != nil {
logger.Error("Error loading config: %s", err.Error())
logger.Errorf("Error loading config: %s", err.Error())
os.Exit(1)
}

w := worker.GetTUNASyncWorker(cfg)
if w == nil {
logger.Error("Error intializing TUNA sync worker.")
logger.Errorf("Error intializing TUNA sync worker.")
os.Exit(1)
}

Expand Down
81 changes: 61 additions & 20 deletions cmd/tunasynctl/tunasynctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"

"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
"gopkg.in/op/go-logging.v1"

Expand All @@ -18,6 +19,9 @@ const (
listJobsPath = "/jobs"
listWorkersPath = "/workers"
cmdPath = "/cmd"

systemCfgFile = "/etc/tunasync/ctl.conf"
userCfgFile = "$HOME/.config/tunasync/ctl.conf"
)

var logger = logging.MustGetLogger("tunasynctl-cmd")
Expand All @@ -35,29 +39,65 @@ func initializeWrapper(handler func(*cli.Context)) func(*cli.Context) {
}
}

type config struct {
ManagerAddr string `toml:"manager_addr"`
ManagerPort int `toml:"manager_port"`
CACert string `toml:"ca_cert"`
}

func loadConfig(cfgFile string, c *cli.Context) (*config, error) {
cfg := new(config)
cfg.ManagerAddr = "localhost"
cfg.ManagerPort = 14242

if cfgFile != "" {
if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
logger.Errorf(err.Error())
return nil, err
}
}

if c.String("manager") != "" {
cfg.ManagerAddr = c.String("manager")
}
if c.Int("port") > 0 {
cfg.ManagerPort = c.Int("port")
}

if c.String("ca-cert") != "" {
cfg.CACert = c.String("ca-cert")
}
return cfg, nil
}

func initialize(c *cli.Context) error {
// init logger
tunasync.InitLogger(c.Bool("verbose"), c.Bool("verbose"), false)
var cfgFile string

// parse manager server address
baseURL = c.String("manager")
if baseURL == "" {
baseURL = "localhost"
}
managerPort := c.String("port")
if managerPort != "" {
baseURL += ":" + managerPort
// choose config file and load config
if c.String("config") != "" {
cfgFile = c.String("config")
} else if _, err := os.Stat(os.ExpandEnv(userCfgFile)); err == nil {
cfgFile = os.ExpandEnv(userCfgFile)
} else if _, err := os.Stat(systemCfgFile); err == nil {
cfgFile = systemCfgFile
}
if c.Bool("no-ssl") {
baseURL = "http://" + baseURL
} else {
baseURL = "https://" + baseURL
cfg, err := loadConfig(cfgFile, c)

if err != nil {
logger.Errorf("Load configuration for tunasynctl error: %s", err.Error())
return err
}

// parse base url of the manager server
baseURL = fmt.Sprintf("https://%s:%d",
cfg.ManagerAddr, cfg.ManagerPort)

logger.Infof("Use manager address: %s", baseURL)

// create HTTP client
var err error
client, err = tunasync.CreateHTTPClient(c.String("ca-cert"))
client, err = tunasync.CreateHTTPClient(cfg.CACert)
if err != nil {
err = fmt.Errorf("Error initializing HTTP client: %s", err.Error())
logger.Error(err.Error())
Expand Down Expand Up @@ -169,6 +209,11 @@ func main() {
app.Version = "0.1"

commonFlags := []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Read configuration from `FILE` rather than" +
" ~/.config/tunasync/ctl.conf and /etc/tunasync/ctl.conf",
},
cli.StringFlag{
Name: "manager, m",
Usage: "The manager server address",
Expand All @@ -178,14 +223,10 @@ func main() {
Usage: "The manager server port",
},
cli.StringFlag{
Name: "ca-cert, c",
Usage: "Trust CA cert `CERT`",
Name: "ca-cert",
Usage: "Trust root CA cert file `CERT`",
},

cli.BoolFlag{
Name: "no-ssl",
Usage: "Use http rather than https",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Enable verbosely logging",
Expand Down

0 comments on commit 2ea22ec

Please sign in to comment.