當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。