Skip to content

Commit

Permalink
entries: create command implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
1 parent 3389da3 commit a3ce15d
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
)

var (
listTo string
listFrom string
listOrder string
createDate string
createDuration time.Duration
createNote string
listTo string
listFrom string
listOrder string
createDate string
createDuration time.Duration
createNote string
createProjectId string
createServiceId string
)

func init() {
Expand All @@ -35,6 +37,8 @@ func init() {
entriesCreateCommand.Flags().StringVarP(&createDate, "date", "D", now.Format("2006-01-02"), "day for which to create entry (in YYYY-MM-DD format)")
entriesCreateCommand.Flags().DurationVarP(&createDuration, "duration", "d", defaultDuration, "duration of entry (format examples: '1h15m' or '300m' or '6h')")
entriesCreateCommand.Flags().StringVarP(&createNote, "note", "n", "", "a note describing what was worked on")
entriesCreateCommand.Flags().StringVarP(&createProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
entriesCreateCommand.Flags().StringVarP(&createServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
entriesCommand.AddCommand(entriesCreateCommand)
rootCmd.AddCommand(entriesCommand)
}
Expand Down Expand Up @@ -72,22 +76,51 @@ var entriesListCommand = &cobra.Command{
return
}

t := tabby.New()
t.AddHeader("id", "notes", "date", "time", "project,service")
for _, entry := range entries {
trimmedNotes := strings.Replace(entry.Note, "\r\n", ",", -1)
shortenedNotes := fmt.Sprintf("%.50s", trimmedNotes)
shortenedProjectService := fmt.Sprintf("%.50s", entry.ProjectName+","+entry.ServiceName)
t.AddLine(entry.Id, shortenedNotes, entry.Date, entry.Duration.String(), shortenedProjectService)
}
t.Print()
printEntries(entries)
},
}

func printEntries(entries []*mite.TimeEntry) {
t := tabby.New()
t.AddHeader("id", "notes", "date", "time", "project,service")
for _, entry := range entries {
trimmedNotes := strings.Replace(entry.Note, "\r\n", ",", -1)
shortenedNotes := fmt.Sprintf("%.50s", trimmedNotes)
shortenedProjectService := fmt.Sprintf("%.50s", entry.ProjectName+","+entry.ServiceName)
t.AddLine(entry.Id, shortenedNotes, entry.Date, entry.Duration.String(), shortenedProjectService)
}
t.Print()
}

var entriesCreateCommand = &cobra.Command{
Use: "create",
Short: "create time entries",
Run: func(cmd *cobra.Command, args []string) {
if createProjectId == "" || createServiceId == "" {
_, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id")
return
}

cDate, err := time.Parse("2006-01-02", createDate)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return
}

timeEntry := mite.TimeEntryCommand{
Date: &cDate,
Duration: &createDuration,
Note: createNote,
ProjectId: createProjectId,
ServiceId: createServiceId,
}

entry, err := deps.miteApi.CreateTimeEntry(&timeEntry)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return
}

printEntries([]*mite.TimeEntry{entry})
},
}

0 comments on commit a3ce15d

Please sign in to comment.