From aee4a11e2d7263e6f2056ff0af8273e842e14ff4 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 12 Mar 2017 13:39:15 +0100 Subject: Parse timesheet files Expect a timesheet file as the last argument to the program. Parse the contents into `TimeEntry` objects. `TimeEntry`ies will then be able to be POSTed to Time Task to submit times. The time entries input file is a YAML document in this format: - client: A client project: A project module: A module task: A task work_type: type date: 2017-03-06 time: 7 billable: true description: It contains an array where each element is a time entry. Had a lot of trouble parsing the date into a `time.Time`. Finally realised that my first and biggest problem was somehow I was importing `yaml.v1` instead of `yaml.v2`, and thus my `UnmarshalYAML` function was never getting called. Wanted a way to get the time as a string and parse it myself into a time. At first tried using an `UnmarshalText` function: type Time time.Time func (t *Time) UnmarshalText(text []byte) error { parsed, err := time.Parse("2006-01-02", string(text)) if err == nil { *t = Time(parsed) } return err } But in order to do that I had to make a type alias to `time.Time`. Doing so was not ideal, because then I'd have to convert my `Time` into a `time.Time` any time I wanted to use it for real. Ended up going with a suggestion from here: https://mlafeldt.github.io/blog/decoding-yaml-in-go/ Creating an auxiliary struct in `UnmarshalYAML` to unmarshal the date into a string and then parse it myself as a date. I don't really like it because it's a lot of ceremony just to parse one type myself, but can't come up with a better solution right now so there you have it. --- main.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 3cc1a9e..5c6e5e4 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,14 @@ package main import ( + "fmt" "io/ioutil" "log" + "os" - "gopkg.in/yaml.v1" + "com.teddywing/timetasker/timetask" + + "gopkg.in/yaml.v2" ) type Config struct { @@ -41,6 +45,25 @@ var config Config func main() { loadConfig() + + if len(os.Args) == 1 { + fmt.Println("Not enough arguments") + os.Exit(1) + } + + file_path := os.Args[len(os.Args)-1] + file, err := ioutil.ReadFile(file_path) + if err != nil { + log.Println(err) + } + + time_entries := []timetask.TimeEntry{} + err = yaml.Unmarshal(file, &time_entries) + if err != nil { + log.Println(err) + } + + log.Printf("%+v", time_entries) } func loadConfig() { -- cgit v1.2.3