Rework to match other .txt projects

This commit is contained in:
Brian Buller 2023-08-23 11:22:19 -05:00
parent d7904bcba2
commit c0e6b72ed3
2 changed files with 34 additions and 32 deletions

View File

@ -7,15 +7,15 @@ import (
) )
type Project struct { type Project struct {
Id int // Internal project id Id int `json:"id"` // Internal project id
Original string // Original raw project text Original string `json:"original"` // Original raw project text
Name string // The name of the project Name string `json:"name"` // The name of the project
Directory string // The directory part of the project text Directory string `json:"directory"` // The directory part of the project text
Notes string // The filename of the notes file, defaults to Directory+/+notes.md Notes string `json:"notes"` // The filename of the notes file, defaults to Directory+/+notes.md
Repo string // The directory to the git repository, defaults to Directory+/+Name Repo string `json:"repo"` // The directory to the git repository, defaults to Directory+/+Name
Contexts []string Contexts []string `json:"contexts"`
ProjectTags []string ProjectTags []string `json:"projectTags"`
AdditionalTags map[string]string AdditionalTags map[string]string `json:"additionalTags"`
} }
// ParseProject parses the input text string into a Project struct // ParseProject parses the input text string into a Project struct

View File

@ -9,18 +9,20 @@ import (
"strings" "strings"
) )
type ProjectList []*Project type ProjectList struct {
Projects []*Project `json:"projects"`
}
func NewProjectList() *ProjectList { func NewProjectList() *ProjectList {
return &ProjectList{} return &ProjectList{}
} }
func (projectlist *ProjectList) Size() int { func (projectlist *ProjectList) Size() int {
return len([]*Project(*projectlist)) return len(projectlist.Projects)
} }
func (projectlist *ProjectList) GetProjectSlice() []*Project { func (projectlist *ProjectList) GetProjectSlice() []*Project {
return []*Project(*projectlist) return projectlist.Projects
} }
func (projectlist *ProjectList) GetProjectsWithContext(context string) *ProjectList { func (projectlist *ProjectList) GetProjectsWithContext(context string) *ProjectList {
@ -39,9 +41,9 @@ func (projectlist *ProjectList) GetProjectsWithProjectTag(projectTag string) *Pr
// bool), and returns a new ProjectList. The original ProjectList is not modified. // bool), and returns a new ProjectList. The original ProjectList is not modified.
func (projectlist *ProjectList) Filter(predicate func(*Project) bool) *ProjectList { func (projectlist *ProjectList) Filter(predicate func(*Project) bool) *ProjectList {
var newList ProjectList var newList ProjectList
for _, t := range *projectlist { for _, t := range projectlist.Projects {
if predicate(t) { if predicate(t) {
newList = append(newList, t) newList.AddProject(t)
} }
} }
return &newList return &newList
@ -52,21 +54,21 @@ func (projectlist *ProjectList) AddProject(project *Project) {
// The new project will be id 1 // The new project will be id 1
project.Id = 1 project.Id = 1
// All other projects get their id incremented // All other projects get their id incremented
for _, p := range *projectlist { for _, p := range projectlist.Projects {
p.Id++ p.Id++
} }
// Now prepend the project to the slice // Now prepend the project to the slice
*projectlist = append(*projectlist, &Project{}) projectlist.Projects = append(projectlist.Projects, &Project{})
copy((*projectlist)[1:], (*projectlist)[0:]) copy(projectlist.Projects[1:], projectlist.Projects[0:])
(*projectlist)[0] = project projectlist.Projects[0] = project
} }
// GetProject returns the Project with the given project 'id' form the ProjectList. // GetProject returns the Project with the given project 'id' form the ProjectList.
// Returns an error if Project could not be found. // Returns an error if Project could not be found.
func (projectlist *ProjectList) GetProject(id int) (*Project, error) { func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
for i := range *projectlist { for i := range projectlist.Projects {
if ([]*Project(*projectlist))[i].Id == id { if projectlist.Projects[i].Id == id {
return ([]*Project(*projectlist))[i], nil return projectlist.Projects[i], nil
} }
} }
return nil, errors.New("project not found") return nil, errors.New("project not found")
@ -77,9 +79,9 @@ func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
func (projectlist *ProjectList) RemoveProjectById(id int) error { func (projectlist *ProjectList) RemoveProjectById(id int) error {
var newList ProjectList var newList ProjectList
found := false found := false
for _, p := range *projectlist { for _, p := range projectlist.Projects {
if p.Id != id { if p.Id != id {
newList = append(newList, p) newList.AddProject(p)
} else { } else {
found = true found = true
} }
@ -96,9 +98,9 @@ func (projectlist *ProjectList) RemoveProjectById(id int) error {
func (projectlist *ProjectList) RemoveProject(project Project) error { func (projectlist *ProjectList) RemoveProject(project Project) error {
var newList ProjectList var newList ProjectList
found := false found := false
for _, p := range *projectlist { for _, p := range projectlist.Projects {
if p.String() != project.String() { if p.String() != project.String() {
newList = append(newList, p) newList.AddProject(p)
} else { } else {
found = true found = true
} }
@ -129,7 +131,7 @@ func (projectlist *ProjectList) ArchiveProjectToFile(project Project, filename s
// LoadFromFile loads a ProjectList from *os.File. // LoadFromFile loads a ProjectList from *os.File.
// Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File. // Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File.
func (projectlist *ProjectList) LoadFromFile(file *os.File) error { func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
*projectlist = []*Project{} // Empty projectlist projectlist.Projects = []*Project{} // Empty projectlist
projectId := 1 projectId := 1
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
@ -143,7 +145,7 @@ func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
return err return err
} }
project.Id = projectId project.Id = projectId
*projectlist = append(*projectlist, project) projectlist.AddProject(project)
projectId++ projectId++
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
@ -178,19 +180,19 @@ func (projectlist *ProjectList) WriteToFilename(filename string) error {
// String returns a complete list of projects in project.txt format. // String returns a complete list of projects in project.txt format.
func (projectlist ProjectList) String() string { func (projectlist ProjectList) String() string {
var ret string var ret string
for _, project := range projectlist { for _, project := range projectlist.Projects {
ret += fmt.Sprintf("%s\n", project.String()) ret += fmt.Sprintf("%s\n", project.String())
} }
return ret return ret
} }
// LoadFromFile loads and returns a ProjectList from *os.File. // LoadFromFile loads and returns a ProjectList from *os.File.
func LoadFromFile(file *os.File) (ProjectList, error) { func LoadFromFile(file *os.File) (*ProjectList, error) {
projectlist := ProjectList{} projectlist := ProjectList{}
if err := projectlist.LoadFromFile(file); err != nil { if err := projectlist.LoadFromFile(file); err != nil {
return nil, err return nil, err
} }
return projectlist, nil return &projectlist, nil
} }
// WriteToFile writes a ProjectList to *os.File. // WriteToFile writes a ProjectList to *os.File.
@ -199,12 +201,12 @@ func WriteToFile(projectlist *ProjectList, file *os.File) error {
} }
// LoadFromFilename loads and returns a ProjectList from a file (most likely called "project.txt") // LoadFromFilename loads and returns a ProjectList from a file (most likely called "project.txt")
func LoadFromFilename(filename string) (ProjectList, error) { func LoadFromFilename(filename string) (*ProjectList, error) {
projectlist := ProjectList{} projectlist := ProjectList{}
if err := projectlist.LoadFromFilename(filename); err != nil { if err := projectlist.LoadFromFilename(filename); err != nil {
return nil, err return nil, err
} }
return projectlist, nil return &projectlist, nil
} }
// WriteToFilename write a ProjectList to the specified file (most likely called "project.txt") // WriteToFilename write a ProjectList to the specified file (most likely called "project.txt")