本文整理汇总了Golang中github.com/spf13/cobra.Command.Instances方法的典型用法代码示例。如果您正苦于以下问题:Golang Command.Instances方法的具体用法?Golang Command.Instances怎么用?Golang Command.Instances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/spf13/cobra.Command
的用法示例。
在下文中一共展示了Command.Instances方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: deployContainers
func (ctx *CommandContext) deployContainers(c *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Fail(1, "Valid arguments: <deployment_file|URL> <host> ...")
}
t := ctx.Transport.Get()
path := args[0]
if path == "" {
cmd.Fail(1, "Argument 1 must be deployment file or URL describing how the containers are related")
}
u, err := url.Parse(path)
if nil != err {
cmd.Fail(1, "Cannot Parse Argument 1: %s", err.Error())
}
var deploy *deployment.Deployment
switch u.Scheme {
case "":
deploy, err = deployment.NewDeploymentFromFile(u.Path)
case "file":
deploy, err = deployment.NewDeploymentFromFile(u.Path)
case "http", "https":
deploy, err = deployment.NewDeploymentFromURL(u.String(), *ctx.Insecure, time.Duration(ctx.timeout))
default:
cmd.Fail(1, "Unsupported URL Scheme '%s' for deployment", u.Scheme)
}
if nil != err {
cmd.Fail(1, "Unable to load deployment from %s: %s", path, err.Error())
}
if len(args) == 1 {
args = append(args, transport.Local.String())
}
servers, err := transport.NewTransportLocators(t, args[1:]...)
if err != nil {
cmd.Fail(1, "You must pass zero or more valid host names (use '%s' or pass no arguments for the current server): %s", transport.Local.String(), err.Error())
}
re := regexp.MustCompile("\\.\\d{8}\\-\\d{6}\\z")
now := time.Now().Format(".20060102-150405")
base := filepath.Base(path)
base = re.ReplaceAllString(base, "")
newPath := base + now
fmt.Printf("==> Deploying %s\n", path)
changes, removed, err := deploy.Describe(deployment.SimplePlacement(servers), t)
if err != nil {
cmd.Fail(1, "Deployment is not valid: %s", err.Error())
}
if len(removed) > 0 {
removedIds, err := LocatorsForDeploymentInstances(t, removed)
if err != nil {
cmd.Fail(1, "Unable to generate deployment info: %s", err.Error())
}
failures := cmd.Executor{
On: removedIds,
Serial: func(on cmd.Locator) cmd.JobRequest {
return &cjobs.DeleteContainerRequest{
Id: cloc.AsIdentifier(on),
}
},
Output: os.Stdout,
OnSuccess: func(r *cmd.CliJobResponse, w io.Writer, job cmd.RequestedJob) {
fmt.Fprintf(w, "==> Deleted %s", string(job.Request.(*cjobs.DeleteContainerRequest).Id))
},
Transport: t,
}.Stream()
for i := range failures {
fmt.Fprintf(os.Stderr, failures[i].Error())
}
}
addedIds, err := LocatorsForDeploymentInstances(t, changes.Instances.Added())
if err != nil {
cmd.Fail(1, "Unable to generate deployment info: %s", err.Error())
}
errors := cmd.Executor{
On: addedIds,
Serial: func(on cmd.Locator) cmd.JobRequest {
instance, _ := changes.Instances.Find(cloc.AsIdentifier(on))
links := instance.NetworkLinks()
return &cjobs.InstallContainerRequest{
RequestIdentifier: jobs.NewRequestIdentifier(),
Id: instance.Id,
Image: instance.Image,
Environment: instance.EnvironmentVariables(),
Isolate: ctx.isolate,
Ports: instance.Ports.PortPairs(),
NetworkLinks: &links,
}
},
//.........这里部分代码省略.........