github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v7/env_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/actor/v7action"
    10  	"code.cloudfoundry.org/cli/command"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	"code.cloudfoundry.org/cli/command/v7/shared"
    13  )
    14  
    15  //go:generate counterfeiter . EnvActor
    16  
    17  type EnvActor interface {
    18  	GetEnvironmentVariablesByApplicationNameAndSpace(appName string, spaceGUID string) (v7action.EnvironmentVariableGroups, v7action.Warnings, error)
    19  }
    20  
    21  type EnvCommand struct {
    22  	RequiredArgs    flag.EnvironmentArgs `positional-args:"yes"`
    23  	usage           interface{}          `usage:"CF_NAME env APP_NAME"`
    24  	relatedCommands interface{}          `related_commands:"app, v3-apps, set-env, unset-env, running-environment-variable-group, staging-environment-variable-group"`
    25  
    26  	UI          command.UI
    27  	Config      command.Config
    28  	SharedActor command.SharedActor
    29  	Actor       EnvActor
    30  }
    31  
    32  func (cmd *EnvCommand) Setup(config command.Config, ui command.UI) error {
    33  	cmd.UI = ui
    34  	cmd.Config = config
    35  	cmd.SharedActor = sharedaction.NewActor(config)
    36  
    37  	ccClient, _, err := shared.NewClients(config, ui, true, "")
    38  	if err != nil {
    39  		return err
    40  	}
    41  	cmd.Actor = v7action.NewActor(ccClient, config, nil, nil)
    42  
    43  	return nil
    44  }
    45  
    46  func (cmd EnvCommand) Execute(_ []string) error {
    47  	err := cmd.SharedActor.CheckTarget(true, true)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	user, err := cmd.Config.CurrentUser()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	appName := cmd.RequiredArgs.AppName
    58  	cmd.UI.DisplayTextWithFlavor("Getting env variables for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    59  		"AppName":   appName,
    60  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    61  		"SpaceName": cmd.Config.TargetedSpace().Name,
    62  		"Username":  user.Name,
    63  	})
    64  	cmd.UI.DisplayOK()
    65  
    66  	envGroups, warnings, err := cmd.Actor.GetEnvironmentVariablesByApplicationNameAndSpace(
    67  		appName,
    68  		cmd.Config.TargetedSpace().GUID,
    69  	)
    70  	cmd.UI.DisplayWarnings(warnings)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	if len(envGroups.System) > 0 || len(envGroups.Application) > 0 {
    76  		cmd.UI.DisplayHeader("System-Provided:")
    77  		cmd.displaySystem(envGroups.System)
    78  		if len(envGroups.Application) > 0 {
    79  			cmd.UI.DisplayNewline()
    80  			cmd.displaySystem(envGroups.Application)
    81  		}
    82  	} else {
    83  		cmd.UI.DisplayText("No system-provided env variables have been set")
    84  	}
    85  	cmd.UI.DisplayNewline()
    86  
    87  	if len(envGroups.EnvironmentVariables) > 0 {
    88  		cmd.UI.DisplayHeader("User-Provided:")
    89  		cmd.displayEnvGroup(envGroups.EnvironmentVariables)
    90  	} else {
    91  		cmd.UI.DisplayText("No user-provided env variables have been set")
    92  	}
    93  	cmd.UI.DisplayNewline()
    94  
    95  	if len(envGroups.Running) > 0 {
    96  		cmd.UI.DisplayHeader("Running Environment Variable Groups:")
    97  		cmd.displayEnvGroup(envGroups.Running)
    98  	} else {
    99  		cmd.UI.DisplayText("No running env variables have been set")
   100  	}
   101  	cmd.UI.DisplayNewline()
   102  
   103  	if len(envGroups.Staging) > 0 {
   104  		cmd.UI.DisplayHeader("Staging Environment Variable Groups:")
   105  		cmd.displayEnvGroup(envGroups.Staging)
   106  	} else {
   107  		cmd.UI.DisplayText("No staging env variables have been set")
   108  	}
   109  	cmd.UI.DisplayNewline()
   110  
   111  	return nil
   112  }
   113  
   114  func (cmd EnvCommand) displayEnvGroup(group map[string]interface{}) error {
   115  	keys := sortKeys(group)
   116  
   117  	for _, key := range keys {
   118  		cmd.UI.DisplayText(fmt.Sprintf("%s: %v", key, group[key]))
   119  	}
   120  	return nil
   121  }
   122  
   123  func sortKeys(group map[string]interface{}) []string {
   124  	keys := make([]string, 0, len(group))
   125  	for key := range group {
   126  		keys = append(keys, key)
   127  	}
   128  	sort.Strings(keys)
   129  	return keys
   130  }
   131  
   132  func (cmd EnvCommand) displaySystem(group map[string]interface{}) error {
   133  	for key, val := range group {
   134  		jsonVal, err := json.MarshalIndent(val, "", " ")
   135  		if err != nil {
   136  			return err
   137  		}
   138  		cmd.UI.DisplayText(fmt.Sprintf("%s: %s", key, jsonVal))
   139  	}
   140  	return nil
   141  }