// This package defines several top level IO functions for interacting with // issues and collections of issues. // // Examples: // // To check if a folder is spec compliant: // ... // if issues.IsIssue(path_to_folder) { // process_issue(path_to_folder) // } // // Additionally, to check if a folder is a collection of spec compliant issues: // ... // if issues.IsIssueCollection(path_to_folder) { // process_collection(path_to_folder) // } // // To write an [Issue] object to disk: // ... // // NOT IMPLEMENTED // // To remove an [Issue] object from disk: // ... // // NOT IMPLEMENTED package issues import ( "errors" "os" "path/filepath" ) // converts file from []string to string, reports errors func readPath(path string) (output string, err error) { content, err := os.ReadFile(path) if err != nil { return "", err } for _, line := range content { output = output + string(line) } return output, nil } // Reports true when the specified path conforms to the minimum Poorman spec func IsIssue(path string) bool { files, err := os.ReadDir(path) if err != nil { return false } var specFiles []bool for _, file := range files { if file.Name() == "description" || file.Name() == "status" { specFiles = append(specFiles, true) } } if len(specFiles) >= 2 { return true } return false } // Reports true when the specified path is a directory of Issues func IsIssueCollection(path string) bool { if IsIssue(path) { return false } files, err := os.ReadDir(path) if err != nil { return false } var isIssue []bool for _, file := range files { if IsIssue(path + "/" + file.Name()) { isIssue = append(isIssue, true) } } if len(isIssue) > 0 { return true } return false } // Writes a issue to disk func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) { if IsIssue(issue.Path) && !ignoreExisting { return false, errors.New("path exists") } // make base directory (effectively, the title) err = os.Mkdir(issue.Path, 0755) if err != nil { return false, err } // Write Fields f := []Field{issue.Description, issue.Status} for _, field := range f { if len(field.Path) > 0 { // skip empty paths path := filepath.Join(issue.Path, field.Path) err = os.WriteFile(path, []byte(field.Data), 0755) if err != nil { return false, err // test by overwrite path } } } // Write Tags if len(issue.Tags.Path) > 0 { // skip tags with no path init err = os.Mkdir(filepath.Join(issue.Path, issue.Tags.Path), 0755) if err != nil { return false, err // test by overwrite path } for _, field := range issue.Tags.Fields { if len(field.Path) > 0 { // skip empty paths path := filepath.Join(issue.Path, issue.Tags.Path, field.Path) err = os.WriteFile(path, []byte(field.Data), 0755) if err != nil { return false, err // test by overwrite path } } } } // Write Blockedby if len(issue.Tags.Path) > 0 { // skip blockedby with no path init err = os.Mkdir(filepath.Join(issue.Path, issue.Blockedby.Path), 0755) if err != nil { return false, err // test by overwrite path } for _, field := range issue.Blockedby.Fields { if len(field.Path) > 0 { // skip empty paths path := filepath.Join(issue.Path, issue.Blockedby.Path, field.Path) err = os.WriteFile(path, []byte(field.Data), 0755) if err != nil { return false, err // test by overwrite path } } } } return true, nil } // Removes a issue from disk func DeleteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement