Add working recording of start end end point
parent
8858ba2132
commit
9da07e5e7e
69
main.go
69
main.go
|
@ -25,28 +25,79 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func start() {
|
type Record struct {
|
||||||
fmt.Print(time.Now().Format("2 Jan 2006 15:04:05"))
|
Start time.Time
|
||||||
fmt.Println(" Start record")
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() {
|
type Records struct {
|
||||||
|
Entries []Record
|
||||||
|
}
|
||||||
|
|
||||||
|
func start() Record {
|
||||||
|
fmt.Print(time.Now().Format("2 Jan 2006 15:04:05"))
|
||||||
|
fmt.Println(" Start record")
|
||||||
|
return Record{Start: time.Now()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop(entry *Record) {
|
||||||
fmt.Print(time.Now().Format("2 Jan 2006 15:04:05"))
|
fmt.Print(time.Now().Format("2 Jan 2006 15:04:05"))
|
||||||
fmt.Println(" Stop record")
|
fmt.Println(" Stop record")
|
||||||
|
entry.End = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
|
|
||||||
|
// representation of the data
|
||||||
|
data := Records {
|
||||||
|
Entries: []Record {},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
jsonFile, err := os.Open("test.json")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||||
|
|
||||||
|
// var records Records
|
||||||
|
var current *Record
|
||||||
|
json.Unmarshal(byteValue, &data)
|
||||||
|
|
||||||
|
for i := 0; i < len(data.Entries); i++ {
|
||||||
|
if data.Entries[i].End.IsZero() {
|
||||||
|
fmt.Println("Running Task: ")
|
||||||
|
fmt.Println(data.Entries[i].Start)
|
||||||
|
current = &data.Entries[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
case "start":
|
case "start":
|
||||||
start()
|
if current == nil {
|
||||||
case "stop":
|
data.Entries = append(data.Entries, start())
|
||||||
stop()
|
} else {
|
||||||
default:
|
fmt.Println("There is already a time tracking running")
|
||||||
fmt.Println("Missing argument")
|
|
||||||
}
|
}
|
||||||
|
case "stop":
|
||||||
|
fmt.Println(current)
|
||||||
|
stop(current)
|
||||||
|
fmt.Println(current)
|
||||||
|
default:
|
||||||
|
fmt.Println("Unknown argument")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
file, _ := json.MarshalIndent(data, "", " ")
|
||||||
|
_ = ioutil.WriteFile("test.json", file, 0644)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue