github.com/grantbow/bug@v0.3.1/bugs/Find.go (about)

     1  package bugs
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  type BugNotFoundError string
    11  
    12  func (b BugNotFoundError) Error() string {
    13  	return string(b)
    14  }
    15  func FindBugsByTag(tags []string) []Bug {
    16  	issues, _ := ioutil.ReadDir(string(GetRootDir()) + "/issues")
    17  
    18  	var bugs []Bug
    19  	for idx, file := range issues {
    20  		if file.IsDir() == true {
    21  			bug := Bug{}
    22  			bug.LoadBug(Directory(GetRootDir() + "/issues/" + Directory(issues[idx].Name())))
    23  			for _, tag := range tags {
    24  				if bug.HasTag(Tag(tag)) {
    25  					bugs = append(bugs, bug)
    26  					break
    27  				}
    28  			}
    29  		}
    30  	}
    31  	return bugs
    32  	return []Bug{}
    33  }
    34  
    35  func LoadBugByHeuristic(id string) (*Bug, error) {
    36  	issues, _ := ioutil.ReadDir(string(GetRootDir()) + "/issues")
    37  
    38  	idx, err := strconv.Atoi(id)
    39  	if err == nil { // && idx > 0 && idx <= len(issues) {
    40  		return LoadBugByIndex(idx)
    41  	}
    42  
    43  	var candidate *Bug
    44  	for idx, file := range issues {
    45  		if file.IsDir() == true {
    46  			bug := Bug{}
    47  			bug.LoadBug(Directory(GetRootDir() + "/issues/" + Directory(issues[idx].Name())))
    48  			if bugid := bug.Identifier(); bugid == id {
    49  				return &bug, nil
    50  			} else if strings.Index(bugid, id) >= 0 {
    51  				candidate = &bug
    52  			}
    53  
    54  		}
    55  	}
    56  	if candidate != nil {
    57  		return candidate, nil
    58  	}
    59  	return nil, BugNotFoundError("Could not find bug " + id)
    60  }
    61  func LoadBugByStringIndex(i string) (*Bug, error) {
    62  	issues, _ := ioutil.ReadDir(string(GetRootDir()) + "/issues")
    63  
    64  	idx, err := strconv.Atoi(i)
    65  	if err != nil {
    66  		return nil, BugNotFoundError("Index not a number")
    67  	}
    68  	if idx < 1 || idx > len(issues) {
    69  		return nil, BugNotFoundError("Invalid Index")
    70  	}
    71  
    72  	b := Bug{}
    73  	directoryString := fmt.Sprintf("%s%s%s", GetRootDir(), "/issues/", issues[idx-1].Name())
    74  	b.LoadBug(Directory(directoryString))
    75  	return &b, nil
    76  }
    77  
    78  func LoadBugByIdentifier(id string) (*Bug, error) {
    79  	issues, _ := ioutil.ReadDir(string(GetRootDir()) + "/issues")
    80  
    81  	for idx, file := range issues {
    82  		if file.IsDir() == true {
    83  			bug := Bug{}
    84  			bug.LoadBug(Directory(GetRootDir() + "/issues/" + Directory(issues[idx].Name())))
    85  			if bug.Identifier() == id {
    86  				return &bug, nil
    87  			}
    88  		}
    89  	}
    90  	return nil, BugNotFoundError("No bug named " + id)
    91  }
    92  func LoadBugByIndex(idx int) (*Bug, error) {
    93  	issues, _ := ioutil.ReadDir(string(GetRootDir()) + "/issues")
    94  	if idx < 1 || idx > len(issues) {
    95  		return nil, BugNotFoundError("Invalid bug index")
    96  	}
    97  
    98  	b := Bug{}
    99  	directoryString := fmt.Sprintf("%s%s%s", GetRootDir(), "/issues/", issues[idx-1].Name())
   100  	b.LoadBug(Directory(directoryString))
   101  	return &b, nil
   102  }
   103  
   104  func GetAllBugs() []Bug {
   105  	issues, _ := ioutil.ReadDir(string(GetRootDir()) + "/issues")
   106  
   107  	var bugs []Bug
   108  	for idx, file := range issues {
   109  		if file.IsDir() == true {
   110  			bug := Bug{}
   111  			bug.LoadBug(Directory(GetRootDir() + "/issues/" + Directory(issues[idx].Name())))
   112  			bugs = append(bugs, bug)
   113  		}
   114  	}
   115  	return bugs
   116  }