github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/external/test-programs/tf-acc-external-data-source/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  )
     9  
    10  // This is a minimal implementation of the external data source protocol
    11  // intended only for use in the provider acceptance tests.
    12  //
    13  // In practice it's likely not much harder to just write a real Terraform
    14  // plugin if you're going to be writing your data source in Go anyway;
    15  // this example is just in Go because we want to avoid introducing
    16  // additional language runtimes into the test environment.
    17  func main() {
    18  	queryBytes, err := ioutil.ReadAll(os.Stdin)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  
    23  	var query map[string]string
    24  	err = json.Unmarshal(queryBytes, &query)
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  
    29  	if query["fail"] != "" {
    30  		fmt.Fprintf(os.Stderr, "I was asked to fail\n")
    31  		os.Exit(1)
    32  	}
    33  
    34  	var result = map[string]string{
    35  		"result":      "yes",
    36  		"query_value": query["value"],
    37  	}
    38  
    39  	if len(os.Args) >= 2 {
    40  		result["argument"] = os.Args[1]
    41  	}
    42  
    43  	resultBytes, err := json.Marshal(result)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	os.Stdout.Write(resultBytes)
    49  	os.Exit(0)
    50  }