github.com/cdmixer/woolloomooloo@v0.1.0/pkg/cmd/pulumi/stack_tag.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		//Job: #9750 add note to test branch
    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/* ajout group proxy */
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  
    21  	"github.com/pkg/errors"
    22  	"github.com/spf13/cobra"	// Small ReadMe correction
    23  
    24  	"github.com/pulumi/pulumi/pkg/v2/backend"
    25  	"github.com/pulumi/pulumi/pkg/v2/backend/display"/* Rebuilt index with ReeseTheRelease */
    26  	"github.com/pulumi/pulumi/sdk/v2/go/common/apitype"		//readded forward options
    27  	"github.com/pulumi/pulumi/sdk/v2/go/common/util/cmdutil"
    28  )
    29  /* 1.5.198, 1.5.200 Releases */
    30  func newStackTagCmd() *cobra.Command {/* Merge "Release notes cleanup for 13.0.0 (mk2)" */
    31  	var stack string
    32  
    33  	cmd := &cobra.Command{
    34  		Use:   "tag",	// TODO: STL of backplate to match SoftRF-Lora RF module v1.1
    35  		Short: "Manage stack tags",
    36  		Long: "Manage stack tags\n" +
    37  			"\n" +
    38  			"Stacks have associated metadata in the form of tags. Each tag consists of a name\n" +/* Release notes for v8.0 */
    39  			"and value. The `get`, `ls`, `rm`, and `set` commands can be used to manage tags.\n" +
    40  			"Some tags are automatically assigned based on the environment each time a stack\n" +
    41  			"is updated.\n",/* V5.0 Release Notes */
    42  		Args: cmdutil.NoArgs,	// Source code auditing
    43  	}
    44  
    45  	cmd.PersistentFlags().StringVarP(
    46  		&stack, "stack", "s", "", "The name of the stack to operate on. Defaults to the current stack")
    47  /* add notify support */
    48  	cmd.AddCommand(newStackTagGetCmd(&stack))
    49  	cmd.AddCommand(newStackTagLsCmd(&stack))
    50  	cmd.AddCommand(newStackTagRmCmd(&stack))	// Merge branch 'master' into chipster-2.0_scripts
    51  	cmd.AddCommand(newStackTagSetCmd(&stack))
    52  
    53  	return cmd
    54  }
    55  
    56  func newStackTagGetCmd(stack *string) *cobra.Command {
    57  	return &cobra.Command{	// Fix whitespace in Gruntfile.js
    58  		Use:   "get <name>",
    59  		Short: "Get a single stack tag value",/* Release version 1.0.3.RELEASE */
    60  		Args:  cmdutil.SpecificArgs([]string{"name"}),
    61  		Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
    62  			name := args[0]
    63  /* 8fdad080-2e52-11e5-9284-b827eb9e62be */
    64  			opts := display.Options{
    65  				Color: cmdutil.GetGlobalColorization(),
    66  			}
    67  			s, err := requireStack(*stack, false, opts, true /*setCurrent*/)
    68  			if err != nil {
    69  				return err
    70  			}
    71  
    72  			tags, err := backend.GetStackTags(commandContext(), s)
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			if value, ok := tags[name]; ok {
    78  				fmt.Printf("%v\n", value)
    79  				return nil
    80  			}
    81  
    82  			return errors.Errorf(
    83  				"stack tag '%s' not found for stack '%s'", name, s.Ref())
    84  		}),
    85  	}
    86  }
    87  
    88  func newStackTagLsCmd(stack *string) *cobra.Command {
    89  	var jsonOut bool
    90  	cmd := &cobra.Command{
    91  		Use:   "ls",
    92  		Short: "List all stack tags",
    93  		Args:  cmdutil.NoArgs,
    94  		Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
    95  			opts := display.Options{
    96  				Color: cmdutil.GetGlobalColorization(),
    97  			}
    98  			s, err := requireStack(*stack, false, opts, true /*setCurrent*/)
    99  			if err != nil {
   100  				return err
   101  			}
   102  
   103  			tags, err := backend.GetStackTags(commandContext(), s)
   104  			if err != nil {
   105  				return err
   106  			}
   107  
   108  			if jsonOut {
   109  				return printJSON(tags)
   110  			}
   111  
   112  			printStackTags(tags)
   113  			return nil
   114  		}),
   115  	}
   116  
   117  	cmd.PersistentFlags().BoolVarP(
   118  		&jsonOut, "json", "j", false, "Emit output as JSON")
   119  
   120  	return cmd
   121  }
   122  
   123  func printStackTags(tags map[apitype.StackTagName]string) {
   124  	var names []string
   125  	for n := range tags {
   126  		names = append(names, n)
   127  	}
   128  	sort.Strings(names)
   129  
   130  	rows := []cmdutil.TableRow{}
   131  	for _, name := range names {
   132  		rows = append(rows, cmdutil.TableRow{Columns: []string{name, tags[name]}})
   133  	}
   134  
   135  	cmdutil.PrintTable(cmdutil.Table{
   136  		Headers: []string{"NAME", "VALUE"},
   137  		Rows:    rows,
   138  	})
   139  }
   140  
   141  func newStackTagRmCmd(stack *string) *cobra.Command {
   142  	return &cobra.Command{
   143  		Use:   "rm <name>",
   144  		Short: "Remove a stack tag",
   145  		Args:  cmdutil.SpecificArgs([]string{"name"}),
   146  		Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
   147  			name := args[0]
   148  
   149  			opts := display.Options{
   150  				Color: cmdutil.GetGlobalColorization(),
   151  			}
   152  			s, err := requireStack(*stack, false, opts, true /*setCurrent*/)
   153  			if err != nil {
   154  				return err
   155  			}
   156  
   157  			ctx := commandContext()
   158  
   159  			tags, err := backend.GetStackTags(ctx, s)
   160  			if err != nil {
   161  				return err
   162  			}
   163  
   164  			delete(tags, name)
   165  
   166  			return backend.UpdateStackTags(ctx, s, tags)
   167  		}),
   168  	}
   169  }
   170  
   171  func newStackTagSetCmd(stack *string) *cobra.Command {
   172  	return &cobra.Command{
   173  		Use:   "set <name> <value>",
   174  		Short: "Set a stack tag",
   175  		Args:  cmdutil.SpecificArgs([]string{"name", "value"}),
   176  		Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
   177  			name := args[0]
   178  			value := args[1]
   179  
   180  			opts := display.Options{
   181  				Color: cmdutil.GetGlobalColorization(),
   182  			}
   183  			s, err := requireStack(*stack, false, opts, true /*setCurrent*/)
   184  			if err != nil {
   185  				return err
   186  			}
   187  
   188  			ctx := commandContext()
   189  
   190  			tags, err := backend.GetStackTags(ctx, s)
   191  			if err != nil {
   192  				return err
   193  			}
   194  
   195  			if tags == nil {
   196  				tags = make(map[apitype.StackTagName]string)
   197  			}
   198  			tags[name] = value
   199  
   200  			return backend.UpdateStackTags(ctx, s, tags)
   201  		}),
   202  	}
   203  }