diff options
author | Teddy Wing | 2017-03-12 13:39:15 +0100 |
---|---|---|
committer | Teddy Wing | 2017-03-12 13:39:15 +0100 |
commit | aee4a11e2d7263e6f2056ff0af8273e842e14ff4 (patch) | |
tree | 9831a181a40ff51a1fd8ef338229e15f6b1907e2 /main.go | |
parent | e6eee887588efff57d7e8b39894143880357f61c (diff) | |
download | timetasker-aee4a11e2d7263e6f2056ff0af8273e842e14ff4.tar.bz2 |
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.
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -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() { |