github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v3/v3_unset_env_command.go (about)

     1  package v3
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v3action"
     8  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
     9  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion"
    10  	"github.com/liamawhite/cli-with-i18n/command"
    11  	"github.com/liamawhite/cli-with-i18n/command/flag"
    12  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    13  	"github.com/liamawhite/cli-with-i18n/command/v3/shared"
    14  )
    15  
    16  //go:generate counterfeiter . V3UnsetEnvActor
    17  
    18  type V3UnsetEnvActor interface {
    19  	CloudControllerAPIVersion() string
    20  	UnsetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, EnvironmentVariableName string) (v3action.Warnings, error)
    21  }
    22  
    23  type V3UnsetEnvCommand struct {
    24  	RequiredArgs    flag.UnsetEnvironmentArgs `positional-args:"yes"`
    25  	usage           interface{}               `usage:"CF_NAME v3-unset-env APP_NAME ENV_VAR_NAME"`
    26  	relatedCommands interface{}               `related_commands:"v3-apps, v3-env, v3-restart, v3-set-env, v3-stage"`
    27  
    28  	UI          command.UI
    29  	Config      command.Config
    30  	SharedActor command.SharedActor
    31  	Actor       V3UnsetEnvActor
    32  }
    33  
    34  func (cmd *V3UnsetEnvCommand) Setup(config command.Config, ui command.UI) error {
    35  	cmd.UI = ui
    36  	cmd.Config = config
    37  	cmd.SharedActor = sharedaction.NewActor(config)
    38  
    39  	ccClient, _, err := shared.NewClients(config, ui, true)
    40  	if err != nil {
    41  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    42  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionV3}
    43  		}
    44  
    45  		return err
    46  	}
    47  	cmd.Actor = v3action.NewActor(nil, ccClient, config)
    48  
    49  	return nil
    50  }
    51  
    52  func (cmd V3UnsetEnvCommand) Execute(args []string) error {
    53  	cmd.UI.DisplayText(command.ExperimentalWarning)
    54  	cmd.UI.DisplayNewline()
    55  
    56  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionV3)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	err = cmd.SharedActor.CheckTarget(cmd.Config, true, true)
    62  	if err != nil {
    63  		return shared.HandleError(err)
    64  	}
    65  
    66  	user, err := cmd.Config.CurrentUser()
    67  	if err != nil {
    68  		return shared.HandleError(err)
    69  	}
    70  
    71  	appName := cmd.RequiredArgs.AppName
    72  	cmd.UI.DisplayTextWithFlavor("Removing env variable {{.EnvVarName}} from app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    73  		"AppName":    appName,
    74  		"EnvVarName": cmd.RequiredArgs.EnvironmentVariableName,
    75  		"OrgName":    cmd.Config.TargetedOrganization().Name,
    76  		"SpaceName":  cmd.Config.TargetedSpace().Name,
    77  		"Username":   user.Name,
    78  	})
    79  
    80  	warnings, err := cmd.Actor.UnsetEnvironmentVariableByApplicationNameAndSpace(
    81  		appName,
    82  		cmd.Config.TargetedSpace().GUID,
    83  		cmd.RequiredArgs.EnvironmentVariableName,
    84  	)
    85  	cmd.UI.DisplayWarnings(warnings)
    86  	if err != nil {
    87  		switch errVal := err.(type) {
    88  		case v3action.EnvironmentVariableNotSetError:
    89  			cmd.UI.DisplayText(errVal.Error())
    90  		default:
    91  			return shared.HandleError(err)
    92  		}
    93  	}
    94  
    95  	cmd.UI.DisplayOK()
    96  	if err == nil {
    97  		cmd.UI.DisplayText("TIP: Use 'cf v3-stage {{.AppName}}' to ensure your env variable changes take effect.", map[string]interface{}{
    98  			"AppName": appName,
    99  		})
   100  	}
   101  
   102  	return nil
   103  }