github.com/komosa/bug@v0.3.1/bugs/Directory.go (about)

     1  package bugs
     2  
     3  import (
     4  	"os"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  func GetRootDir() Directory {
    10  	dir := os.Getenv("PMIT")
    11  	if dir != "" {
    12  		return Directory(dir)
    13  	}
    14  
    15  	wd, _ := os.Getwd()
    16  
    17  	if dirinfo, err := os.Stat(wd + "/issues"); err == nil && dirinfo.IsDir() {
    18  		return Directory(wd)
    19  	}
    20  
    21  	// There's no environment variable and no issues
    22  	// directory, so walk up the tree until we find one
    23  	pieces := strings.Split(wd, "/")
    24  
    25  	for i := len(pieces); i > 0; i -= 1 {
    26  		dir := strings.Join(pieces[0:i], "/")
    27  		if dirinfo, err := os.Stat(dir + "/issues"); err == nil && dirinfo.IsDir() {
    28  			return Directory(dir)
    29  		}
    30  	}
    31  	return ""
    32  }
    33  
    34  func GetIssuesDir() Directory {
    35  	return GetRootDir() + "/issues/"
    36  }
    37  
    38  type Directory string
    39  
    40  func (d Directory) GetShortName() Directory {
    41  	pieces := strings.Split(string(d), "/")
    42  	return Directory(pieces[len(pieces)-1])
    43  }
    44  
    45  func (d Directory) ToTitle() string {
    46  	multidash := regexp.MustCompile("([_]*)-([-_]*)")
    47  	dashReplacement := strings.Replace(string(d), " ", "/", -1)
    48  	return multidash.ReplaceAllStringFunc(dashReplacement, func(match string) string {
    49  		if match == "-" {
    50  			return " "
    51  		}
    52  		if strings.Count(match, "_") == 0 {
    53  			return match[1:]
    54  		}
    55  		return strings.Replace(match, "_", " ", -1)
    56  	})
    57  }