github.com/developest/gtm-enhanced@v1.0.4-0.20220111132249-cc80a3372c3f/command/commit.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  	"strings"
    10  
    11  	"github.com/DEVELOPEST/gtm-core/metric"
    12  	"github.com/mitchellh/cli"
    13  )
    14  
    15  // CommitCmd struct contain methods for commit command
    16  type CommitCmd struct {
    17  	UI cli.Ui
    18  }
    19  
    20  // NewCommit returns new CommitCmd struct
    21  func NewCommit() (cli.Command, error) {
    22  	return CommitCmd{}, nil
    23  }
    24  
    25  // Help returns help for commit command
    26  func (c CommitCmd) Help() string {
    27  	helpText := `
    28  Usage: gtm commit [options]
    29  
    30    Save pending time with last commit. 
    31  
    32  Options:
    33  
    34    -yes                       Save time data without asking for confirmation.
    35  `
    36  	return strings.TrimSpace(helpText)
    37  }
    38  
    39  // Run executes commit commands with args
    40  func (c CommitCmd) Run(args []string) int {
    41  
    42  	var yes bool
    43  	cmdFlags := flag.NewFlagSet("commit", flag.ContinueOnError)
    44  	cmdFlags.BoolVar(&yes, "yes", false, "")
    45  	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
    46  	if err := cmdFlags.Parse(args); err != nil {
    47  		return 1
    48  	}
    49  
    50  	confirm := yes
    51  	if !confirm {
    52  		response, err := c.UI.Ask("Save time for last commit (y/n)?")
    53  		if err != nil {
    54  			return 0
    55  		}
    56  		confirm = strings.TrimSpace(strings.ToLower(response)) == "y"
    57  	}
    58  
    59  	if confirm {
    60  		if _, err := metric.Process(false); err != nil {
    61  			c.UI.Error(err.Error())
    62  			return 1
    63  		}
    64  	}
    65  	return 0
    66  }
    67  
    68  // Synopsis return help for commit command
    69  func (c CommitCmd) Synopsis() string {
    70  	return "Save pending time with the last commit"
    71  }