Skip to content

Commit

Permalink
(release): v0.5.0 (#30)
Browse files Browse the repository at this point in the history
* (feat): me command
* (feat): user id not required
* (release): v0.5.0
  • Loading branch information
lucassabreu authored Jun 15, 2020
1 parent ac9bfb1 commit c38bb80
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 3 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.5.0] - 2020-06-15

## Changed

- `in`, `log` and `report` now don't require you to inform a "user-id", if none is set,
than will get the user id from the token used to access the api

## Added

- `me` command returns information about the user who owns the token used to access
the clockify's api

## [v0.4.0] - 2020-06-01

## Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Available Commands:
help Help about any command
in Create a new time entry and starts it
log List the entries from a specific day
me Show the user info
out Stops the last time entry
project Allow project aliasing and integration of a project with GitHub:Issues or Trello
report report for date ranges and with more data (format date as 2016-01-02)
Expand Down
12 changes: 12 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ func (c *Client) GetUser(id string) (*dto.User, error) {
return user, err
}

func (c *Client) GetMe() (dto.User, error) {
r, err := c.NewRequest("GET", "v1/user", nil)

if err != nil {
return dto.User{}, err
}

var user dto.User
_, err = c.Do(r, &user)
return user, err
}

// GetTaskParam params to get a Task
type GetTaskParam struct {
Workspace string
Expand Down
15 changes: 15 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,18 @@ func getTagIDs(tagIDs []string, workspace string, c *api.Client) ([]string, erro

return tagIDs, nil
}

func getUserId(c *api.Client) (string, error) {
userId := viper.GetString("user.id")
if len(userId) > 0 {
return userId, nil
}

u, err := c.GetMe()
if err != nil {
return "", err
}

return u.ID, nil

}
7 changes: 6 additions & 1 deletion cmd/inClone.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ var inCloneCmd = &cobra.Command{
Run: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) {
var err error

userId, err := getUserId(c)
if err != nil {
printError(err)
return
}
workspace := viper.GetString("workspace")
tec, err := getTimeEntry(
args[0],
workspace,
viper.GetString("user.id"),
userId,
c,
)
tec.TimeInterval.End = nil
Expand Down
8 changes: 7 additions & 1 deletion cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ var logCmd = &cobra.Command{
filterDate = time.Now().Add(time.Hour * -24)
}

userId, err := getUserId(c)
if err != nil {
printError(err)
return
}

log, err := c.Log(api.LogParam{
Workspace: viper.GetString("workspace"),
UserID: viper.GetString("user.id"),
UserID: userId,
Date: filterDate,
PaginationParam: api.PaginationParam{AllPages: true},
})
Expand Down
70 changes: 70 additions & 0 deletions cmd/me.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright © 2020 Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"io"
"os"

"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
"github.com/lucassabreu/clockify-cli/reports"
"github.com/spf13/cobra"
)

// meCmd represents the me command
var meCmd = &cobra.Command{
Use: "me",
Short: "Show the user info",
Run: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) {
format, _ := cmd.Flags().GetString("format")
asJSON, _ := cmd.Flags().GetBool("json")

u, err := c.GetMe()
if err != nil {
printError(err)
return
}

var reportFn func(dto.User, io.Writer) error
reportFn = func(u dto.User, w io.Writer) error {
return reports.UserPrint([]dto.User{u}, w)
}

if asJSON {
reportFn = reports.UserJSONPrint
}

if format != "" {
reportFn = func(u dto.User, w io.Writer) error {
return reports.UserPrintWithTemplate(format)(
[]dto.User{u},
w,
)
}
}

if err = reportFn(u, os.Stdout); err != nil {
printError(err)
}
}),
}

func init() {
rootCmd.AddCommand(meCmd)

meCmd.Flags().StringP("format", "f", "", "golang text/template format to be applied on the user")
meCmd.Flags().BoolP("json", "j", false, "print as json")
}
9 changes: 8 additions & 1 deletion cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ func reportWithRange(c *api.Client, start, end time.Time, cmd *cobra.Command) {

start = start.Add(time.Duration(start.Hour()) * time.Hour * -1)
end = end.Add(time.Duration(24-start.Hour()) * time.Hour * 1)

userId, err := getUserId(c)
if err != nil {
printError(err)
return
}

log, err := c.LogRange(api.LogRangeParam{
Workspace: viper.GetString("workspace"),
UserID: viper.GetString("user.id"),
UserID: userId,
FirstDate: start,
LastDate: end,
PaginationParam: api.PaginationParam{AllPages: true},
Expand Down
5 changes: 5 additions & 0 deletions reports/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reports

import (
"encoding/json"
"fmt"
"io"
"text/template"
Expand All @@ -18,6 +19,10 @@ func UserPrintQuietly(users []dto.User, w io.Writer) error {
return nil
}

func UserJSONPrint(u dto.User, w io.Writer) error {
return json.NewEncoder(w).Encode(u)
}

// UserPrint will print more details
func UserPrint(users []dto.User, w io.Writer) error {
tw := tablewriter.NewWriter(w)
Expand Down

0 comments on commit c38bb80

Please sign in to comment.