mirror of
https://github.com/tuna/tunasync.git
synced 2025-04-20 11:42:43 +00:00
fix(cmd): change deprecated signature to cli.ActionFunc
load configurations from all config files in correct order
This commit is contained in:
parent
28c8145137
commit
4c5e18cb92
@ -20,8 +20,8 @@ const (
|
||||
listWorkersPath = "/workers"
|
||||
cmdPath = "/cmd"
|
||||
|
||||
systemCfgFile = "/etc/tunasync/ctl.conf"
|
||||
userCfgFile = "$HOME/.config/tunasync/ctl.conf"
|
||||
systemCfgFile = "/etc/tunasync/ctl.conf" // system-wide conf
|
||||
userCfgFile = "$HOME/.config/tunasync/ctl.conf" // user-specific conf
|
||||
)
|
||||
|
||||
var logger = logging.MustGetLogger("tunasynctl-cmd")
|
||||
@ -29,13 +29,13 @@ var logger = logging.MustGetLogger("tunasynctl-cmd")
|
||||
var baseURL string
|
||||
var client *http.Client
|
||||
|
||||
func initializeWrapper(handler func(*cli.Context)) func(*cli.Context) {
|
||||
return func(c *cli.Context) {
|
||||
func initializeWrapper(handler cli.ActionFunc) cli.ActionFunc {
|
||||
return func(c *cli.Context) error {
|
||||
err := initialize(c)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
return cli.NewExitError("", 1)
|
||||
}
|
||||
handler(c)
|
||||
return handler(c)
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,18 +45,40 @@ type config struct {
|
||||
CACert string `toml:"ca_cert"`
|
||||
}
|
||||
|
||||
func loadConfig(cfgFile string, c *cli.Context) (*config, error) {
|
||||
cfg := new(config)
|
||||
cfg.ManagerAddr = "localhost"
|
||||
cfg.ManagerPort = 14242
|
||||
|
||||
func loadConfig(cfgFile string, cfg *config) error {
|
||||
if cfgFile != "" {
|
||||
if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
|
||||
logger.Errorf(err.Error())
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initialize(c *cli.Context) error {
|
||||
// init logger
|
||||
tunasync.InitLogger(c.Bool("verbose"), c.Bool("verbose"), false)
|
||||
|
||||
cfg := new(config)
|
||||
|
||||
// default configs
|
||||
cfg.ManagerAddr = "localhost"
|
||||
cfg.ManagerPort = 14242
|
||||
|
||||
// find config file and load config
|
||||
if _, err := os.Stat(systemCfgFile); err == nil {
|
||||
loadConfig(systemCfgFile, cfg)
|
||||
}
|
||||
fmt.Println(os.ExpandEnv(userCfgFile))
|
||||
if _, err := os.Stat(os.ExpandEnv(userCfgFile)); err == nil {
|
||||
loadConfig(os.ExpandEnv(userCfgFile), cfg)
|
||||
}
|
||||
if c.String("config") != "" {
|
||||
loadConfig(c.String("config"), cfg)
|
||||
}
|
||||
|
||||
// override config using the command-line arguments
|
||||
if c.String("manager") != "" {
|
||||
cfg.ManagerAddr = c.String("manager")
|
||||
}
|
||||
@ -67,28 +89,6 @@ func loadConfig(cfgFile string, c *cli.Context) (*config, error) {
|
||||
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
|
||||
|
||||
// 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
|
||||
}
|
||||
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",
|
||||
@ -97,6 +97,7 @@ func initialize(c *cli.Context) error {
|
||||
logger.Infof("Use manager address: %s", baseURL)
|
||||
|
||||
// create HTTP client
|
||||
var err error
|
||||
client, err = tunasync.CreateHTTPClient(cfg.CACert)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error initializing HTTP client: %s", err.Error())
|
||||
@ -107,44 +108,54 @@ func initialize(c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func listWorkers(c *cli.Context) {
|
||||
func listWorkers(c *cli.Context) error {
|
||||
var workers []tunasync.WorkerStatus
|
||||
_, err := tunasync.GetJSON(baseURL+listWorkersPath, &workers, client)
|
||||
if err != nil {
|
||||
logger.Errorf("Filed to correctly get informations from manager server: %s", err.Error())
|
||||
os.Exit(1)
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Filed to correctly get informations from"+
|
||||
"manager server: %s", err.Error()), 1)
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(workers, "", " ")
|
||||
if err != nil {
|
||||
logger.Errorf("Error printing out informations: %s", err.Error())
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Error printing out informations: %s",
|
||||
err.Error()),
|
||||
1)
|
||||
}
|
||||
fmt.Print(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
func listJobs(c *cli.Context) {
|
||||
func listJobs(c *cli.Context) error {
|
||||
// FIXME: there should be an API on manager server side that return MirrorStatus list to tunasynctl
|
||||
var jobs []tunasync.MirrorStatus
|
||||
if c.Bool("all") {
|
||||
_, err := tunasync.GetJSON(baseURL+listJobsPath, &jobs, client)
|
||||
if err != nil {
|
||||
logger.Errorf("Filed to correctly get information of all jobs from manager server: %s", err.Error())
|
||||
os.Exit(1)
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Failed to correctly get information "+
|
||||
"of all jobs from manager server: %s", err.Error()),
|
||||
1)
|
||||
}
|
||||
|
||||
} else {
|
||||
args := c.Args()
|
||||
if len(args) == 0 {
|
||||
logger.Error("Usage Error: jobs command need at least one arguments or \"--all\" flag.")
|
||||
os.Exit(1)
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Usage Error: jobs command need at"+
|
||||
" least one arguments or \"--all\" flag."), 1)
|
||||
}
|
||||
ans := make(chan []tunasync.MirrorStatus, len(args))
|
||||
for _, workerID := range args {
|
||||
go func(workerID string) {
|
||||
var workerJobs []tunasync.MirrorStatus
|
||||
_, err := tunasync.GetJSON(fmt.Sprintf("%s/workers/%s/jobs", baseURL, workerID), &workerJobs, client)
|
||||
_, err := tunasync.GetJSON(fmt.Sprintf("%s/workers/%s/jobs",
|
||||
baseURL, workerID), &workerJobs, client)
|
||||
if err != nil {
|
||||
logger.Errorf("Filed to correctly get jobs for worker %s: %s", workerID, err.Error())
|
||||
logger.Errorf("Filed to correctly get jobs"+
|
||||
" for worker %s: %s", workerID, err.Error())
|
||||
}
|
||||
ans <- workerJobs
|
||||
}(workerID)
|
||||
@ -156,13 +167,16 @@ func listJobs(c *cli.Context) {
|
||||
|
||||
b, err := json.MarshalIndent(jobs, "", " ")
|
||||
if err != nil {
|
||||
logger.Errorf("Error printing out informations: %s", err.Error())
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Error printing out informations: %s", err.Error()),
|
||||
1)
|
||||
}
|
||||
fmt.Printf(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdJob(cmd tunasync.CmdVerb) func(*cli.Context) {
|
||||
return func(c *cli.Context) {
|
||||
func cmdJob(cmd tunasync.CmdVerb) cli.ActionFunc {
|
||||
return func(c *cli.Context) error {
|
||||
var mirrorID string
|
||||
var argsList []string
|
||||
if len(c.Args()) == 1 {
|
||||
@ -173,8 +187,9 @@ func cmdJob(cmd tunasync.CmdVerb) func(*cli.Context) {
|
||||
argsList = append(argsList, strings.TrimSpace(arg))
|
||||
}
|
||||
} else {
|
||||
logger.Error("Usage Error: cmd command receive just 1 required positional argument MIRROR and 1 optional ")
|
||||
os.Exit(1)
|
||||
return cli.NewExitError("Usage Error: cmd command receive just "+
|
||||
"1 required positional argument MIRROR and 1 optional "+
|
||||
"argument WORKER", 1)
|
||||
}
|
||||
|
||||
cmd := tunasync.ClientCmd{
|
||||
@ -185,21 +200,28 @@ func cmdJob(cmd tunasync.CmdVerb) func(*cli.Context) {
|
||||
}
|
||||
resp, err := tunasync.PostJSON(baseURL+cmdPath, cmd, client)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to correctly send command: %s", err.Error())
|
||||
os.Exit(1)
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Failed to correctly send command: %s",
|
||||
err.Error()),
|
||||
1)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to parse response: %s", err.Error())
|
||||
return cli.NewExitError(
|
||||
fmt.Sprintf("Failed to parse response: %s", err.Error()),
|
||||
1)
|
||||
}
|
||||
|
||||
logger.Errorf("Failed to correctly send command: HTTP status code is not 200: %s", body)
|
||||
} else {
|
||||
logger.Info("Succesfully send command")
|
||||
return cli.NewExitError(fmt.Sprintf("Failed to correctly send"+
|
||||
" command: HTTP status code is not 200: %s", body),
|
||||
1)
|
||||
}
|
||||
logger.Info("Succesfully send command")
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user