当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.Close方法代码示例

本文整理汇总了Golang中github.com/codegangsta/cli.Context.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Close方法的具体用法?Golang Context.Close怎么用?Golang Context.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/codegangsta/cli.Context的用法示例。


在下文中一共展示了Context.Close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: run

func (rax *RaxCloud) run(ctx *cli.Context, eng *engine.Engine) (err error) {
	if err = rax.configure(ctx); err == nil {
		if _, name, err := rax.findTargetHost(); err == nil {
			if name == "" {
				rax.createHost(DASS_TARGET_PREFIX + RandomString()[:12])
			}
		} else {
			return err
		}

		eng.Register("create", func(job *engine.Job) (status engine.Status) {
			if ctx, err := rax.getHostContext(); err == nil {
				defer ctx.Close()

				return ctx.exec(job, func(client HttpClient) (status engine.Status) {
					path := "/containers/create"
					config := runconfig.ContainerConfigFromJob(job)

					if data, err := json.Marshal(config); err != nil {
						return job.Errorf("marshaling failure : %v", err)
					} else if resp, err := client.Post(path, string(data)); err != nil {
						return job.Error(err)
					} else {
						var container struct {
							Id       string
							Warnings []string
						}

						if body, err := ioutil.ReadAll(resp.Body); err != nil {
							return job.Errorf("Failed to copy response body: %v", err)
						} else if err := json.Unmarshal([]byte(body), &container); err != nil {
							return job.Errorf("Failed to read container info from body: %v", err)
						} else {
							job.Printf("%s\n", container.Id)
						}
					}

					return engine.StatusOK
				})
			} else {
				return job.Errorf("Failed to create host context: %v", err)
			}
		})

		eng.Register("container_delete", func(job *engine.Job) (status engine.Status) {
			if ctx, err := rax.getHostContext(); err == nil {
				defer ctx.Close()

				return ctx.exec(job, func(client HttpClient) (status engine.Status) {
					path := fmt.Sprintf("/containers/%s?force=%s", job.Args[0], url.QueryEscape(job.Getenv("forceRemove")))

					if _, err := client.Delete(path, ""); err != nil {
						return job.Error(err)
					}

					return engine.StatusOK
				})
			} else {
				return job.Errorf("Failed to create host context: %v", err)
			}
		})

		eng.Register("containers", func(job *engine.Job) (status engine.Status) {
			if ctx, err := rax.getHostContext(); err == nil {
				defer ctx.Close()

				return ctx.exec(job, func(client HttpClient) (status engine.Status) {
					path := fmt.Sprintf("/containers/json?all=%s&limit=%s",
						url.QueryEscape(job.Getenv("all")), url.QueryEscape(job.Getenv("limit")))

					if resp, err := client.Get(path, ""); err != nil {
						return job.Error(err)
					} else {
						created := engine.NewTable("Created", 0)

						if body, err := ioutil.ReadAll(resp.Body); err != nil {
							return job.Errorf("Failed to copy response body: %v", err)
						} else if created.ReadListFrom(body); err != nil {
							return job.Errorf("Failed to read list from body: %v", err)
						} else {
							created.WriteListTo(job.Stdout)
						}
					}

					return engine.StatusOK
				})
			} else {
				return job.Errorf("Failed to create host context: %v", err)
			}
		})

		eng.Register("version", func(job *engine.Job) (status engine.Status) {
			if ctx, err := rax.getHostContext(); err == nil {
				defer ctx.Close()

				return ctx.exec(job, func(client HttpClient) (status engine.Status) {
					path := "/version"

					if resp, err := client.Get(path, ""); err != nil {
						return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err)
//.........这里部分代码省略.........
开发者ID:BillTheBest,项目名称:libswarm,代码行数:101,代码来源:rax.go


注:本文中的github.com/codegangsta/cli.Context.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。