本文整理汇总了Golang中github.com/digitalocean/doctl/Godeps/_workspace/src/golang.org/x/oauth2.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的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)
}
示例2: DefaultClient
// DefaultClient returns an HTTP Client that uses the
// DefaultTokenSource to obtain authentication credentials.
//
// This client should be used when developing services
// that run on Google App Engine or Google Compute Engine
// and use "Application Default Credentials."
//
// For more details, see:
// https://developers.google.com/accounts/docs/application-default-credentials
//
func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error) {
ts, err := DefaultTokenSource(ctx, scope...)
if err != nil {
return nil, err
}
return oauth2.NewClient(ctx, ts), nil
}
示例3: domainRecordShow
func domainRecordShow(ctx *cli.Context) {
if len(ctx.Args()) == 2 {
fmt.Printf("Error: Must provide domain name and domain record id.\n")
os.Exit(64)
}
tokenSource := &TokenSource{
AccessToken: APIKey,
}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
client := godo.NewClient(oauthClient)
domainName := ctx.Args().First()
recordID, err := strconv.Atoi(ctx.Args()[1])
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
domainDecord, _, err := client.Domains.Record(domainName, recordID)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
WriteOutput(domainDecord)
}
示例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)
}
}
示例5: actionList
func actionList(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: ctx.Int("page"),
PerPage: ctx.Int("page-size"),
}
actionList, _, err := client.Actions.List(opt)
if err != nil {
fmt.Printf("Unable to list Actions: %s\n", err)
os.Exit(1)
}
cliOut := NewCLIOutput()
defer cliOut.Flush()
cliOut.Header("ID", "Region", "ResourceType", "ResourceID", "Type", "StartedAt", "CompletedAt", "Status")
for _, action := range actionList {
cliOut.Writeln("%d\t%s\t%s\t%d\t%s\t%s\t%s\t%s\n",
action.ID, action.RegionSlug, action.ResourceType, action.ResourceID, action.Type, action.StartedAt, action.CompletedAt, action.Status)
}
}
示例6: dropletActionUpgrade
func dropletActionUpgrade(ctx *cli.Context) {
if ctx.Int("id") == 0 && len(ctx.Args()) != 1 {
log.Fatal("Error: Must provide ID or name for Droplet to resize.")
}
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.Upgrade(droplet.ID)
if err != nil {
log.Fatal(err)
}
WriteOutput(action)
}
示例7: 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)
}
示例8: domainList
func domainList(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: ctx.Int("page"),
PerPage: ctx.Int("page-size"),
}
domainList, _, err := client.Domains.List(opt)
if err != nil {
log.Fatalf("Unable to list Domains: %s.", err)
}
cliOut := NewCLIOutput()
defer cliOut.Flush()
cliOut.Header("Name", "TTL")
for _, domain := range domainList {
cliOut.Writeln("%s\t%d\n", domain.Name, domain.TTL)
}
}
示例9: 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)
}
示例10: 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)
}
}
示例11: 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)
}
示例12: 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)
}
示例13: 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)
}
}
示例14: accountShow
func accountShow(ctx *cli.Context) {
tokenSource := &TokenSource{
AccessToken: APIKey,
}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
client := godo.NewClient(oauthClient)
account, _, err := client.Account.Get()
if err != nil {
log.Fatal(err)
}
WriteOutput(account)
}
示例15: 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)
}
}