github.com/developest/gtm-enhanced@v1.0.4-0.20220111132249-cc80a3372c3f/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/DEVELOPEST/gtm-core/project"
    12  	"github.com/DEVELOPEST/gtm-core/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    -auto-log=""               Enable automatic logging to commits for platform [gitlab, jira].
    38    -local=false               Initialize gtm locally, ak no push / fetch hooks are added.
    39    -tags=tag1,tag2            Add tags to projects, multiple calls appends tags.
    40    -clear-tags                Clear all tags.
    41    -cwd=""                    Add directory where command is run in.
    42  `
    43  	return strings.TrimSpace(helpText)
    44  }
    45  
    46  // Run executes init command with args
    47  func (c InitCmd) Run(args []string) int {
    48  	var terminal, clearTags, local bool
    49  	var tags, autoLog, cwd string
    50  	cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
    51  	cmdFlags.BoolVar(&terminal, "terminal", true, "")
    52  	cmdFlags.BoolVar(&local, "local", false, "")
    53  	cmdFlags.StringVar(&autoLog, "auto-log", "", "")
    54  	cmdFlags.BoolVar(&clearTags, "clear-tags", false, "")
    55  	cmdFlags.StringVar(&tags, "tags", "", "")
    56  	cmdFlags.StringVar(&cwd, "cwd", "", "")
    57  	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
    58  	if err := cmdFlags.Parse(args); err != nil {
    59  		return 1
    60  	}
    61  	m, err := project.Initialize(
    62  		terminal,
    63  		util.Map(strings.Split(tags, ","), strings.TrimSpace),
    64  		clearTags,
    65  		autoLog,
    66  		local,
    67  		cwd,
    68  	)
    69  	if err != nil {
    70  		c.UI.Error(err.Error())
    71  		return 1
    72  	}
    73  	c.UI.Output(m + "\n")
    74  	return 0
    75  }
    76  
    77  // Synopsis return help for init command
    78  func (c InitCmd) Synopsis() string {
    79  	return "Initialize git repository for time tracking"
    80  }