github.com/greenboxal/deis@v1.12.1/builder/src/get-app-config.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"flag"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"os"
    11  
    12  	"github.com/deis/deis/builder"
    13  )
    14  
    15  const (
    16  	contentType string = "application/json"
    17  	userAgent   string = "deis-builder"
    18  )
    19  
    20  func init() {
    21  	flag.Usage = func() {
    22  		fmt.Fprintf(os.Stderr, "Usage: [options]\n\n")
    23  		flag.PrintDefaults()
    24  	}
    25  }
    26  
    27  func main() {
    28  	url := flag.String("url", "", "Controller hook URL")
    29  	builderKey := flag.String("key", "", "Builder Key")
    30  	user := flag.String("user", "", "Controller username")
    31  	app := flag.String("app", "", "Controller application name")
    32  
    33  	flag.Parse()
    34  
    35  	if flag.NFlag() < 4 {
    36  		flag.Usage()
    37  		os.Exit(1)
    38  	}
    39  
    40  	if *url == "" {
    41  		fmt.Println("invalid url")
    42  		os.Exit(1)
    43  	}
    44  
    45  	if *builderKey == "" {
    46  		fmt.Println("invalid builder key")
    47  		os.Exit(1)
    48  	}
    49  
    50  	if *user == "" {
    51  		fmt.Println("invalid user")
    52  		os.Exit(1)
    53  	}
    54  
    55  	if *app == "" {
    56  		fmt.Println("invalid app")
    57  		os.Exit(1)
    58  	}
    59  
    60  	data, err := json.Marshal(&builder.ConfigHook{ReceiveUser: *user, ReceiveRepo: *app})
    61  
    62  	if err != nil {
    63  		fmt.Println(err)
    64  		os.Exit(1)
    65  	}
    66  
    67  	b := bytes.NewReader(data)
    68  	client := &http.Client{}
    69  	req, err := http.NewRequest("POST", *url, b)
    70  
    71  	if err != nil {
    72  		fmt.Println(err)
    73  		os.Exit(1)
    74  	}
    75  
    76  	req.Header.Add("Content-Type", contentType)
    77  	req.Header.Add("Accept", contentType)
    78  	req.Header.Add("User-Agent", userAgent)
    79  	req.Header.Add("X-Deis-Builder-Auth", *builderKey)
    80  
    81  	res, err := client.Do(req)
    82  	defer res.Body.Close()
    83  
    84  	if res.StatusCode == 404 {
    85  		fmt.Println("Check the Controller. Is it running?")
    86  		os.Exit(1)
    87  	}
    88  
    89  	body, err := ioutil.ReadAll(res.Body)
    90  	if err != nil {
    91  		fmt.Println(err)
    92  		os.Exit(1)
    93  	}
    94  
    95  	if err != nil || res.StatusCode != 200 {
    96  		fmt.Println("failed retrieving config from controller")
    97  		fmt.Printf("%v\n", body)
    98  		os.Exit(1)
    99  	}
   100  
   101  	config, err := builder.ParseConfig(body)
   102  	if err != nil {
   103  		fmt.Println("failed parsing config from controller")
   104  		fmt.Printf("%v\n", err)
   105  		os.Exit(1)
   106  	}
   107  	toString, err := json.Marshal(config)
   108  	fmt.Println(string(toString))
   109  }