|
| 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 | +} |
0 commit comments