github.com/grantbow/bug@v0.3.1/bugapp/Tag.go (about)

     1  package bugapp
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/driusan/bug/bugs"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  func getAllTags() []string {
    11  	bugs := bugs.GetAllBugs()
    12  
    13  	// Put all the tags in a map, then iterate over
    14  	// the keys so that only unique tags are included
    15  	tagMap := make(map[string]int, 0)
    16  	for _, bug := range bugs {
    17  		for _, tag := range bug.Tags() {
    18  			tagMap[string(tag)] += 1
    19  		}
    20  	}
    21  
    22  	keys := make([]string, 0, len(tagMap))
    23  	for k := range tagMap {
    24  		keys = append(keys, k)
    25  	}
    26  	return keys
    27  }
    28  func Tag(Args ArgumentList) {
    29  	if len(Args) < 2 {
    30  		fmt.Printf("Usage: %s tag [--rm] BugID tagname [more tagnames]\n", os.Args[0])
    31  		fmt.Printf("\nBoth issue number and tagname to set are required.\n")
    32  		fmt.Printf("\nCurrently used tags in entire tree: %s\n", strings.Join(getAllTags(), ", "))
    33  		return
    34  	}
    35  	var removeTags bool = false
    36  	if Args[0] == "--rm" {
    37  		removeTags = true
    38  		Args = Args[1:]
    39  	}
    40  
    41  	b, err := bugs.LoadBugByHeuristic(Args[0])
    42  
    43  	if err != nil {
    44  		fmt.Printf("Could not load bug: %s\n", err.Error())
    45  		return
    46  	}
    47  	for _, tag := range Args[1:] {
    48  		if removeTags {
    49  			b.RemoveTag(bugs.Tag(tag))
    50  		} else {
    51  			b.TagBug(bugs.Tag(tag))
    52  		}
    53  	}
    54  
    55  }