diff --git a/main.go b/main.go index 5df7e0b..61530d5 100644 --- a/main.go +++ b/main.go @@ -25,28 +25,79 @@ import ( "fmt" "os" "time" + "encoding/json" + "io/ioutil" ) -func start() { - fmt.Print(time.Now().Format("2 Jan 2006 15:04:05")) - fmt.Println(" Start record") +type Record struct { + Start time.Time + 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.Println(" Stop record") + entry.End = time.Now() } func main() { args := os.Args[1:] - switch args[0] { - case "start": - start() - case "stop": - stop() - default: - fmt.Println("Missing argument") + // 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] { + case "start": + if current == nil { + data.Entries = append(data.Entries, start()) + } else { + fmt.Println("There is already a time tracking running") + } + 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) + }