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

     1  package unset
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
     7  	"github.com/henvic/wedeploycli/command/env-var/internal/commands"
     8  	"github.com/henvic/wedeploycli/command/internal/we"
     9  	"github.com/henvic/wedeploycli/services"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // Cmd for removing an environment variable
    14  var Cmd = &cobra.Command{
    15  	Use:     "rm",
    16  	Aliases: []string{"unset", "del", "delete"},
    17  	Short:   "Remove an environment variable for a given service",
    18  	Example: "  lcp env-var rm foo",
    19  	PreRunE: preRun,
    20  	RunE:    run,
    21  }
    22  
    23  var setupHost = cmdflagsfromhost.SetupHost{
    24  	Pattern: cmdflagsfromhost.FullHostPattern,
    25  
    26  	Requires: cmdflagsfromhost.Requires{
    27  		Auth:    true,
    28  		Project: true,
    29  		Service: true,
    30  	},
    31  
    32  	PromptMissingService: true,
    33  }
    34  
    35  func init() {
    36  	setupHost.Init(Cmd)
    37  }
    38  
    39  func preRun(cmd *cobra.Command, args []string) error {
    40  	return setupHost.Process(context.Background(), we.Context())
    41  }
    42  
    43  func run(cmd *cobra.Command, args []string) error {
    44  	var c = commands.Command{
    45  		SetupHost:      setupHost,
    46  		ServicesClient: services.New(we.Context()),
    47  	}
    48  
    49  	ctx := context.Background()
    50  
    51  	if len(args) == 0 {
    52  		if err := c.Show(ctx); err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	return c.Delete(ctx, args)
    58  }