github.com/kilpkonn/gtm-enhanced@v1.3.5/command/init.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/git-time-metric/gtm/project"
    12  	"github.com/git-time-metric/gtm/util"
    13  
    14  	"github.com/mitchellh/cli"
    15  )
    16  
    17  // InitCmd contains methods for init command
    18  type InitCmd struct {
    19  	UI cli.Ui
    20  }
    21  
    22  // NewInit returns new InitCmd struct
    23  func NewInit() (cli.Command, error) {
    24  	return InitCmd{}, nil
    25  }
    26  
    27  // Help return help for init command
    28  func (c InitCmd) Help() string {
    29  	helpText := `
    30  Usage: gtm init [options]
    31  
    32    Initialize a git repository for time tracking.
    33  
    34  Options:
    35  
    36    -terminal=true             Enable time tracking for terminal (requires Terminal plug-in).
    37  
    38    -tags=tag1,tag2            Add tags to projects, multiple calls appends tags.
    39  
    40    -clear-tags                Clear all tags.
    41  `
    42  	return strings.TrimSpace(helpText)
    43  }
    44  
    45  // Run executes init command with args
    46  func (c InitCmd) Run(args []string) int {
    47  	var terminal, clearTags bool
    48  	var tags string
    49  	cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
    50  	cmdFlags.BoolVar(&terminal, "terminal", true, "")
    51  	cmdFlags.BoolVar(&clearTags, "clear-tags", false, "")
    52  	cmdFlags.StringVar(&tags, "tags", "", "")
    53  	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
    54  	if err := cmdFlags.Parse(args); err != nil {
    55  		return 1
    56  	}
    57  	m, err := project.Initialize(terminal, util.Map(strings.Split(tags, ","), strings.TrimSpace), clearTags)
    58  	if err != nil {
    59  		c.UI.Error(err.Error())
    60  		return 1
    61  	}
    62  	c.UI.Output(m + "\n")
    63  	return 0
    64  }
    65  
    66  // Synopsis return help for init command
    67  func (c InitCmd) Synopsis() string {
    68  	return "Initialize git repository for time tracking"
    69  }