github.com/driusan/bug@v0.3.2-0.20190306121946-d7f4e7f33fea/bugapp/List.go (about)

     1  package bugapp
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/driusan/bug/bugs"
     6  	"io/ioutil"
     7  	"os"
     8  )
     9  
    10  func getBugName(b bugs.Bug, idx int) string {
    11  	if id := b.Identifier(); id != "" {
    12  		return fmt.Sprintf("Issue %s", id)
    13  	} else {
    14  		return fmt.Sprintf("Issue %d", idx+1)
    15  	}
    16  }
    17  func listTags(files []os.FileInfo, args ArgumentList) {
    18  	b := bugs.Bug{}
    19  	for idx, _ := range files {
    20  		b.LoadBug(bugs.Directory(bugs.GetIssuesDir() + bugs.Directory(files[idx].Name())))
    21  
    22  		for _, tag := range args {
    23  			if b.HasTag(bugs.Tag(tag)) {
    24  				fmt.Printf("%s: %s\n", getBugName(b, idx), b.Title("tags"))
    25  			}
    26  		}
    27  	}
    28  }
    29  func List(args ArgumentList) {
    30  	issues, _ := ioutil.ReadDir(string(bugs.GetIssuesDir()))
    31  
    32  	var wantTags bool = false
    33  	if args.HasArgument("--tags") {
    34  		wantTags = true
    35  	}
    36  
    37  	// No parameters, print a list of all bugs
    38  	if len(args) == 0 || (wantTags && len(args) == 1) {
    39  		//os.Stdout = stdout
    40  		for idx, issue := range issues {
    41  			if issue.IsDir() != true {
    42  				continue
    43  			}
    44  			var dir bugs.Directory = bugs.GetIssuesDir() + bugs.Directory(issue.Name())
    45  			b := bugs.Bug{Dir: dir}
    46  			name := getBugName(b, idx)
    47  			if wantTags == false {
    48  				fmt.Printf("%s: %s\n", name, b.Title(""))
    49  			} else {
    50  				fmt.Printf("%s: %s\n", name, b.Title("tags"))
    51  			}
    52  		}
    53  		return
    54  	}
    55  
    56  	// getAllTags() is defined in Tag.go
    57  	// Get a list of tags, so that when we encounter
    58  	// an error we can check if it's because the user
    59  	// provided a tagname instead of a BugID. If they
    60  	// did, then list bugs matching that tag instead
    61  	// of full descriptions
    62  	tags := getAllTags()
    63  	// There were parameters, so show the full description of each
    64  	// of those issues
    65  	for i, length := 0, len(args); i < length; i += 1 {
    66  		b, err := bugs.LoadBugByHeuristic(args[i])
    67  		if err != nil {
    68  			for _, tagname := range tags {
    69  				if tagname == args[i] {
    70  					listTags(issues, args)
    71  					return
    72  				}
    73  			}
    74  			fmt.Printf("%s\n", err.Error())
    75  			continue
    76  		}
    77  
    78  		b.ViewBug()
    79  		if i < length-1 {
    80  			fmt.Printf("\n--\n\n")
    81  		}
    82  	}
    83  	fmt.Printf("\n")
    84  }