separate logic into packages
This commit is contained in:
parent
71ab1c5e2c
commit
afd2b13047
5 changed files with 54 additions and 16 deletions
2
.github/workflows/debian.yml
vendored
2
.github/workflows/debian.yml
vendored
|
|
@ -17,7 +17,7 @@ jobs:
|
|||
uses: actions/checkout@v3
|
||||
|
||||
- name: Build
|
||||
run: go build -v .
|
||||
run: go build -o timetracker cmd/main.go
|
||||
|
||||
- name: Copy necessary files
|
||||
run: |
|
||||
|
|
|
|||
16
cmd/main.go
Normal file
16
cmd/main.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"ronnyfriedland/timetracker/v2/internal/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mode := flag.String("mode", "cli", "the application mode, available: cli")
|
||||
configPath := flag.String("configpath", "/var/lib/timetracker", "the config path")
|
||||
flag.Parse()
|
||||
|
||||
if *mode == "cli" {
|
||||
cli.Run(configPath)
|
||||
}
|
||||
}
|
||||
0
go.sum
Normal file
0
go.sum
Normal file
24
internal/cli/cli.go
Normal file
24
internal/cli/cli.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"log"
|
||||
"ronnyfriedland/timetracker/v2/internal/logic"
|
||||
)
|
||||
|
||||
const dateLayout = "02.01.2006"
|
||||
const timeLayout = "15:04:05"
|
||||
|
||||
func Run(configPath *string) {
|
||||
duration := logic.Execute(configPath)
|
||||
if duration.Complete {
|
||||
log.Printf("[%s] - Work duration: %2.2fh",
|
||||
duration.Date.Format(dateLayout),
|
||||
duration.Duration.Hours())
|
||||
|
||||
log.Printf("[%s] - Start: %s, End: %s",
|
||||
duration.Date.Format(dateLayout),
|
||||
duration.StartTime.Format(timeLayout),
|
||||
duration.EndTime.Format(timeLayout))
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +1,22 @@
|
|||
package main
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const dateLayout = "02.01.2006"
|
||||
const timeLayout = "15:04:05"
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("configpath", "/var/lib/timetracker", "the config path")
|
||||
flag.Parse()
|
||||
type Duration struct {
|
||||
Date time.Time
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Duration time.Duration
|
||||
Complete bool
|
||||
}
|
||||
|
||||
func Execute(configPath *string) Duration {
|
||||
var currentTime = time.Now()
|
||||
var modificationTime time.Time
|
||||
|
||||
|
|
@ -31,21 +32,18 @@ func main() {
|
|||
if math.Abs(float64(modificationTime.Day()-currentTime.Day())) == 0 {
|
||||
// same day, add to status
|
||||
addCurrentToStatus(timeTrackerStatusFile, currentTime)
|
||||
|
||||
return Duration{Complete: false}
|
||||
} else {
|
||||
duration, from, to := getDurationFromStatus(timeTrackerStatusFile)
|
||||
log.Printf("[%s] - Work duration: %2.2fh",
|
||||
modificationTime.Format(dateLayout),
|
||||
duration.Hours())
|
||||
log.Printf("[%s] - Start: %s, End: %s",
|
||||
modificationTime.Format(dateLayout),
|
||||
from.Format(timeLayout),
|
||||
to.Format(timeLayout))
|
||||
|
||||
// remove data from status
|
||||
cleanupStatus(timeTrackerStatusFile)
|
||||
|
||||
// new day, add first status
|
||||
addCurrentToStatus(timeTrackerStatusFile, currentTime)
|
||||
|
||||
return Duration{Date: modificationTime, StartTime: from, EndTime: to, Duration: duration, Complete: true}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue