Skip to content

Commit c38bb80

Browse files
authored
(release): v0.5.0 (#30)
* (feat): me command * (feat): user id not required * (release): v0.5.0
1 parent ac9bfb1 commit c38bb80

File tree

9 files changed

+136
-3
lines changed

9 files changed

+136
-3
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [v0.5.0] - 2020-06-15
10+
11+
## Changed
12+
13+
- `in`, `log` and `report` now don't require you to inform a "user-id", if none is set,
14+
than will get the user id from the token used to access the api
15+
16+
## Added
17+
18+
- `me` command returns information about the user who owns the token used to access
19+
the clockify's api
20+
921
## [v0.4.0] - 2020-06-01
1022

1123
## Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Available Commands:
4646
help Help about any command
4747
in Create a new time entry and starts it
4848
log List the entries from a specific day
49+
me Show the user info
4950
out Stops the last time entry
5051
project Allow project aliasing and integration of a project with GitHub:Issues or Trello
5152
report report for date ranges and with more data (format date as 2016-01-02)

api/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,18 @@ func (c *Client) GetUser(id string) (*dto.User, error) {
353353
return user, err
354354
}
355355

356+
func (c *Client) GetMe() (dto.User, error) {
357+
r, err := c.NewRequest("GET", "v1/user", nil)
358+
359+
if err != nil {
360+
return dto.User{}, err
361+
}
362+
363+
var user dto.User
364+
_, err = c.Do(r, &user)
365+
return user, err
366+
}
367+
356368
// GetTaskParam params to get a Task
357369
type GetTaskParam struct {
358370
Workspace string

cmd/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,18 @@ func getTagIDs(tagIDs []string, workspace string, c *api.Client) ([]string, erro
267267

268268
return tagIDs, nil
269269
}
270+
271+
func getUserId(c *api.Client) (string, error) {
272+
userId := viper.GetString("user.id")
273+
if len(userId) > 0 {
274+
return userId, nil
275+
}
276+
277+
u, err := c.GetMe()
278+
if err != nil {
279+
return "", err
280+
}
281+
282+
return u.ID, nil
283+
284+
}

cmd/inClone.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ var inCloneCmd = &cobra.Command{
3535
Run: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) {
3636
var err error
3737

38+
userId, err := getUserId(c)
39+
if err != nil {
40+
printError(err)
41+
return
42+
}
3843
workspace := viper.GetString("workspace")
3944
tec, err := getTimeEntry(
4045
args[0],
4146
workspace,
42-
viper.GetString("user.id"),
47+
userId,
4348
c,
4449
)
4550
tec.TimeInterval.End = nil

cmd/log.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ var logCmd = &cobra.Command{
5151
filterDate = time.Now().Add(time.Hour * -24)
5252
}
5353

54+
userId, err := getUserId(c)
55+
if err != nil {
56+
printError(err)
57+
return
58+
}
59+
5460
log, err := c.Log(api.LogParam{
5561
Workspace: viper.GetString("workspace"),
56-
UserID: viper.GetString("user.id"),
62+
UserID: userId,
5763
Date: filterDate,
5864
PaginationParam: api.PaginationParam{AllPages: true},
5965
})

cmd/me.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright © 2020 Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"io"
19+
"os"
20+
21+
"github.com/lucassabreu/clockify-cli/api"
22+
"github.com/lucassabreu/clockify-cli/api/dto"
23+
"github.com/lucassabreu/clockify-cli/reports"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// meCmd represents the me command
28+
var meCmd = &cobra.Command{
29+
Use: "me",
30+
Short: "Show the user info",
31+
Run: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) {
32+
format, _ := cmd.Flags().GetString("format")
33+
asJSON, _ := cmd.Flags().GetBool("json")
34+
35+
u, err := c.GetMe()
36+
if err != nil {
37+
printError(err)
38+
return
39+
}
40+
41+
var reportFn func(dto.User, io.Writer) error
42+
reportFn = func(u dto.User, w io.Writer) error {
43+
return reports.UserPrint([]dto.User{u}, w)
44+
}
45+
46+
if asJSON {
47+
reportFn = reports.UserJSONPrint
48+
}
49+
50+
if format != "" {
51+
reportFn = func(u dto.User, w io.Writer) error {
52+
return reports.UserPrintWithTemplate(format)(
53+
[]dto.User{u},
54+
w,
55+
)
56+
}
57+
}
58+
59+
if err = reportFn(u, os.Stdout); err != nil {
60+
printError(err)
61+
}
62+
}),
63+
}
64+
65+
func init() {
66+
rootCmd.AddCommand(meCmd)
67+
68+
meCmd.Flags().StringP("format", "f", "", "golang text/template format to be applied on the user")
69+
meCmd.Flags().BoolP("json", "j", false, "print as json")
70+
}

cmd/report.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,16 @@ func reportWithRange(c *api.Client, start, end time.Time, cmd *cobra.Command) {
104104

105105
start = start.Add(time.Duration(start.Hour()) * time.Hour * -1)
106106
end = end.Add(time.Duration(24-start.Hour()) * time.Hour * 1)
107+
108+
userId, err := getUserId(c)
109+
if err != nil {
110+
printError(err)
111+
return
112+
}
113+
107114
log, err := c.LogRange(api.LogRangeParam{
108115
Workspace: viper.GetString("workspace"),
109-
UserID: viper.GetString("user.id"),
116+
UserID: userId,
110117
FirstDate: start,
111118
LastDate: end,
112119
PaginationParam: api.PaginationParam{AllPages: true},

reports/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package reports
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"text/template"
@@ -18,6 +19,10 @@ func UserPrintQuietly(users []dto.User, w io.Writer) error {
1819
return nil
1920
}
2021

22+
func UserJSONPrint(u dto.User, w io.Writer) error {
23+
return json.NewEncoder(w).Encode(u)
24+
}
25+
2126
// UserPrint will print more details
2227
func UserPrint(users []dto.User, w io.Writer) error {
2328
tw := tablewriter.NewWriter(w)

0 commit comments

Comments
 (0)