Skip to content

Commit

Permalink
Replace hard coded vacation days
Browse files Browse the repository at this point in the history
  • Loading branch information
roughy committed Apr 25, 2019
1 parent 5328873 commit 662ed5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ $PATH (or %PATH% on windows).
2. run `mite config activity."your activity name here".projectId="the project id"`
3. run `mite config activity."your activity name here".serviceId=<the service id"`
4. the activity names can be used in the `entries create` and `entries edit` sub-commands
6. Optional: set a project & service for your vacation tracking by:
6. Optional: configure your vacation tracking by:
1. retrieving the desired project & service id by executing `mite projects` and `mite services` respectively
2. configuring those id's as default by executing `mite config vacation.projectId="the project id"` and `mite config vacation.serviceId="the service id"`
3. configuring your available vacation days per year `mite config vacation.days="vacation days per year"`

# Usage

Expand Down
35 changes: 22 additions & 13 deletions cmd/vacation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
fullVacationDayDuration = 8
halfVacationDayDuration = fullVacationDayDuration / 2
entryListFilterAtThisYear = "this_year"
textProjectOrServiceNotConfigured = "please set both the vacation project AND service id (either via arguments or config)"
textProjectOrServiceNotConfigured = "please set your vacation configuration for project id, service id AND vacation days (either via arguments or config)"
)

var (
Expand Down Expand Up @@ -46,13 +46,13 @@ var vacationDetailCommand = &cobra.Command{
Use: "details",
Short: "show vacation statistics",
RunE: func(cmd *cobra.Command, args []string) error {
vacationActivity := application.Conf.GetVacation()
vacation := application.Conf.GetVacation()

if vacationActivity.ServiceId == "" {
if vacation.ServiceId == "" {
return errors.New(textProjectOrServiceNotConfigured)
}

serviceId, err := strconv.Atoi(vacationActivity.ServiceId)
serviceId, err := strconv.Atoi(vacation.ServiceId)
if err != nil {
return errors.New(textProjectOrServiceNotConfigured)
}
Expand All @@ -79,21 +79,30 @@ var vacationDetailCommand = &cobra.Command{
}
}

if vacation.Days == "" {
return errors.New(textProjectOrServiceNotConfigured)
}
vacationDays, err := strconv.ParseFloat(vacation.Days, 64)
if err != nil {
return errors.New(textProjectOrServiceNotConfigured)
}

var daysInYear = domain.MinutesAsDays(minutesInYear, fullVacationDayDuration)
var daysInPast = domain.MinutesAsDays(minutesInPast, fullVacationDayDuration)
var daysInFuture = domain.MinutesAsDays(minutesInFuture, fullVacationDayDuration)
var daysUnplanned = 28 - daysInYear // => user config, if not set explain how
var daysUnplanned = vacationDays - daysInYear // => user config, if not set explain how

if vacationDetailsverbose {
var daysInPast = domain.MinutesAsDays(minutesInPast, fullVacationDayDuration)
var daysInFuture = domain.MinutesAsDays(minutesInFuture, fullVacationDayDuration)

fmt.Printf("Vacation statistics of %d:\n"+
" - total: %d days\n"+
" - total: %s days\n"+
"---------------------\n"+
" - booked: %.1f days\n"+
" - taken: %.1f days\n"+
" - planned: %.1f days\n"+
" - unplanned: %.1f days\n",
domain.ThisYear(),
28,
vacation.Days,
daysInYear,
daysInPast,
daysInFuture,
Expand All @@ -115,18 +124,18 @@ var vacationCreateCommand = &cobra.Command{
Use: "create",
Short: "creates a vacation entry (WIP: currently this command creates a vacation day only for today)",
RunE: func(cmd *cobra.Command, args []string) error {
vacationActivity := application.Conf.GetVacation()
vacation := application.Conf.GetVacation()

if vacationActivity.ProjectId == "" || vacationActivity.ServiceId == "" {
if vacation.ProjectId == "" || vacation.ServiceId == "" {
return errors.New(textProjectOrServiceNotConfigured)
}

projectId, err := strconv.Atoi(vacationActivity.ProjectId)
projectId, err := strconv.Atoi(vacation.ProjectId)
if err != nil {
return errors.New(textProjectOrServiceNotConfigured)
}

serviceId, err := strconv.Atoi(vacationActivity.ServiceId)
serviceId, err := strconv.Atoi(vacation.ServiceId)
if err != nil {
return errors.New(textProjectOrServiceNotConfigured)
}
Expand Down
14 changes: 11 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Config interface {
GetApiKey() string
GetActivity(activity string) Activity
GetDisplayLocation() *time.Location
GetVacation() Activity
GetVacation() Vacation
Get(key string) string
Set(key string, value string)
PrintAll()
Expand All @@ -28,6 +28,12 @@ type Activity struct {
ServiceId string
}

type Vacation struct {
ProjectId string
ServiceId string
Days string
}

func NewConfig(fullPath string) Config {
createConfigFileIfNonExistent(fullPath)
viper.SetConfigFile(fullPath)
Expand Down Expand Up @@ -61,12 +67,14 @@ func (c *config) GetActivity(activity string) Activity {
}
}

func (c *config) GetVacation() Activity {
func (c *config) GetVacation() Vacation {
projectId := c.Get("vacation.projectId")
serviceId := c.Get("vacation.serviceId")
return Activity{
days := c.Get("vacation.days")
return Vacation{
ProjectId: projectId,
ServiceId: serviceId,
Days: days,
}
}

Expand Down

0 comments on commit 662ed5c

Please sign in to comment.