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


Golang cli.Context类代码示例

本文整理汇总了Golang中github.com/digitalocean/doctl/Godeps/_workspace/src/github.com/codegangsta/cli.Context的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: sshCreate

func sshCreate(ctx *cli.Context) {
	if len(ctx.Args()) != 2 {
		log.Fatal("Must provide name and public key file.")
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	file, err := os.Open(ctx.Args()[1])
	if err != nil {
		log.Fatalf("Error opening key file: %s.", err)
	}

	keyData, err := ioutil.ReadAll(file)
	if err != nil {
		log.Fatalf("Error reading key file: %s.", err)
	}

	createRequest := &godo.KeyCreateRequest{
		Name:      ctx.Args().First(),
		PublicKey: string(keyData),
	}
	key, _, err := client.Keys.Create(createRequest)
	if err != nil {
		log.Fatal(err)
	}

	WriteOutput(key)
}
开发者ID:phillbaker,项目名称:doctl,代码行数:32,代码来源:sshkey.go

示例2: TestApp_RunAsSubcommandParseFlags

func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
	var context *cli.Context

	a := cli.NewApp()
	a.Commands = []cli.Command{
		{
			Name: "foo",
			Action: func(c *cli.Context) {
				context = c
			},
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "lang",
					Value: "english",
					Usage: "language for the greeting",
				},
			},
			Before: func(_ *cli.Context) error { return nil },
		},
	}
	a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})

	expect(t, context.Args().Get(0), "abcd")
	expect(t, context.String("lang"), "spanish")
}
开发者ID:phillbaker,项目名称:doctl,代码行数:25,代码来源:app_test.go

示例3: sizeList

func sizeList(ctx *cli.Context) {
	if ctx.BoolT("help") == true {
		cli.ShowAppHelp(ctx)
		os.Exit(1)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	opt := &godo.ListOptions{
		Page:    1,
		PerPage: 50, // Not likely to have more than 50 sizes soon
	}
	sizeList, _, err := client.Sizes.List(opt)
	if err != nil {
		fmt.Printf("Unable to list Sizes: %s\n", err)
		os.Exit(1)
	}

	cliOut := NewCLIOutput()
	defer cliOut.Flush()
	cliOut.Header("Slug", "Memory", "VCPUs", "Disk", "Transfer", "Price Monthly", "Price Hourly")
	for _, size := range sizeList {
		cliOut.Writeln("%s\t%dMB\t%d\t%dGB\t%d\t$%.0f\t$%.5f\n",
			size.Slug, size.Memory, size.Vcpus, size.Disk, size.Transfer, size.PriceMonthly, size.PriceHourly)
	}
}
开发者ID:jmptrader,项目名称:doctl,代码行数:30,代码来源:size.go

示例4: regionList

func regionList(ctx *cli.Context) {
	if ctx.BoolT("help") == true {
		cli.ShowAppHelp(ctx)
		os.Exit(1)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	opt := &godo.ListOptions{
		Page:    1,
		PerPage: 50, // Not likely to have more than 50 regions soon
	}
	regionList, _, err := client.Regions.List(opt)
	if err != nil {
		fmt.Printf("Unable to list Regions: %s\n", err)
		os.Exit(1)
	}

	cliOut := NewCLIOutput()
	defer cliOut.Flush()
	cliOut.Header("Name", "Slug", "Available")
	for _, region := range regionList {
		cliOut.Writeln("%s\t%s\t%t\n", region.Name, region.Slug, region.Available)
	}
}
开发者ID:jmptrader,项目名称:doctl,代码行数:29,代码来源:region.go

示例5: dropletActionSnapshot

func dropletActionSnapshot(ctx *cli.Context) {
	if ctx.Int("id") == 0 && len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide ID or name for Droplet to resize.")
	}

	name := ctx.String("name")

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	id := ctx.Int("id")
	if id == 0 {
		droplet, err := FindDropletByName(client, ctx.Args()[0])
		if err != nil {
			log.Fatal(err)
		} else {
			id = droplet.ID
		}
	}

	droplet, _, err := client.Droplets.Get(id)
	if err != nil {
		log.Fatal("Unable to find Droplet: %s.", err)
	}

	action, _, err := client.DropletActions.Snapshot(droplet.ID, name)
	if err != nil {
		log.Fatal(err)
	}

	WriteOutput(action)
}
开发者ID:wojtekzw,项目名称:doctl,代码行数:35,代码来源:droplet.go

示例6: authMethods

func authMethods(ctx *cli.Context) (methods []ssh.AuthMethod, err error) {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Password: ")
	text, _ := reader.ReadString('\n')
	methods = append(methods, ssh.Password(text))

	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}

	keyPath := ctx.String("key")
	if keyPath == "" {
		keyPath = filepath.Join(usr.HomeDir, ".ssh", "id_rsa")
	}

	key, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return
	}

	privateKey, err := ssh.ParsePrivateKey(key)
	if err != nil {
		return
	}
	methods = append(methods, ssh.PublicKeys(privateKey))

	return
}
开发者ID:thebyrd,项目名称:doctl,代码行数:29,代码来源:ssh.go

示例7: dropletList

func dropletList(ctx *cli.Context) {
	if ctx.BoolT("help") == true {
		cli.ShowAppHelp(ctx)
		os.Exit(1)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	opt := &godo.ListOptions{}
	dropletList := []godo.Droplet{}

	for { // TODO make all optional
		dropletPage, resp, err := client.Droplets.List(opt)
		if err != nil {
			fmt.Printf("Unable to list Droplets: %s\n", err)
			os.Exit(1)
		}

		// append the current page's droplets to our list
		for _, d := range dropletPage {
			dropletList = append(dropletList, d)
		}

		// if we are at the last page, break out the for loop
		if resp.Links == nil || resp.Links.IsLastPage() {
			break
		}

		page, err := resp.Links.CurrentPage()
		if err != nil {
			fmt.Printf("Unable to get pagination: %s\n", err)
			os.Exit(1)
		}

		// set the page we want for the next request
		opt.Page = page + 1
	}

	cliOut := NewCLIOutput()
	defer cliOut.Flush()
	cliOut.Header("ID", "Name", "IP Address", "Status", "Memory", "Disk", "Region")
	for _, droplet := range dropletList {
		publicIP := PublicIPForDroplet(&droplet)

		cliOut.Writeln("%d\t%s\t%s\t%s\t%dMB\t%dGB\t%s\n",
			droplet.ID, droplet.Name, publicIP, droplet.Status, droplet.Memory, droplet.Disk, droplet.Region.Slug)
	}
}
开发者ID:jmptrader,项目名称:doctl,代码行数:52,代码来源:droplet.go

示例8: domainRecordList

func domainRecordList(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		fmt.Printf("Error: Must provide a domain name for which to list records.\n")
		os.Exit(64)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	domainName := ctx.Args().First()

	opt := &godo.ListOptions{
		Page:    ctx.Int("page"),
		PerPage: ctx.Int("page-size"),
	}
	domainDecords, _, err := client.Domains.Records(domainName, opt)
	if err != nil {
		fmt.Printf("%s\n", err)
		os.Exit(1)
	}

	WriteOutput(domainDecords)
}
开发者ID:jmptrader,项目名称:doctl,代码行数:26,代码来源:domain.go

示例9: dropletDestroy

func dropletDestroy(ctx *cli.Context) {
	if ctx.Int("id") == 0 && len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide ID or name for Droplet to destroy.")
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	id := ctx.Int("id")
	if id == 0 {
		droplet, err := FindDropletByName(client, ctx.Args()[0])
		if err != nil {
			log.Fatal(err)
		} else {
			id = droplet.ID
		}
	}

	droplet, _, err := client.Droplets.Get(id)
	if err != nil {
		log.Fatalf("Unable to find Droplet: %s.", err)
	}

	_, err = client.Droplets.Delete(id)
	if err != nil {
		log.Fatalf("Unable to destroy Droplet: %s.", err)
	}

	log.Fatalf("Droplet %s destroyed.", droplet.Name)
}
开发者ID:wojtekzw,项目名称:doctl,代码行数:33,代码来源:droplet.go

示例10: sshList

func sshList(ctx *cli.Context) {
	if ctx.BoolT("help") == true {
		cli.ShowAppHelp(ctx)
		os.Exit(1)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	opt := &godo.ListOptions{}
	keyList := []godo.Key{}

	for {
		keyPage, resp, err := client.Keys.List(opt)
		if err != nil {
			fmt.Printf("Unable to list SSH Keys: %s\n", err)
			os.Exit(1)
		}

		// append the current page's droplets to our list
		for _, d := range keyPage {
			keyList = append(keyList, d)
		}

		// if we are at the last page, break out the for loop
		if resp.Links == nil || resp.Links.IsLastPage() {
			break
		}

		page, err := resp.Links.CurrentPage()
		if err != nil {
			fmt.Printf("Unable to get pagination: %s\n", err)
			os.Exit(1)
		}

		// set the page we want for the next request
		opt.Page = page + 1
	}

	cliOut := NewCLIOutput()
	defer cliOut.Flush()
	cliOut.Header("ID", "Name", "Fingerprint")
	for _, key := range keyList {
		cliOut.Writeln("%d\t%s\t%s\n", key.ID, key.Name, key.Fingerprint)
	}
}
开发者ID:jmptrader,项目名称:doctl,代码行数:49,代码来源:sshkey.go

示例11: domainShow

func domainShow(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide name for Domain.")
	}

	name := ctx.Args().First()

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	domain, _, err := client.Domains.Get(name)
	if err != nil {
		log.Fatal(err)
	}

	WriteOutput(domain)
}
开发者ID:phillbaker,项目名称:doctl,代码行数:20,代码来源:domain.go

示例12: domainDestroy

func domainDestroy(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide a name for the domain to destroy.")
	}

	name := ctx.Args().First()

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	_, err := client.Domains.Delete(name)
	if err != nil {
		log.Fatalf("Unable to destroy domain: %s.", err)
	}

	log.Printf("Domain %s destroyed", name)
}
开发者ID:phillbaker,项目名称:doctl,代码行数:20,代码来源:domain.go

示例13: sshFind

func sshFind(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		log.Fatal("Error: Must provide name for Key.")
	}

	name := ctx.Args().First()

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	key, err := FindKeyByName(client, name)
	if err != nil {
		log.Fatal(err)
	}

	WriteOutput(key)
}
开发者ID:phillbaker,项目名称:doctl,代码行数:20,代码来源:sshkey.go

示例14: domainRecordDestroy

func domainRecordDestroy(ctx *cli.Context) {
	if len(ctx.Args()) != 2 {
		fmt.Printf("Error: Must provide domain name and domain record id.\n")
		os.Exit(1)
	}

	domainName := ctx.Args().First()
	recordID, err := strconv.Atoi(ctx.Args()[1])
	if err != nil {
		fmt.Printf("%s\n", err)
		os.Exit(1)
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	_, err = client.Domains.DeleteRecord(domainName, recordID)
	if err != nil {
		fmt.Printf("Unable to destroy domain record: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("Domain record %d destroyed.\n", recordID)
}
开发者ID:jmptrader,项目名称:doctl,代码行数:27,代码来源:domain.go

示例15: domainCreate

func domainCreate(ctx *cli.Context) {
	if len(ctx.Args()) != 2 {
		log.Fatal("Must provide domain name and Droplet name.")
	}

	tokenSource := &TokenSource{
		AccessToken: APIKey,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)

	droplet, err := FindDropletByName(client, ctx.Args()[1])
	if err != nil {
		log.Fatal(err)
	}

	createRequest := &godo.DomainCreateRequest{
		Name:      ctx.Args().First(),
		IPAddress: PublicIPForDroplet(droplet),
	}
	domain, _, err := client.Domains.Create(createRequest)
	if err != nil {
		log.Fatal(err)
	}

	WriteOutput(domain)
}
开发者ID:phillbaker,项目名称:doctl,代码行数:27,代码来源:domain.go


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