commit bb9e6e1b8cba396ab309d94d586d8ddb00f9cc13
parent 9d1d1117b95059e7555861053ed109d01c6f8dc2
Author: Tomas Nemec <nemi@skaut.cz>
Date: Tue, 21 Feb 2023 22:28:59 +0100
refactor
Diffstat:
4 files changed, 43 insertions(+), 43 deletions(-)
diff --git a/command.go b/command.go
@@ -30,7 +30,7 @@ func (c *Command) nextArg() (string, error) {
return first, nil
}
-func (c Command) add() {
+func (c Command) Add() {
if len(c.args) != 3 {
fmt.Fprintln(os.Stderr, "add <group> <start> <stop>")
os.Exit(1)
@@ -68,7 +68,7 @@ func (c Command) add() {
c.repository.Save(group, entry)
}
-func (c Command) start() {
+func (c Command) Start() {
if len(c.args) < 1 || len(c.args) > 2 {
fmt.Fprintln(os.Stderr, "start <group> [<start>]")
os.Exit(1)
@@ -97,7 +97,7 @@ func (c Command) start() {
c.repository.Save(group, entry)
}
-func (c Command) stop() {
+func (c Command) Stop() {
if len(c.args) < 1 || len(c.args) > 2 {
fmt.Fprintln(os.Stderr, "stop <group> [<stop>]")
os.Exit(1)
@@ -133,7 +133,7 @@ func (c Command) stop() {
c.repository.Save(group, entry)
}
-func (c Command) ls() {
+func (c Command) Ls() {
if len(c.args) > 1 {
fmt.Fprintln(os.Stderr, "ls [<group>]")
os.Exit(1)
@@ -148,7 +148,7 @@ func (c Command) ls() {
formatEntries(c.repository, group, c.entryTimeContext)
}
-func (c Command) lsr() {
+func (c Command) Lsr() {
if len(c.args) > 1 {
fmt.Fprintln(os.Stderr, "lsr [<group>]")
os.Exit(1)
@@ -159,7 +159,7 @@ func (c Command) lsr() {
groupPath, _ = c.nextArg()
}
- groups, err := c.repository.ListGroupsDeep(groupPath)
+ groups, err := c.repository.ListGroups(groupPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@@ -170,7 +170,7 @@ func (c Command) lsr() {
}
}
-func (c Command) report() {
+func (c Command) Report() {
if len(c.args) < 1 || len(c.args) > 2 {
fmt.Fprintln(os.Stderr, "report <since> [<until>]")
os.Exit(1)
@@ -199,7 +199,7 @@ func (c Command) report() {
}
}
- groups, err := c.repository.ListGroupsDeep(groupPath)
+ groups, err := c.repository.ListGroups(groupPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@@ -208,7 +208,7 @@ func (c Command) report() {
var atLeastOneEntry bool
var totalDuration time.Duration
for _, group := range groups {
- entries, err := c.repository.List(group, c.entryTimeContext)
+ entries, err := c.repository.ListEntries(group, c.entryTimeContext)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@@ -234,7 +234,7 @@ func (c Command) report() {
}
func formatEntries(repository FSRepository, group Group, entryTime *TimeContext) {
- entries, err := repository.List(group, entryTime)
+ entries, err := repository.ListEntries(group, entryTime)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
diff --git a/main.go b/main.go
@@ -33,16 +33,16 @@ func main() {
repository := NewFSRepository(rootPath)
command := Command{repository, args, entryTime}
if cmd == "add" {
- command.add()
+ command.Add()
} else if cmd == "start" {
- command.start()
+ command.Start()
} else if cmd == "stop" {
- command.stop()
+ command.Stop()
} else if cmd == "ls" {
- command.ls()
+ command.Ls()
} else if cmd == "lsr" {
- command.lsr()
+ command.Lsr()
} else if cmd == "report" {
- command.report()
+ command.Report()
}
}
diff --git a/repository.go b/repository.go
@@ -96,28 +96,7 @@ func (repo FSRepository) Exists(group Group) bool {
return false
}
-func (repo FSRepository) ListGroupsDeep(groupPath string) ([]Group, error) {
- var groups []Group
- err := filepath.WalkDir(path.Join(repo.rootPath, groupPath), func(path string, d fs.DirEntry, err error) error {
- if d.IsDir() {
- if path == groupPath {
- return nil
- }
- groupPath := strings.TrimPrefix(path, repo.rootPath)
- groupPath = strings.TrimPrefix(groupPath, string(os.PathSeparator))
- group := NewGroup(groupPath)
- groups = append(groups, group)
- }
- return nil
- })
- if err != nil {
- return []Group{}, err
- }
-
- return groups, nil
-}
-
-func (repo FSRepository) List(group Group, entryTime *TimeContext) ([]Entry, error) {
+func (repo FSRepository) ListEntries(group Group, entryTime *TimeContext) ([]Entry, error) {
if !repo.Exists(group) {
return []Entry{}, errors.New("Group '" + group.Name + "' does not exist")
}
@@ -147,7 +126,28 @@ func (repo FSRepository) List(group Group, entryTime *TimeContext) ([]Entry, err
return entries, nil
}
-func (repo FSRepository) find(entryPath string, entryTime *TimeContext) (Entry, error) {
+func (repo FSRepository) ListGroups(groupPath string) ([]Group, error) {
+ var groups []Group
+ err := filepath.WalkDir(path.Join(repo.rootPath, groupPath), func(path string, d fs.DirEntry, err error) error {
+ if d.IsDir() {
+ if path == groupPath {
+ return nil
+ }
+ groupPath := strings.TrimPrefix(path, repo.rootPath)
+ groupPath = strings.TrimPrefix(groupPath, string(os.PathSeparator))
+ group := NewGroup(groupPath)
+ groups = append(groups, group)
+ }
+ return nil
+ })
+ if err != nil {
+ return []Group{}, err
+ }
+
+ return groups, nil
+}
+
+func (repo FSRepository) find(entryPath string, timeContext *TimeContext) (Entry, error) {
if _, err := os.Stat(entryPath); os.IsNotExist(err) {
return Entry{}, err
}
@@ -163,12 +163,12 @@ func (repo FSRepository) find(entryPath string, entryTime *TimeContext) (Entry,
lines := strings.Split(string(data), "\n")
if base == activeFile {
- start, _ := entryTime.ParseEntry(lines[0])
+ start, _ := timeContext.ParseEntry(lines[0])
timeRange, err := NewTimeRange(start, start.Add(time.Second))
return Entry{Completed: false, TimeRange: timeRange}, err
} else {
- start, _ := entryTime.ParseEntry(lines[0])
- stop, _ := entryTime.ParseEntry(lines[1])
+ start, _ := timeContext.ParseEntry(lines[0])
+ stop, _ := timeContext.ParseEntry(lines[1])
timeRange, err := NewTimeRange(start, stop)
return Entry{Completed: true, TimeRange: timeRange}, err
}
diff --git a/repository_test.go b/repository_test.go
@@ -38,7 +38,7 @@ func TestSave(t *testing.T) {
}
}
- list, _ := repository.List(group, timeContext)
+ list, _ := repository.ListEntries(group, timeContext)
if len(list) != 3 {
t.Error("Expected 3 entries")