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


Golang Context.String方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: sshDestroy

func sshDestroy(ctx *cli.Context) {
	if ctx.Int("id") == 0 && ctx.String("fingerprint") == "" && len(ctx.Args()) < 1 {
		fmt.Printf("Error: Must provide ID, fingerprint or name for SSH Key to destroy.\n")
		os.Exit(1)
	}

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

	id := ctx.Int("id")
	fingerprint := ctx.String("fingerprint")
	var key godo.Key
	if id == 0 && fingerprint == "" {
		key, err := FindKeyByName(client, ctx.Args().First())
		if err != nil {
			fmt.Printf("%s\n", err)
			os.Exit(64)
		} else {
			id = key.ID
		}
	} else if id != 0 {
		key, _, err := client.Keys.GetByID(id)
		if err != nil {
			fmt.Printf("Unable to find SSH Key: %s\n", err)
			os.Exit(1)
		} else {
			id = key.ID
		}
	} else {
		key, _, err := client.Keys.GetByFingerprint(fingerprint)
		if err != nil {
			fmt.Printf("Unable to find SSH Key: %s\n", err)
			os.Exit(1)
		} else {
			id = key.ID
		}
	}

	_, err := client.Keys.DeleteByID(id)
	if err != nil {
		fmt.Printf("Unable to destroy SSH Key: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("Key %s destroyed.\n", key.Name)
}
开发者ID:jmptrader,项目名称:doctl,代码行数:49,代码来源:sshkey.go

示例5: dropletActionResize

func dropletActionResize(ctx *cli.Context) {
	if ctx.Int("id") == 0 && len(ctx.Args()) != 1 {
		fmt.Printf("Error: Must provide ID or name for Droplet to destroy.\n")
		os.Exit(1)
	}

	size := ctx.String("size")
	disk := ctx.Bool("disk")

	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 {
			fmt.Printf("%s\n", err)
			os.Exit(64)
		} else {
			id = droplet.ID
		}
	}

	droplet, _, err := client.Droplets.Get(id)
	if err != nil {
		fmt.Printf("Unable to find Droplet: %s\n", err)
		os.Exit(1)
	}

	action, _, err := client.DropletActions.Resize(droplet.ID, size, disk)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

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

示例6: domainRecordCreate

func domainRecordCreate(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		cli.ShowAppHelp(ctx)
		fmt.Printf("Must specify a domain name to add a record to.\n")
		os.Exit(1)
	}

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

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

	createRequest := &godo.DomainRecordEditRequest{
		Type: strings.ToUpper(ctx.String("type")),
		Name: ctx.String("name"),
		Data: ctx.String("data"),
	}

	if createRequest.Type == "MX" || createRequest.Type == "SRV" {
		createRequest.Priority = ctx.Int("priority")
	}
	if createRequest.Type == "SRV" {
		createRequest.Port = ctx.Int("port")
		createRequest.Weight = ctx.Int("weight")
	}

	domainRecord, _, err := client.Domains.CreateRecord(domainName, createRequest)
	if err != nil {
		fmt.Printf("%s\n", err)
		os.Exit(1)
	}

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

示例7: dropletCreate

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

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

	// Add domain to end if available.
	dropletName := ctx.Args().First()
	if ctx.Bool("add-region") {
		dropletName = fmt.Sprintf("%s.%s", dropletName, ctx.String("region"))
	}
	if ctx.String("domain") != "" {
		dropletName = fmt.Sprintf("%s.%s", dropletName, ctx.String("domain"))
	}

	// Loop through the SSH Keys and add by name. DO API should have handled
	// this case as well.
	var sshKeys []godo.DropletCreateSSHKey
	keyNames := ctx.String("ssh-keys")
	if keyNames != "" {
		for _, keyName := range strings.Split(keyNames, ",") {
			sshKey, err := FindKeyByName(client, keyName)
			if sshKey != nil && err == nil {
				sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ID: sshKey.ID})
			} else {
				log.Fatalf("Warning: Could not find key: %q.", keyName)
			}
		}
	}

	userData := ""
	userDataPath := ctx.String("user-data-file")
	if userDataPath != "" {
		file, err := os.Open(userDataPath)
		if err != nil {
			log.Fatalf("Error opening user data file: %s.", err)
		}

		userDataFile, err := ioutil.ReadAll(file)
		if err != nil {
			log.Fatalf("Error reading user data file: %s.", err)
		}
		userData = string(userDataFile)
	} else {
		userData = ctx.String("user-data")
	}

	createRequest := &godo.DropletCreateRequest{
		Name:   dropletName,
		Region: ctx.String("region"),
		Size:   ctx.String("size"),
		Image: godo.DropletCreateImage{
			Slug: ctx.String("image"),
		},
		SSHKeys:           sshKeys,
		Backups:           ctx.Bool("backups"),
		IPv6:              ctx.Bool("ipv6"),
		PrivateNetworking: ctx.Bool("private-networking"),
		UserData:          userData,
	}

	droplet, resp, err := client.Droplets.Create(createRequest)
	if err != nil {
		log.Fatalf("Unable to create Droplet: %s.", err)
	}

	if ctx.Bool("wait-for-active") {
		util.WaitForActive(client, resp.Links.Actions[0].HREF)
	}
	WriteOutput(droplet)
}
开发者ID:wojtekzw,项目名称:doctl,代码行数:76,代码来源:droplet.go


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