github.com/naphatkrit/deis@v1.12.3/client/cmd/tags.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/deis/deis/pkg/prettyprint"
     8  
     9  	"github.com/deis/deis/client/controller/api"
    10  	"github.com/deis/deis/client/controller/models/config"
    11  )
    12  
    13  // TagsList lists an app's tags.
    14  func TagsList(appID string) error {
    15  	c, appID, err := load(appID)
    16  
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	config, err := config.List(c, appID)
    22  
    23  	fmt.Printf("=== %s Tags\n", appID)
    24  
    25  	tagMap := make(map[string]string)
    26  
    27  	for key, value := range config.Tags {
    28  		tagMap[key] = fmt.Sprintf("%v", value)
    29  	}
    30  
    31  	fmt.Print(prettyprint.PrettyTabs(tagMap, 5))
    32  
    33  	return nil
    34  }
    35  
    36  // TagsSet sets an app's tags.
    37  func TagsSet(appID string, tags []string) error {
    38  	c, appID, err := load(appID)
    39  
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	tagsMap := parseTags(tags)
    45  
    46  	fmt.Print("Applying tags... ")
    47  
    48  	quit := progress()
    49  	configObj := api.Config{}
    50  	configObj.Tags = tagsMap
    51  
    52  	_, err = config.Set(c, appID, configObj)
    53  
    54  	quit <- true
    55  	<-quit
    56  
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	fmt.Print("done\n\n")
    62  
    63  	return TagsList(appID)
    64  }
    65  
    66  // TagsUnset removes an app's tags.
    67  func TagsUnset(appID string, tags []string) error {
    68  	c, appID, err := load(appID)
    69  
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	fmt.Print("Applying tags... ")
    75  
    76  	quit := progress()
    77  
    78  	configObj := api.Config{}
    79  
    80  	tagsMap := make(map[string]interface{})
    81  
    82  	for _, tag := range tags {
    83  		tagsMap[tag] = nil
    84  	}
    85  
    86  	configObj.Tags = tagsMap
    87  
    88  	_, err = config.Set(c, appID, configObj)
    89  
    90  	quit <- true
    91  	<-quit
    92  
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	fmt.Print("done\n\n")
    98  
    99  	return TagsList(appID)
   100  }
   101  
   102  func parseTags(tags []string) map[string]interface{} {
   103  	tagMap := make(map[string]interface{})
   104  
   105  	for _, tag := range tags {
   106  		key, value, err := parseTag(tag)
   107  
   108  		if err != nil {
   109  			fmt.Println(err)
   110  			continue
   111  		}
   112  
   113  		tagMap[key] = value
   114  	}
   115  
   116  	return tagMap
   117  }
   118  
   119  func parseTag(tag string) (string, string, error) {
   120  	parts := strings.Split(tag, "=")
   121  
   122  	if len(parts) != 2 {
   123  		return "", "", fmt.Errorf(`%s is invalid, Must be in format key=value
   124  Examples: rack=1 evironment=production`, tag)
   125  	}
   126  
   127  	return parts[0], parts[1], nil
   128  }