mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
- climc: 设置 option 没有 subcommand 的情况下进入交互模式, 代码逻辑变清晰
- make fmt
This commit is contained in:
+68
-42
@@ -133,6 +133,40 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func enterInteractiveMode(
|
||||
parser *structarg.ArgumentParser,
|
||||
sessionFactory func() *mcclient.ClientSession,
|
||||
) {
|
||||
promputils.InitEnv(parser, sessionFactory())
|
||||
defer fmt.Println("Bye!")
|
||||
p := prompt.New(
|
||||
promputils.Executor,
|
||||
promputils.Completer,
|
||||
prompt.OptionPrefix("climc> "),
|
||||
prompt.OptionTitle("Climc, a Command Line Interface to Manage Clouds"),
|
||||
prompt.OptionMaxSuggestion(16),
|
||||
)
|
||||
p.Run()
|
||||
}
|
||||
|
||||
func executeSubcommand(
|
||||
subcmd *structarg.SubcommandArgument,
|
||||
subparser *structarg.ArgumentParser,
|
||||
options *BaseOptions,
|
||||
sessionFactory func() *mcclient.ClientSession,
|
||||
) {
|
||||
var e error
|
||||
suboptions := subparser.Options()
|
||||
if options.SUBCOMMAND == "help" {
|
||||
e = subcmd.Invoke(suboptions)
|
||||
} else {
|
||||
e = subcmd.Invoke(sessionFactory(), suboptions)
|
||||
}
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
parser, e := getSubcommandsParser()
|
||||
if e != nil {
|
||||
@@ -143,47 +177,39 @@ func main() {
|
||||
|
||||
if options.Help {
|
||||
fmt.Print(parser.HelpString())
|
||||
} else if options.Version {
|
||||
fmt.Printf("Yunion API client version:\n %s\n", version.GetJsonString())
|
||||
} else if len(os.Args) <= 1 {
|
||||
session, e := newClientSession(options)
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
promputils.InitEnv(parser, session)
|
||||
defer fmt.Println("Bye!")
|
||||
p := prompt.New(
|
||||
promputils.Executor,
|
||||
promputils.Completer,
|
||||
prompt.OptionPrefix("climc> "),
|
||||
prompt.OptionTitle("Climc, a Command Line Interface to Manage Clouds"),
|
||||
prompt.OptionMaxSuggestion(16),
|
||||
)
|
||||
p.Run()
|
||||
} else {
|
||||
subcmd := parser.GetSubcommand()
|
||||
subparser := subcmd.GetSubParser()
|
||||
if e != nil {
|
||||
if subparser != nil {
|
||||
fmt.Print(subparser.Usage())
|
||||
} else {
|
||||
fmt.Print(parser.Usage())
|
||||
}
|
||||
showErrorAndExit(e)
|
||||
} else {
|
||||
session, e := newClientSession(options)
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
suboptions := subparser.Options()
|
||||
if options.SUBCOMMAND == "help" {
|
||||
e = subcmd.Invoke(suboptions)
|
||||
} else {
|
||||
e = subcmd.Invoke(session, suboptions)
|
||||
}
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if options.Version {
|
||||
fmt.Printf("Yunion API client version:\n %s\n", version.GetJsonString())
|
||||
return
|
||||
}
|
||||
|
||||
ensureSessionFactory := func() *mcclient.ClientSession {
|
||||
session, err := newClientSession(options)
|
||||
if err != nil {
|
||||
showErrorAndExit(err)
|
||||
}
|
||||
return session
|
||||
}
|
||||
|
||||
// enter interactive mode when not enough argument and SUBCOMMAND is empty
|
||||
if _, ok := e.(*structarg.NotEnoughArgumentsError); ok && options.SUBCOMMAND == "" {
|
||||
enterInteractiveMode(parser, ensureSessionFactory)
|
||||
return
|
||||
}
|
||||
|
||||
subcmd := parser.GetSubcommand()
|
||||
subparser := subcmd.GetSubParser()
|
||||
if e != nil {
|
||||
if subparser != nil {
|
||||
fmt.Print(subparser.Usage())
|
||||
} else {
|
||||
fmt.Print(parser.Usage())
|
||||
}
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
|
||||
// execute subcommand in non-interactive mode
|
||||
executeSubcommand(subcmd, subparser, options, ensureSessionFactory)
|
||||
}
|
||||
|
||||
@@ -140,12 +140,12 @@ func init() {
|
||||
})
|
||||
|
||||
type CloudproviderSyncOptions struct {
|
||||
ID string `help:"ID or Name of cloud provider"`
|
||||
Force bool `help:"Force sync no matter what"`
|
||||
FullSync bool `help:"Synchronize everything"`
|
||||
Region []string `help:"region to sync"`
|
||||
Zone []string `help:"region to sync"`
|
||||
Host []string `help:"region to sync"`
|
||||
ID string `help:"ID or Name of cloud provider"`
|
||||
Force bool `help:"Force sync no matter what"`
|
||||
FullSync bool `help:"Synchronize everything"`
|
||||
Region []string `help:"region to sync"`
|
||||
Zone []string `help:"region to sync"`
|
||||
Host []string `help:"region to sync"`
|
||||
}
|
||||
R(&CloudproviderSyncOptions{}, "cloud-provider-sync", "Sync of a cloud provider account", func(s *mcclient.ClientSession, args *CloudproviderSyncOptions) error {
|
||||
params := jsonutils.NewDict()
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/gin-gonic/gin.v1"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/util/prometheus"
|
||||
"yunion.io/x/pkg/utils"
|
||||
"gopkg.in/gin-gonic/gin.v1"
|
||||
|
||||
o "yunion.io/x/onecloud/cmd/scheduler/options"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
|
||||
Reference in New Issue
Block a user