Search code examples
jsongotextaws-cliaws-sdk-go

How to set the output of the aws-sdk-go to "text"?


Although the output setting has been set to text

~/.aws/config

[default]
output=text

the aws-sdk-go returns json. The question is whether the output could be switched to text.

When:

aws route53 get-hosted-zone --id some-id

is run, the output looks as follows:

NAMESERVERS some-ns
NAMESERVERS some-ns1
NAMESERVERS some-ns2
NAMESERVERS some-ns3

According to the this AWS documentation one could set the configuration:

sess, err := session.NewSession(&aws.Config{
    Region: aws.String("us-east-2")},
)

One attempt was to consult this Config struct, but an Output option seems to be omitted.

How to set the output to text?

Note: an issue has added to the github page of the aws-sdk-go as well.

Example

package main

import (
    "fmt"
    "log"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/route53"
)

func main() {
    session, err := session.NewSession()
    if err != nil {
        log.Fatal(err)
    }
    r53 := route53.New(session)

    listParams := &route53.ListResourceRecordSetsInput{
        HostedZoneId: aws.String("some-id"),
    }
    records, err := r53.ListResourceRecordSets(listParams)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(records)
}

returns:

{
  IsTruncated: false,
  MaxItems: "100",
  ResourceRecordSets: [
    {
      Name: "some-domain.",
      ResourceRecords: [{
          Value: "some-ip"
        }],
      TTL: 7200,
      Type: "A"
    }
}

while aws route53 list-resource-record-sets --hosted-zone-id some-id, results in:

RESOURCERECORDSETS      some-domain.     7200    A
RESOURCERECORDS some-ip

Problem

While it is possible to set the format of the aws-cli to output, it does not seem to be possible to do the same for the SDK.


Question

How to let the go-aws-sdk return text rather than json?


Solution

  • I have all of the information you need, you just have to unravel it from the response (records).

    To get similar results from the last cli command:

    for _, recordSet := range records.ResourceRecordSets {
        log.Println("RESOURCERECORDSETS " + *recordSet.Name + strconv.Itoa(int(*recordSet.TTL)) + *recordSet.Type)
        for _, record := range recordSet.ResourceRecords {
            log.Println("RESOURCERECORDS " + *record.Value)
        }
        log.Println("")
    }