github.com/greenboxal/deis@v1.12.1/deisctl/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/deis/deis/deisctl/utils"
    12  )
    13  
    14  // fileKeys define config keys to be read from local files
    15  var fileKeys = []string{
    16  	"/deis/platform/sshPrivateKey",
    17  	"/deis/router/sslCert",
    18  	"/deis/router/sslKey",
    19  	"/deis/router/sslDhparam"}
    20  
    21  // b64Keys define config keys to be base64 encoded before stored
    22  var b64Keys = []string{"/deis/platform/sshPrivateKey"}
    23  
    24  // Config runs the config subcommand
    25  func Config(target string, action string, key []string, cb Backend) error {
    26  	return doConfig(target, action, key, cb, os.Stdout)
    27  }
    28  
    29  // CheckConfig looks for a value at a keyspace path
    30  // and returns an error if a value is not found
    31  func CheckConfig(root string, k string, cb Backend) error {
    32  
    33  	_, err := doConfigGet(cb, root, []string{k})
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func doConfig(target string, action string, key []string, cb Backend, w io.Writer) error {
    42  	rootPath := "/deis/" + target + "/"
    43  
    44  	var vals []string
    45  	var err error
    46  
    47  	switch action {
    48  	case "rm":
    49  		vals, err = doConfigRm(cb, rootPath, key)
    50  	case "set":
    51  		vals, err = doConfigSet(cb, rootPath, key)
    52  	default:
    53  		vals, err = doConfigGet(cb, rootPath, key)
    54  	}
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	// print results
    60  	for _, v := range vals {
    61  		fmt.Fprintf(w, "%v\n", v)
    62  	}
    63  	return nil
    64  }
    65  
    66  func doConfigSet(cb Backend, root string, kvs []string) ([]string, error) {
    67  	var result []string
    68  
    69  	for _, kv := range kvs {
    70  
    71  		// split k/v from args
    72  		split := strings.SplitN(kv, "=", 2)
    73  		k, v := split[0], split[1]
    74  
    75  		// prepare path and value
    76  		path := root + k
    77  		val, err := valueForPath(path, v)
    78  		if err != nil {
    79  			return result, err
    80  		}
    81  
    82  		// set key/value in config backend
    83  		ret, err := cb.Set(path, val)
    84  		if err != nil {
    85  			return result, err
    86  		}
    87  		result = append(result, ret)
    88  
    89  	}
    90  	return result, nil
    91  }
    92  
    93  func doConfigGet(cb Backend, root string, keys []string) ([]string, error) {
    94  	var result []string
    95  	for _, k := range keys {
    96  		val, err := cb.Get(root + k)
    97  		if err != nil {
    98  			return result, err
    99  		}
   100  		result = append(result, val)
   101  	}
   102  	return result, nil
   103  }
   104  
   105  func doConfigRm(cb Backend, root string, keys []string) ([]string, error) {
   106  	var result []string
   107  	for _, k := range keys {
   108  		err := cb.Delete(root + k)
   109  		if err != nil {
   110  			return result, err
   111  		}
   112  		result = append(result, k)
   113  	}
   114  	return result, nil
   115  }
   116  
   117  // valueForPath returns the canonical value for a user-defined path and value
   118  func valueForPath(path string, v string) (string, error) {
   119  
   120  	// check if path is part of fileKeys
   121  	for _, p := range fileKeys {
   122  
   123  		if path == p {
   124  
   125  			// read value from filesystem
   126  			bytes, err := ioutil.ReadFile(utils.ResolvePath(v))
   127  			if err != nil {
   128  				return "", err
   129  			}
   130  
   131  			// see if we should return base64 encoded value
   132  			for _, pp := range b64Keys {
   133  				if path == pp {
   134  					return base64.StdEncoding.EncodeToString(bytes), nil
   135  				}
   136  			}
   137  
   138  			return string(bytes), nil
   139  		}
   140  	}
   141  
   142  	return v, nil
   143  
   144  }