Skip to content

Commit f9b5673

Browse files
committed
entries: added configurable activity as a shortcut for projectId&serviceId
1 parent 161e19f commit f9b5673

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ $PATH (or %PATH% on windows).
1414
3. Optional: set a default project & service by:
1515
1. retrieving the desired project & service id by executing `mite-go projects` and `mite-go services` respectively
1616
2. configuring those id's as default by executing `mite-go config projectId=<the project id>` and `mite-go config serviceId=<the service id>`
17+
4. Optional: mite-go allows you to define often used project & service combinations as activities. You can configure them by:
18+
1. think of a good name for the activity
19+
2. run `mite-go config activity.<your activity name here>.projectId=<the project id>`
20+
3. run `mite-go config activity.<your activity name here>.serviceId=<the service id>`
21+
4. the activity names can be used in the `entries create` and `entries edit` sub-commands
1722

1823
# Usage
1924

cmd/entries.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ var (
2020
createNote string
2121
createProjectId string
2222
createServiceId string
23+
createActivity string
2324
editTimeEntryId string
2425
editDate string
2526
editDuration string
2627
editNote string
2728
editProjectId string
2829
editServiceId string
30+
editActivity string
2931
deleteTimeEntryId string
3032
)
3133

@@ -47,6 +49,7 @@ func init() {
4749
entriesCreateCommand.Flags().StringVarP(&createNote, "note", "n", "", "a note describing what was worked on")
4850
entriesCreateCommand.Flags().StringVarP(&createProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
4951
entriesCreateCommand.Flags().StringVarP(&createServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
52+
entriesCreateCommand.Flags().StringVarP(&createActivity, "activity", "a", "", "activity describing a specific project and service combination")
5053
entriesCommand.AddCommand(entriesCreateCommand)
5154
// edit
5255
entriesEditCommand.Flags().StringVarP(&editDate, "date", "D", "", "day for which to edit entry (in YYYY-MM-DD format)")
@@ -55,6 +58,7 @@ func init() {
5558
entriesEditCommand.Flags().StringVarP(&editTimeEntryId, "id", "i", "", "the time entry id to edit")
5659
entriesEditCommand.Flags().StringVarP(&editProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
5760
entriesEditCommand.Flags().StringVarP(&editServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
61+
entriesEditCommand.Flags().StringVarP(&editActivity, "activity", "a", "", "activity describing a specific project and service combination")
5862
entriesCommand.AddCommand(entriesEditCommand)
5963
// delete
6064
entriesDeleteCommand.Flags().StringVarP(&deleteTimeEntryId, "id", "i", "", "the time entry id to delete")
@@ -114,15 +118,9 @@ var entriesCreateCommand = &cobra.Command{
114118
Use: "create",
115119
Short: "creates a time entry",
116120
RunE: func(cmd *cobra.Command, args []string) error {
117-
if createProjectId == "" {
118-
createProjectId = deps.conf.Get("projectId")
119-
}
120-
121-
if createServiceId == "" {
122-
createServiceId = deps.conf.Get("serviceId")
123-
}
121+
projectId, servicesId := servicesAndProjectId()
124122

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

@@ -135,8 +133,8 @@ var entriesCreateCommand = &cobra.Command{
135133
Date: &cDate,
136134
Duration: &createDuration,
137135
Note: createNote,
138-
ProjectId: createProjectId,
139-
ServiceId: createServiceId,
136+
ProjectId: projectId,
137+
ServiceId: servicesId,
140138
}
141139

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

150+
func servicesAndProjectId() (projectId, servicesId string) {
151+
if createProjectId == "" && createActivity != "" {
152+
activity := deps.conf.GetActivity(createActivity)
153+
createProjectId = activity.ProjectId
154+
}
155+
156+
if createServiceId == "" && createActivity != "" {
157+
activity := deps.conf.GetActivity(createActivity)
158+
createServiceId = activity.ServiceId
159+
}
160+
161+
if createProjectId == "" {
162+
createProjectId = deps.conf.Get("projectId")
163+
}
164+
165+
if createServiceId == "" {
166+
createServiceId = deps.conf.Get("serviceId")
167+
}
168+
169+
return projectId, servicesId
170+
}
171+
152172
var entriesEditCommand = &cobra.Command{
153173
Use: "edit",
154174
Short: "edits a time entry",
@@ -188,11 +208,17 @@ var entriesEditCommand = &cobra.Command{
188208
timeEntry.Note = editNote
189209
}
190210

191-
if editProjectId != "" {
211+
if editActivity != "" {
212+
activity := deps.conf.GetActivity(editActivity)
213+
timeEntry.ProjectId = activity.ProjectId
214+
timeEntry.ServiceId = activity.ServiceId
215+
}
216+
217+
if editProjectId != "" && timeEntry.ProjectId == "" {
192218
timeEntry.ProjectId = editProjectId
193219
}
194220

195-
if editServiceId != "" {
221+
if editServiceId != "" && timeEntry.ProjectId == "" {
196222
timeEntry.ServiceId = editServiceId
197223
}
198224

config/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type Config interface {
1010
GetApiUrl() string
1111
GetApiKey() string
12+
GetActivity(activity string) Activity
1213
Get(key string) string
1314
Set(key string, value string)
1415
PrintAll()
@@ -21,6 +22,11 @@ type config struct {
2122
fileFullPath string
2223
}
2324

25+
type Activity struct {
26+
ProjectId string
27+
ServiceId string
28+
}
29+
2430
func NewConfig(fileName, filePath, fileType string) Config {
2531
viper.AddConfigPath("$HOME")
2632
viper.SetConfigName(fileName)
@@ -37,6 +43,15 @@ func (c *config) GetApiKey() string {
3743
return c.Get("api.key")
3844
}
3945

46+
func (c *config) GetActivity(activity string) Activity {
47+
projectId := c.Get(fmt.Sprintf("activity.%s.projectId", activity))
48+
serviceId := c.Get(fmt.Sprintf("activity.%s.serviceId", activity))
49+
return Activity{
50+
ProjectId: projectId,
51+
ServiceId: serviceId,
52+
}
53+
}
54+
4055
func (c *config) Get(key string) string {
4156
err := viper.ReadInConfig()
4257
if err != nil {

0 commit comments

Comments
 (0)