github.com/kilpkonn/gtm-enhanced@v1.3.5/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/git-time-metric/gtm/metric"
    13  	"github.com/git-time-metric/gtm/note"
    14  	"github.com/git-time-metric/gtm/project"
    15  	"github.com/git-time-metric/gtm/report"
    16  	"github.com/git-time-metric/gtm/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  
    41    -color=false               Always output color even if no terminal is detected, i.e 'gtm status -color | less -R'
    42  
    43    -total-only=false          Only display total pending time
    44  
    45    -long-duration             If total-only, display total pending time in long duration format
    46  
    47    -tags=""                   Project tags to report status for, i.e --tags tag1,tag2
    48  
    49    -all=false                 Show status for all projects
    50  `
    51  	return strings.TrimSpace(helpText)
    52  }
    53  
    54  // Run executes status command with args
    55  func (c StatusCmd) Run(args []string) int {
    56  	var color, terminalOff, totalOnly, all, profile, longDuration bool
    57  	var tags string
    58  	cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
    59  	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'")
    60  	cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "Exclude time spent in terminal (Terminal plugin is required)")
    61  	cmdFlags.BoolVar(&totalOnly, "total-only", false, "Only display total time")
    62  	cmdFlags.BoolVar(&longDuration, "long-duration", false, "Display total time in long duration format")
    63  	cmdFlags.StringVar(&tags, "tags", "", "Project tags to show status on")
    64  	cmdFlags.BoolVar(&all, "all", false, "Show status for all projects")
    65  	cmdFlags.BoolVar(&profile, "profile", false, "Enable profiling")
    66  	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
    67  	if err := cmdFlags.Parse(args); err != nil {
    68  		return 1
    69  	}
    70  
    71  	if totalOnly && (all || tags != "") {
    72  		c.UI.Error("\n-tags and -all options not allowed with -total-only\n")
    73  		return 1
    74  	}
    75  
    76  	var (
    77  		err        error
    78  		commitNote note.CommitNote
    79  		out        string
    80  	)
    81  
    82  	index, err := project.NewIndex()
    83  	if err != nil {
    84  		c.UI.Error(err.Error())
    85  		return 1
    86  	}
    87  
    88  	tagList := []string{}
    89  	if tags != "" {
    90  		tagList = util.Map(strings.Split(tags, ","), strings.TrimSpace)
    91  	}
    92  
    93  	projects, err := index.Get(tagList, all)
    94  	if err != nil {
    95  		c.UI.Error(err.Error())
    96  		return 1
    97  	}
    98  
    99  	options := report.OutputOptions{
   100  		TotalOnly:    totalOnly,
   101  		LongDuration: longDuration,
   102  		TerminalOff:  terminalOff,
   103  		Color:        color}
   104  
   105  	for _, projPath := range projects {
   106  		if commitNote, err = metric.Process(true, projPath); err != nil {
   107  			c.UI.Error(err.Error())
   108  			return 1
   109  		}
   110  		o, err := report.Status(commitNote, options, projPath)
   111  		if err != nil {
   112  			c.UI.Error(err.Error())
   113  			return 1
   114  		}
   115  		out += o
   116  	}
   117  
   118  	if totalOnly {
   119  		// plain output, no ansi escape sequences
   120  		fmt.Print(out)
   121  	} else {
   122  		c.UI.Output(out)
   123  	}
   124  	return 0
   125  }
   126  
   127  // Synopsis returns help for status command
   128  func (c StatusCmd) Synopsis() string {
   129  	return "Show pending time"
   130  }