Skip to content

Commit

Permalink
Add option to format duration in seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
berlam committed Oct 4, 2019
1 parent 8889af5 commit 40246af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
9 changes: 5 additions & 4 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
showCmd.PersistentFlags().IntVar(&internal.Config.Month, internal.FlagMonth, int(time.Now().Month()), "specify the month to query effort for")
showCmd.PersistentFlags().BoolVarP(&internal.Config.Summarize, internal.FlagSummarize, "s", false, "summarize effort per day")
showCmd.PersistentFlags().BoolVar(&internal.Config.PrintEmptyLine, internal.FlagPrintEmptyLine, false, "print empty line for missing day during summary")
showCmd.PersistentFlags().BoolVar(&internal.Config.Seconds, internal.FlagSeconds, false, "display duration in seconds")

showBcsCmd.Flags().StringVar(&internal.Config.Report, internal.FlagReport, "", "specify the name of the report")
showBcsCmd.Flags().StringArrayVar(&internal.Config.Projects, internal.FlagProjects, nil, "specify the oid of the project")
Expand Down Expand Up @@ -70,7 +71,7 @@ var showBcsCmd = &cobra.Command{
internal.Config.Year,
time.Month(internal.Config.Month),
internal.Config.Report,
).Print(os.Stdout, false, internal.Config.Summarize, internal.Config.PrintEmptyLine)
).Print(os.Stdout, false, internal.Config.Summarize, internal.Config.PrintEmptyLine, internal.Config.Seconds)
} else {
bcs.GetBulkTimesheet(
pkg.NewHttpClient(),
Expand All @@ -80,7 +81,7 @@ var showBcsCmd = &cobra.Command{
time.Month(internal.Config.Month),
pkg.Project(internal.Config.Projects[0]),
internal.Config.Report,
).Print(os.Stdout, true, internal.Config.Summarize, internal.Config.PrintEmptyLine)
).Print(os.Stdout, true, internal.Config.Summarize, internal.Config.PrintEmptyLine, internal.Config.Seconds)
}
},
}
Expand All @@ -99,7 +100,7 @@ var showJiraCmd = &cobra.Command{
internal.Config.Year,
time.Month(internal.Config.Month),
internal.Projects(internal.Config.Projects),
).Print(os.Stdout, false, internal.Config.Summarize, internal.Config.PrintEmptyLine)
).Print(os.Stdout, false, internal.Config.Summarize, internal.Config.PrintEmptyLine, internal.Config.Seconds)
} else {
jira.GetBulkTimesheet(
pkg.NewHttpClient(),
Expand All @@ -109,7 +110,7 @@ var showJiraCmd = &cobra.Command{
time.Month(internal.Config.Month),
internal.Projects(internal.Config.Projects),
internal.Users(internal.Config.Users),
).Print(os.Stdout, true, internal.Config.Summarize, internal.Config.PrintEmptyLine)
).Print(os.Stdout, true, internal.Config.Summarize, internal.Config.PrintEmptyLine, internal.Config.Seconds)
}
},
}
2 changes: 2 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
FlagReport = "report"
FlagSummarize = "summarize"
FlagPrintEmptyLine = "printemptyline"
FlagSeconds = "seconds"
FlagAll = "all"
FlagForce = "force"
)
Expand Down Expand Up @@ -52,6 +53,7 @@ type Configuration struct {
Report string `mapstructure:"report"`
Summarize bool `mapstructure:"summarize"`
PrintEmptyLine bool `mapstructure:"printemptyline"`
Seconds bool `mapstructure:"seconds"`
}

var Config Configuration
Expand Down
19 changes: 13 additions & 6 deletions pkg/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkg
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"log"
"strings"
Expand Down Expand Up @@ -195,13 +196,13 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
for _, effort := range ts {
if currentUser != nil && currentUser.DisplayName != effort.User.DisplayName {
if currentDate.Day() > 1 {
emptyLinesForDaysBetween(csvw, spec, currentDate, currentDate.AddDate(0, 1, 1-currentDate.Day()), currentUser)
emptyLinesForDaysBetween(csvw, spec, currentDate, currentDate.AddDate(0, 1, 1-currentDate.Day()), currentUser, seconds)
}
currentUser = effort.User
currentDate = time.Date(effort.Date.Year(), time.Month(effort.Date.Month()), 1, 0, 0, 0, 0, time.UTC)
}
if printEmptyLine {
emptyLinesForDaysBetween(csvw, spec, currentDate, effort.Date, effort.User)
emptyLinesForDaysBetween(csvw, spec, currentDate, effort.Date, effort.User, seconds)
currentDate = effort.Date.AddDate(0, 0, 1)
}

Expand Down Expand Up @@ -236,20 +237,26 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
}
}
if printEmptyLine && currentDate.Day() > 1 {
emptyLinesForDaysBetween(csvw, spec, currentDate, currentDate.AddDate(0, 1, 1-currentDate.Day()), currentUser)
emptyLinesForDaysBetween(csvw, spec, currentDate, currentDate.AddDate(0, 1, 1-currentDate.Day()), currentUser, seconds)
}
csvw.Flush()
}

func emptyLinesForDaysBetween(csvw *csv.Writer, spec *CsvSpecification, from, to time.Time, user *User) {
func emptyLinesForDaysBetween(csvw *csv.Writer, spec *CsvSpecification, from, to time.Time, user *User, seconds bool) {
result := make([]string, spec.fields)
if spec.user.enabled {
if spec.user.enabled && user != nil {
result[spec.user.index] = user.DisplayName
}
if spec.duration.enabled {
var duration time.Duration
duration = 0
result[spec.duration.index] = duration.String()
var text string
if seconds {
text = fmt.Sprintf("%.0f", duration.Seconds())
} else {
text = duration.String()
}
result[spec.duration.index] = text
}
for i := int(to.Sub(from).Truncate(time.Hour*24).Hours() / 24); i > 0; i-- {
if spec.date.enabled {
Expand Down

0 comments on commit 40246af

Please sign in to comment.