| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- // Data and interface definitions for bugs
- package buggo
- import (
- "os"
- "path/filepath"
- "strings"
- )
- // Bug Definitions ------------------------------------------------------------
- // ----------------------------------------------------------------------------
- type Bug struct {
- Title string // The title of the bug in human readable format
- Description Field // The description of the bug
- Status Field // The status of the bug
- Tags VariadicField // A slice of VariadicFields
- Blockedby VariadicField // A slice of VariadicFields
- Path string // The path to the bug
- // machineTitle string // The machine parseable bug title
- }
- // Constrcutor for Bugs
- func (b Bug) New(title string, status Field, tags VariadicField, blockedby VariadicField, path string) (bug Bug, err error) { // TODO Implement
- return Bug{
- Title: title,
- Status: status,
- Tags: tags,
- Blockedby: blockedby,
- Path: path,
- }, err
- }
- // Constrcutor for Bugs that loads relevant data from disk
- func (b Bug) NewFromPath(path string) (bug Bug, err error) {
- // Required Fields
- description := &Field{Path: "/description"}
- status := &Field{Path: "/status"}
- requiredFields := []*Field{description, status}
- for _, field := range requiredFields {
- data, err := readPath(path + field.Path)
- if err != nil {
- return Bug{}, err
- }
- field.Data = data
- }
- // Variadic Fields
- tags := VariadicField{Path: "/tags"}
- blockers := VariadicField{Path: "/blockedby"}
- tags, _ = VariadicField.NewFromPath(tags, path)
- blockers, _ = VariadicField.NewFromPath(blockers, path)
- // we can ignore the errors, as loadVariadicField already gracefully handles
- //them.
- // title from path
- title := parsePathToHuman(path)
- return Bug{
- Title: title,
- Description: *description,
- Status: *status,
- Tags: tags,
- Blockedby: blockers,
- Path: path,
- }, err
- }
- // Field Definitions ----------------------------------------------------------
- // ----------------------------------------------------------------------------
- // A struct representing data that is tied to data on disk
- type Field struct {
- Path string
- Data string
- }
- // Constructor for Fields
- func (f Field) New(data string, path string) Field { return Field{Data: data, Path: path} }
- // VariadicField Definitions --------------------------------------------------
- // ----------------------------------------------------------------------------
- // VariadicFields hold lists of Field objects.
- type VariadicField struct {
- Path string // The associated path on disk of Fields represented by Variadic Field
- Fields []Field // The underlying slice of Field objects
- }
- // Constructor for VariadicFields
- func (vf VariadicField) New(fields []Field, path string) VariadicField {
- return VariadicField{Fields: fields, Path: path}
- }
- // Constructor for VariadicFields that loads relevant data from disk
- func (vf VariadicField) NewFromPath(pathOnDisk string) (v VariadicField, err error) {
- rootPath := pathOnDisk + "/" + vf.Path
- files, err := os.ReadDir(rootPath)
- if err != nil {
- return vf, err
- }
- for _, file := range files {
- data, err := readPath(rootPath + "/" + file.Name())
- if err != nil {
- return vf, err
- }
- if file.Name()[0:1] == "." {
- continue
- }
- vf.Fields = append(vf.Fields, Field{Data: data, Path: file.Name()})
- }
- return vf, err
- }
- // Util Definitions -----------------------------------------------------------
- // ----------------------------------------------------------------------------
- // Parses human readable strings as path strings
- func parseHumanToPath(humanReadable string) string {
- var out string
- out = strings.ReplaceAll(humanReadable, "-", "\\replace/")
- out = strings.ReplaceAll(out, " ", "-")
- out = strings.ReplaceAll(out, "\\replace/", "--")
- return out
- }
- // Parses machine parseable paths as human readable strings
- func parsePathToHuman(path string) string {
- _, last := filepath.Split(filepath.Clean(path))
- last = strings.ReplaceAll(last, "--", "\\replace/")
- last = strings.ReplaceAll(last, "-", " ")
- last = strings.ReplaceAll(last, "\\replace/", "-")
- return last
- }
|