github.com/criteo-forks/consul@v1.4.5-criteonogrpc/command/helpers/helpers.go (about)

     1  package helpers
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  )
    10  
    11  func LoadDataSource(data string, testStdin io.Reader) (string, error) {
    12  	var stdin io.Reader = os.Stdin
    13  	if testStdin != nil {
    14  		stdin = testStdin
    15  	}
    16  
    17  	// Handle empty quoted shell parameters
    18  	if len(data) == 0 {
    19  		return "", nil
    20  	}
    21  
    22  	switch data[0] {
    23  	case '@':
    24  		data, err := ioutil.ReadFile(data[1:])
    25  		if err != nil {
    26  			return "", fmt.Errorf("Failed to read file: %s", err)
    27  		} else {
    28  			return string(data), nil
    29  		}
    30  	case '-':
    31  		if len(data) > 1 {
    32  			return data, nil
    33  		}
    34  		var b bytes.Buffer
    35  		if _, err := io.Copy(&b, stdin); err != nil {
    36  			return "", fmt.Errorf("Failed to read stdin: %s", err)
    37  		}
    38  		return b.String(), nil
    39  	default:
    40  		return data, nil
    41  	}
    42  }