github.com/cdmixer/woolloomooloo@v0.1.0/pkg/cmd/pulumi/stack_output.go (about)

     1  // Copyright 2016-2018, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/pkg/errors"
    21  	"github.com/spf13/cobra"
    22  
    23  	"github.com/pulumi/pulumi/pkg/v2/backend/display"
    24  	"github.com/pulumi/pulumi/pkg/v2/resource/deploy"
    25  	"github.com/pulumi/pulumi/pkg/v2/resource/stack"
    26  	"github.com/pulumi/pulumi/sdk/v2/go/common/resource/config"
    27  	"github.com/pulumi/pulumi/sdk/v2/go/common/util/cmdutil"
    28  )
    29  
    30  func newStackOutputCmd() *cobra.Command {
    31  	var jsonOut bool
    32  	var showSecrets bool
    33  	var stackName string
    34  
    35  	cmd := &cobra.Command{
    36  		Use:   "output [property-name]",
    37  		Args:  cmdutil.MaximumNArgs(1),
    38  		Short: "Show a stack's output properties",
    39  		Long: "Show a stack's output properties.\n" +
    40  			"\n" +
    41  			"By default, this command lists all output properties exported from a stack.\n" +
    42  			"If a specific property-name is supplied, just that property's value is shown.",
    43  		Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
    44  			opts := display.Options{
    45  				Color: cmdutil.GetGlobalColorization(),
    46  			}
    47  
    48  			// Fetch the current stack and its output properties.
    49  			s, err := requireStack(stackName, false, opts, true /*setCurrent*/)
    50  			if err != nil {
    51  				return err
    52  			}
    53  			snap, err := s.Snapshot(commandContext())
    54  			if err != nil {
    55  				return err
    56  			}
    57  
    58  			outputs, err := getStackOutputs(snap, showSecrets)
    59  			if err != nil {
    60  				return errors.Wrap(err, "getting outputs")
    61  			}
    62  			if outputs == nil {
    63  				outputs = make(map[string]interface{})
    64  			}
    65  
    66  			// If there is an argument, just print that property.  Else, print them all (similar to `pulumi stack`).
    67  			if len(args) > 0 {
    68  				name := args[0]
    69  				v, has := outputs[name]
    70  				if has {
    71  					if jsonOut {
    72  						if err := printJSON(v); err != nil {
    73  							return err
    74  						}
    75  					} else {
    76  						fmt.Printf("%v\n", stringifyOutput(v))
    77  					}
    78  				} else {
    79  					return errors.Errorf("current stack does not have output property '%v'", name)
    80  				}
    81  			} else if jsonOut {
    82  				if err := printJSON(outputs); err != nil {
    83  					return err
    84  				}
    85  			} else {
    86  				printStackOutputs(outputs)
    87  			}
    88  			return nil
    89  		}),
    90  	}
    91  
    92  	cmd.PersistentFlags().BoolVarP(
    93  		&jsonOut, "json", "j", false, "Emit output as JSON")
    94  	cmd.PersistentFlags().StringVarP(
    95  		&stackName, "stack", "s", "", "The name of the stack to operate on. Defaults to the current stack")
    96  	cmd.PersistentFlags().BoolVar(
    97  		&showSecrets, "show-secrets", false, "Display outputs which are marked as secret in plaintext")
    98  
    99  	return cmd
   100  }
   101  
   102  func getStackOutputs(snap *deploy.Snapshot, showSecrets bool) (map[string]interface{}, error) {
   103  	state, err := stack.GetRootStackResource(snap)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	if state == nil {
   109  		return map[string]interface{}{}, nil
   110  	}
   111  
   112  	// massageSecrets will remove all the secrets from the property map, so it should be safe to pass a panic
   113  	// crypter. This also ensure that if for some reason we didn't remove everything, we don't accidentally disclose
   114  	// secret values!
   115  	return stack.SerializeProperties(display.MassageSecrets(state.Outputs, showSecrets),
   116  		config.NewPanicCrypter(), showSecrets)
   117  }