github.com/chenbh/concourse/v6@v6.4.2/worker/runtime/integration/sample/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"net/http"
     9  	"os"
    10  	"os/signal"
    11  	"strings"
    12  	"syscall"
    13  	"time"
    14  )
    15  
    16  var (
    17  	flagWaitForSignal = flag.String("wait-for-signal", "", "wait for a sigal (sigterm|sighup)")
    18  	flagHttpGet       = flag.String("http-get", "", "website to perform an HTTP GET request against")
    19  	flagWriteTenTimes = flag.String("write-many-times", "", "writes a string to stdout many times")
    20  	flagCatFile = flag.String("cat", "", "writes contents of file to stdout")
    21  
    22  	signals = map[string]os.Signal{
    23  		"sighup":  syscall.SIGHUP,
    24  		"sigterm": syscall.SIGTERM,
    25  	}
    26  )
    27  
    28  const defaultMessage = "hello world"
    29  
    30  // waitForSignal blocks until the signal we care about (`sig`) is develired.
    31  //
    32  // ps.: all other signals are ignored.
    33  //
    34  func waitForSignal(sig string) {
    35  	s, found := signals[strings.ToLower(sig)]
    36  	if !found {
    37  		log.Fatal("signal %s not found - available: %v",
    38  			sig, signals,
    39  		)
    40  	}
    41  
    42  	ch := make(chan os.Signal)
    43  	signal.Ignore()
    44  	signal.Notify(ch, s)
    45  	<-ch
    46  
    47  	fmt.Println("got signaled!")
    48  }
    49  
    50  func httpGet(url string) {
    51  	client := &http.Client{
    52  		CheckRedirect: func(req *http.Request, via []*http.Request) error {
    53  			return http.ErrUseLastResponse
    54  		},
    55  	}
    56  
    57  	resp, err := client.Get(url)
    58  	if err != nil {
    59  		log.Fatal("failed performing http get", err)
    60  	}
    61  
    62  	fmt.Println(resp.Status)
    63  }
    64  
    65  func writeTenTimes(content string) {
    66  	for i := 0; i < 20; i++ {
    67  		fmt.Println(content)
    68  		time.Sleep(300 * time.Millisecond)
    69  	}
    70  }
    71  
    72  func catFile(pathToFile string){
    73  	bytes, err	:= ioutil.ReadFile(pathToFile)
    74  	if err != nil {
    75  		log.Fatal("failed to read file ", err)
    76  	}
    77  	fmt.Print(string(bytes))
    78  }
    79  
    80  func main() {
    81  	flag.Parse()
    82  
    83  	switch {
    84  	case *flagWaitForSignal != "":
    85  		waitForSignal(*flagWaitForSignal)
    86  	case *flagHttpGet != "":
    87  		httpGet(*flagHttpGet)
    88  	case *flagWriteTenTimes != "":
    89  		writeTenTimes(*flagWriteTenTimes)
    90  	case *flagCatFile != "":
    91  		catFile(*flagCatFile)
    92  	default:
    93  		fmt.Println(defaultMessage)
    94  	}
    95  }