gott/main.go

157 lines
4.0 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"
"path/filepath"
)
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) {
if entry == nil {
fmt.Println("No active tracking")
return
}
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 list(records []Record) {
for _, r := range records {
fmt.Print(r.Start.Format("02.01.2006") + " ")
fmt.Print(r.Start.Format("15:04:05") + " - " + r.End.Format("15:04:05") + " ")
fmt.Print(r.End.Sub(r.Start))
fmt.Println()
for _, t := range r.Tags {
fmt.Println("\t" + t)
}
}
}
func help() {
fmt.Println("go time tracker v0.1\n")
fmt.Println("Syntax: gott [start|stop|list|help]\n")
fmt.Println("Options:")
fmt.Println("\tstart [tag(s)]\tStart new time interval")
fmt.Println("\tstop [tag(s)]\tStop current time tracking interval")
fmt.Println("\tlist\t\tList all entries")
fmt.Println("\thelp, h\t show this help")
fmt.Println("\nThis Software is freely developed at\nhttps://git.soundship.de/gcrkrause/gott unter the MIT License")
fmt.Println("Copyright © 2020 Georg Krause <mail@georg-krause.net>\nSee LICENSE for the full license")
}
func main() {
datafile := os.Getenv("HOME") + "/.gott/data.json"
args := os.Args[1:]
dir := filepath.Dir(datafile)
if _, errDir := os.Stat(dir); os.IsNotExist(errDir) {
errCreateDir := os.Mkdir(dir, 0777)
if errCreateDir == nil {
fmt.Println("Created data directory")
} else {
fmt.Println("Cannot create data directory. Please create it on your own")
return
}
}
// representation of the data
data := Records {
Entries: []Record {},
}
jsonFile, err := os.OpenFile(datafile, os.O_RDWR|os.O_CREATE, 0755)
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:])
case "list":
list(data.Entries)
case "help":
help()
case "h":
help()
default:
fmt.Println("Unknown argument")
}
}
file, _ := json.MarshalIndent(data, "", " ")
_ = ioutil.WriteFile(datafile, file, 0644)
}