Skip to content

Commit

Permalink
entries: added configurable activity as a shortcut for projectId&serv…
Browse files Browse the repository at this point in the history
…iceId
  • Loading branch information
phiros committed Apr 5, 2019
1 parent 161e19f commit f9b5673
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ $PATH (or %PATH% on windows).
3. Optional: set a default project & service by:
1. retrieving the desired project & service id by executing `mite-go projects` and `mite-go services` respectively
2. configuring those id's as default by executing `mite-go config projectId=<the project id>` and `mite-go config serviceId=<the service id>`
4. Optional: mite-go allows you to define often used project & service combinations as activities. You can configure them by:
1. think of a good name for the activity
2. run `mite-go config activity.<your activity name here>.projectId=<the project id>`
3. run `mite-go 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

# Usage

Expand Down
50 changes: 38 additions & 12 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ var (
createNote string
createProjectId string
createServiceId string
createActivity string
editTimeEntryId string
editDate string
editDuration string
editNote string
editProjectId string
editServiceId string
editActivity string
deleteTimeEntryId string
)

Expand All @@ -47,6 +49,7 @@ func init() {
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)")
entriesCreateCommand.Flags().StringVarP(&createActivity, "activity", "a", "", "activity describing a specific project and service combination")
entriesCommand.AddCommand(entriesCreateCommand)
// edit
entriesEditCommand.Flags().StringVarP(&editDate, "date", "D", "", "day for which to edit entry (in YYYY-MM-DD format)")
Expand All @@ -55,6 +58,7 @@ func init() {
entriesEditCommand.Flags().StringVarP(&editTimeEntryId, "id", "i", "", "the time entry id to edit")
entriesEditCommand.Flags().StringVarP(&editProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
entriesEditCommand.Flags().StringVarP(&editServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
entriesEditCommand.Flags().StringVarP(&editActivity, "activity", "a", "", "activity describing a specific project and service combination")
entriesCommand.AddCommand(entriesEditCommand)
// delete
entriesDeleteCommand.Flags().StringVarP(&deleteTimeEntryId, "id", "i", "", "the time entry id to delete")
Expand Down Expand Up @@ -114,15 +118,9 @@ var entriesCreateCommand = &cobra.Command{
Use: "create",
Short: "creates a time entry",
RunE: func(cmd *cobra.Command, args []string) error {
if createProjectId == "" {
createProjectId = deps.conf.Get("projectId")
}

if createServiceId == "" {
createServiceId = deps.conf.Get("serviceId")
}
projectId, servicesId := servicesAndProjectId()

if createProjectId == "" || createServiceId == "" {
if projectId == "" || servicesId == "" {
return errors.New("please set both the project AND service id (either via arguments or config)")
}

Expand All @@ -135,8 +133,8 @@ var entriesCreateCommand = &cobra.Command{
Date: &cDate,
Duration: &createDuration,
Note: createNote,
ProjectId: createProjectId,
ServiceId: createServiceId,
ProjectId: projectId,
ServiceId: servicesId,
}

entry, err := deps.miteApi.CreateTimeEntry(&timeEntry)
Expand All @@ -149,6 +147,28 @@ var entriesCreateCommand = &cobra.Command{
},
}

func servicesAndProjectId() (projectId, servicesId string) {
if createProjectId == "" && createActivity != "" {
activity := deps.conf.GetActivity(createActivity)
createProjectId = activity.ProjectId
}

if createServiceId == "" && createActivity != "" {
activity := deps.conf.GetActivity(createActivity)
createServiceId = activity.ServiceId
}

if createProjectId == "" {
createProjectId = deps.conf.Get("projectId")
}

if createServiceId == "" {
createServiceId = deps.conf.Get("serviceId")
}

return projectId, servicesId
}

var entriesEditCommand = &cobra.Command{
Use: "edit",
Short: "edits a time entry",
Expand Down Expand Up @@ -188,11 +208,17 @@ var entriesEditCommand = &cobra.Command{
timeEntry.Note = editNote
}

if editProjectId != "" {
if editActivity != "" {
activity := deps.conf.GetActivity(editActivity)
timeEntry.ProjectId = activity.ProjectId
timeEntry.ServiceId = activity.ServiceId
}

if editProjectId != "" && timeEntry.ProjectId == "" {
timeEntry.ProjectId = editProjectId
}

if editServiceId != "" {
if editServiceId != "" && timeEntry.ProjectId == "" {
timeEntry.ServiceId = editServiceId
}

Expand Down
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Config interface {
GetApiUrl() string
GetApiKey() string
GetActivity(activity string) Activity
Get(key string) string
Set(key string, value string)
PrintAll()
Expand All @@ -21,6 +22,11 @@ type config struct {
fileFullPath string
}

type Activity struct {
ProjectId string
ServiceId string
}

func NewConfig(fileName, filePath, fileType string) Config {
viper.AddConfigPath("$HOME")
viper.SetConfigName(fileName)
Expand All @@ -37,6 +43,15 @@ func (c *config) GetApiKey() string {
return c.Get("api.key")
}

func (c *config) GetActivity(activity string) Activity {
projectId := c.Get(fmt.Sprintf("activity.%s.projectId", activity))
serviceId := c.Get(fmt.Sprintf("activity.%s.serviceId", activity))
return Activity{
ProjectId: projectId,
ServiceId: serviceId,
}
}

func (c *config) Get(key string) string {
err := viper.ReadInConfig()
if err != nil {
Expand Down

0 comments on commit f9b5673

Please sign in to comment.