github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/env-var/set/set.go (about)

     1  package add
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io/ioutil"
     7  	"strings"
     8  
     9  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
    10  	"github.com/henvic/wedeploycli/command/env-var/internal/commands"
    11  	"github.com/henvic/wedeploycli/command/internal/we"
    12  	"github.com/henvic/wedeploycli/services"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	file    string
    18  	replace bool
    19  )
    20  
    21  // Cmd for setting an environment variable
    22  var Cmd = &cobra.Command{
    23  	Use:     "set",
    24  	Aliases: []string{"add"},
    25  	Short:   "Set an environment variable for a given service",
    26  	Example: `  lcp env-var set key value
    27    lcp env-var set key=value`,
    28  	Args:    checkFileAndArgs,
    29  	PreRunE: preRun,
    30  	RunE:    run,
    31  }
    32  
    33  var setupHost = cmdflagsfromhost.SetupHost{
    34  	Pattern: cmdflagsfromhost.FullHostPattern,
    35  
    36  	Requires: cmdflagsfromhost.Requires{
    37  		Auth:    true,
    38  		Project: true,
    39  		Service: true,
    40  	},
    41  
    42  	PromptMissingService: true,
    43  }
    44  
    45  func init() {
    46  	setupHost.Init(Cmd)
    47  	Cmd.Flags().StringVarP(&file, "file", "F", "",
    48  		"Read environment variables from file")
    49  	Cmd.Flags().BoolVar(&replace, "replace", false,
    50  		"Replace set of environment variables")
    51  }
    52  
    53  func checkFileAndArgs(cmd *cobra.Command, args []string) error {
    54  	if file != "" && len(args) != 0 {
    55  		return errors.New("can't merge environment variables from a file and process arguments")
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func preRun(cmd *cobra.Command, args []string) error {
    62  	return setupHost.Process(context.Background(), we.Context())
    63  }
    64  
    65  func readEnvsFromFile(filepath string) ([]string, error) {
    66  	b, err := ioutil.ReadFile(filepath) // #nosec
    67  
    68  	if err != nil {
    69  		return []string{}, err
    70  	}
    71  
    72  	var value = string(b)
    73  	value = strings.Replace(value, "\r\n", "\n", -1) // windows...
    74  
    75  	return strings.Split(value, "\n"), nil // notice the line ending instead of space
    76  }
    77  
    78  func run(cmd *cobra.Command, args []string) (err error) {
    79  	var c = commands.Command{
    80  		SetupHost:      setupHost,
    81  		ServicesClient: services.New(we.Context()),
    82  	}
    83  
    84  	if file != "" {
    85  		args, err = readEnvsFromFile(file)
    86  
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		c.SkipPrompt = true
    92  	}
    93  
    94  	ctx := context.Background()
    95  
    96  	if replace {
    97  		return c.Replace(ctx, args)
    98  	}
    99  
   100  	return c.Add(ctx, args)
   101  }