github.com/developest/gtm-enhanced@v1.0.4-0.20220111132249-cc80a3372c3f/command/status.go (about)

     1  // Copyright 2016 Michael Schenk. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package command
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/DEVELOPEST/gtm-core/metric"
    13  	"github.com/DEVELOPEST/gtm-core/note"
    14  	"github.com/DEVELOPEST/gtm-core/project"
    15  	"github.com/DEVELOPEST/gtm-core/report"
    16  	"github.com/DEVELOPEST/gtm-core/util"
    17  	"github.com/mitchellh/cli"
    18  )
    19  
    20  // StatusCmd containt methods for status command
    21  type StatusCmd struct {
    22  	UI cli.Ui
    23  }
    24  
    25  // NewStatus returns new StatusCmd struct
    26  func NewStatus() (cli.Command, error) {
    27  	return StatusCmd{}, nil
    28  }
    29  
    30  // Help returns help for status command
    31  func (c StatusCmd) Help() string {
    32  	helpText := `
    33  Usage: gtm status [options]
    34  
    35    Show pending time for git repositories.
    36  
    37  Options:
    38  
    39    -terminal-off=false        Exclude time spent in terminal (Terminal plug-in is required)
    40    -app-off=false             Exclude time spent in apps
    41    -color=false               Always output color even if no terminal is detected, i.e 'gtm status -color | less -R'
    42    -total-only=false          Only display total pending time
    43    -long-duration             If total-only, display total pending time in long duration format
    44    -tags=""                   Project tags to report status for, i.e --tags tag1,tag2
    45    -all=false                 Show status for all projects
    46    -auto-log=""               Format output for auto logging time [gitlab, jira]
    47    -cwd=""                    Set cwd (useful for plugins)
    48  `
    49  	return strings.TrimSpace(helpText)
    50  }
    51  
    52  // Run executes status command with args
    53  func (c StatusCmd) Run(args []string) int {
    54  	var color, terminalOff, appOff, totalOnly, all, profile, longDuration bool
    55  	var tags, cwd, autoLog string
    56  	cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
    57  	cmdFlags.BoolVar(&color, "color", false, "Always output color even if no terminal is detected. Use this with pagers i.e 'less -R' or 'more -R'")
    58  	cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "Exclude time spent in terminal (Terminal plugin is required)")
    59  	cmdFlags.BoolVar(&appOff, "app-off", false, "Exclude time spent in apps")
    60  	cmdFlags.BoolVar(&totalOnly, "total-only", false, "Only display total time")
    61  	cmdFlags.BoolVar(&longDuration, "long-duration", false, "Display total time in long duration format")
    62  	cmdFlags.StringVar(&tags, "tags", "", "Project tags to show status on")
    63  	cmdFlags.BoolVar(&all, "all", false, "Show status for all projects")
    64  	cmdFlags.StringVar(&autoLog, "auto-log", "", "Format time for auto logging")
    65  	cmdFlags.StringVar(&cwd, "cwd", "", "Set cwd")
    66  	cmdFlags.BoolVar(&profile, "profile", false, "Enable profiling")
    67  	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
    68  	if err := cmdFlags.Parse(args); err != nil {
    69  		return 1
    70  	}
    71  
    72  	if totalOnly && (all || tags != "") {
    73  		c.UI.Error("\n-tags and -all options not allowed with -total-only\n")
    74  		return 1
    75  	}
    76  
    77  	var (
    78  		projects   []string
    79  		err        error
    80  		commitNote note.CommitNote
    81  		out        string
    82  	)
    83  
    84  	index, err := project.NewIndex()
    85  	if err != nil {
    86  		c.UI.Error(err.Error())
    87  		return 1
    88  	}
    89  
    90  	var tagList []string
    91  	if tags != "" {
    92  		tagList = util.Map(strings.Split(tags, ","), strings.TrimSpace)
    93  	}
    94  
    95  	if cwd != "" {
    96  		projects, err = index.Get(tagList, all, cwd)
    97  	} else {
    98  		projects, err = index.Get(tagList, all)
    99  	}
   100  	if err != nil {
   101  		c.UI.Error(err.Error())
   102  		return 1
   103  	}
   104  
   105  	options := report.OutputOptions{
   106  		TotalOnly:    totalOnly,
   107  		AutoLog:      autoLog,
   108  		LongDuration: longDuration,
   109  		TerminalOff:  terminalOff,
   110  		AppOff:       appOff,
   111  		Color:        color}
   112  
   113  	for _, projPath := range projects {
   114  		if commitNote, err = metric.Process(true, projPath); err != nil {
   115  			c.UI.Error(err.Error())
   116  			return 1
   117  		}
   118  		o, err := report.Status(commitNote, options, projPath)
   119  		if err != nil {
   120  			c.UI.Error(err.Error())
   121  			return 1
   122  		}
   123  		out += o
   124  	}
   125  
   126  	if totalOnly {
   127  		// plain output, no ansi escape sequences
   128  		fmt.Print(out)
   129  	} else {
   130  		c.UI.Output(out)
   131  	}
   132  	return 0
   133  }
   134  
   135  // Synopsis returns help for status command
   136  func (c StatusCmd) Synopsis() string {
   137  	return "Show pending time"
   138  }