go-projecttxt/projectlist.go

208 lines
6.1 KiB
Go
Raw Normal View History

2022-06-07 18:50:33 +00:00
package projecttxt
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)
2023-08-23 16:22:19 +00:00
type ProjectList struct {
Projects []*Project `json:"projects"`
}
2022-06-07 18:50:33 +00:00
2023-08-23 16:29:59 +00:00
func NewProjectList() *ProjectList { return &ProjectList{Projects: []*Project{}} }
func (projectlist *ProjectList) Size() int { return len(projectlist.Projects) }
func (projectlist *ProjectList) GetProjectSlice() []*Project { return projectlist.Projects }
2022-06-07 18:50:33 +00:00
func (projectlist *ProjectList) GetProjectsWithContext(context string) *ProjectList {
return projectlist.Filter(func(p *Project) bool {
return p.HasContext(context)
})
}
func (projectlist *ProjectList) GetProjectsWithProjectTag(projectTag string) *ProjectList {
return projectlist.Filter(func(p *Project) bool {
return p.HasProjectTag(projectTag)
})
}
// Filter filters the current TimerList for the given predicate (a function that takes a timer as input and returns a
// bool), and returns a new ProjectList. The original ProjectList is not modified.
func (projectlist *ProjectList) Filter(predicate func(*Project) bool) *ProjectList {
var newList ProjectList
2023-08-23 16:22:19 +00:00
for _, t := range projectlist.Projects {
2022-06-07 18:50:33 +00:00
if predicate(t) {
2023-08-23 16:22:19 +00:00
newList.AddProject(t)
2022-06-07 18:50:33 +00:00
}
}
return &newList
}
// AddProject prepends a Project to the current ProjectList and takes care to set the ProjectId correctly
func (projectlist *ProjectList) AddProject(project *Project) {
// The new project will be id 1
project.Id = 1
// All other projects get their id incremented
2023-08-23 16:22:19 +00:00
for _, p := range projectlist.Projects {
2022-06-07 18:50:33 +00:00
p.Id++
}
// Now prepend the project to the slice
2023-08-23 16:22:19 +00:00
projectlist.Projects = append(projectlist.Projects, &Project{})
copy(projectlist.Projects[1:], projectlist.Projects[0:])
projectlist.Projects[0] = project
2022-06-07 18:50:33 +00:00
}
// GetProject returns the Project with the given project 'id' form the ProjectList.
// Returns an error if Project could not be found.
func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
2023-08-23 16:22:19 +00:00
for i := range projectlist.Projects {
if projectlist.Projects[i].Id == id {
return projectlist.Projects[i], nil
2022-06-07 18:50:33 +00:00
}
}
return nil, errors.New("project not found")
}
// RemoveProjectById removes any Project with given Project 'id' from the ProjectList.
// Returns an error if no Project was removed.
func (projectlist *ProjectList) RemoveProjectById(id int) error {
var newList ProjectList
found := false
2023-08-23 16:22:19 +00:00
for _, p := range projectlist.Projects {
2022-06-07 18:50:33 +00:00
if p.Id != id {
2023-08-23 16:22:19 +00:00
newList.AddProject(p)
2022-06-07 18:50:33 +00:00
} else {
found = true
}
}
if !found {
return errors.New("project not found")
}
*projectlist = newList
return nil
}
// RemoveProject removes any Project from the ProjectList with the same String representation as the given Project.
// Returns an error if no Project was removed.
func (projectlist *ProjectList) RemoveProject(project Project) error {
var newList ProjectList
found := false
2023-08-23 16:22:19 +00:00
for _, p := range projectlist.Projects {
2022-06-07 18:50:33 +00:00
if p.String() != project.String() {
2023-08-23 16:22:19 +00:00
newList.AddProject(p)
2022-06-07 18:50:33 +00:00
} else {
found = true
}
}
if !found {
return errors.New("project not found")
}
*projectlist = newList
return nil
}
// ArchiveProjctToFile removes the project from the active list and concatenates it to
// the passed in filename
// Return an err if any part of that fails
func (projectlist *ProjectList) ArchiveProjectToFile(project Project, filename string) error {
if err := projectlist.RemoveProject(project); err != nil {
return err
}
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(project.String() + "\n")
return err
}
// 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.
func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
2023-08-23 16:22:19 +00:00
projectlist.Projects = []*Project{} // Empty projectlist
2022-06-07 18:50:33 +00:00
projectId := 1
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := strings.Trim(scanner.Text(), "\t\n\r") // Read Line
// Ignore blank lines
if text == "" {
continue
}
project, err := ParseProject(text)
if err != nil {
return err
}
project.Id = projectId
2023-08-23 16:22:19 +00:00
projectlist.AddProject(project)
2022-06-07 18:50:33 +00:00
projectId++
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
// WriteToFile writes a ProjectList to *os.File
func (projectlist *ProjectList) WriteToFile(file *os.File) error {
writer := bufio.NewWriter(file)
_, err := writer.WriteString(projectlist.String())
writer.Flush()
return err
}
// LoadFromFilename loads a ProjectList from the filename.
func (projectlist *ProjectList) LoadFromFilename(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
return projectlist.LoadFromFile(file)
}
// WriteToFilename writes a ProjectList to the specified file (most likely called "project.txt").
func (projectlist *ProjectList) WriteToFilename(filename string) error {
return ioutil.WriteFile(filename, []byte(projectlist.String()), 0640)
}
// String returns a complete list of projects in project.txt format.
func (projectlist ProjectList) String() string {
var ret string
2023-08-23 16:22:19 +00:00
for _, project := range projectlist.Projects {
2022-06-07 18:50:33 +00:00
ret += fmt.Sprintf("%s\n", project.String())
}
return ret
}
// LoadFromFile loads and returns a ProjectList from *os.File.
2023-08-23 16:22:19 +00:00
func LoadFromFile(file *os.File) (*ProjectList, error) {
2022-06-07 18:50:33 +00:00
projectlist := ProjectList{}
if err := projectlist.LoadFromFile(file); err != nil {
return nil, err
}
2023-08-23 16:22:19 +00:00
return &projectlist, nil
2022-06-07 18:50:33 +00:00
}
// WriteToFile writes a ProjectList to *os.File.
func WriteToFile(projectlist *ProjectList, file *os.File) error {
return projectlist.WriteToFile(file)
}
// LoadFromFilename loads and returns a ProjectList from a file (most likely called "project.txt")
2023-08-23 16:22:19 +00:00
func LoadFromFilename(filename string) (*ProjectList, error) {
2022-06-07 18:50:33 +00:00
projectlist := ProjectList{}
if err := projectlist.LoadFromFilename(filename); err != nil {
return nil, err
}
2023-08-23 16:22:19 +00:00
return &projectlist, nil
2022-06-07 18:50:33 +00:00
}
// WriteToFilename write a ProjectList to the specified file (most likely called "project.txt")
func WriteToFilename(projectlist *ProjectList, filename string) error {
return projectlist.WriteToFilename(filename)
}