110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
/*
|
|
Copyright © 2020 Georg Krause <mail@georg-krause.net>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
)
|
|
|
|
type Record struct {
|
|
Start time.Time
|
|
End time.Time
|
|
Tags []string
|
|
}
|
|
|
|
type Records struct {
|
|
Entries []Record
|
|
}
|
|
|
|
func start(tags []string) Record {
|
|
fmt.Print(time.Now().Format("2 Jan 2006 15:04:05"))
|
|
fmt.Println(" Start record")
|
|
var record Record
|
|
record.Start = time.Now()
|
|
for _, tag := range tags {
|
|
record.Tags = append(record.Tags, tag)
|
|
}
|
|
return record
|
|
}
|
|
|
|
func stop(entry *Record, tags []string) {
|
|
fmt.Print(time.Now().Format("2 Jan 2006 15:04:05"))
|
|
fmt.Println(" Stop record")
|
|
for _, tag := range tags {
|
|
entry.Tags = append(entry.Tags, tag)
|
|
}
|
|
entry.End = time.Now()
|
|
}
|
|
|
|
|
|
func main() {
|
|
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 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(args[1:]))
|
|
} else {
|
|
fmt.Println("There is already a time tracking running")
|
|
}
|
|
case "stop":
|
|
stop(current, args[1:])
|
|
default:
|
|
fmt.Println("Unknown argument")
|
|
}
|
|
|
|
}
|
|
file, _ := json.MarshalIndent(data, "", " ")
|
|
_ = ioutil.WriteFile("test.json", file, 0644)
|
|
|
|
}
|