generated from yngvark/go-cmd-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
150 lines (122 loc) · 3.03 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cmd
import (
"fmt"
"io"
"os"
"github.com/sirupsen/logrus"
"github.com/yngvark.com/clonerepo/pkg/git"
"github.com/spf13/viper"
"github.com/yngvark.com/clonerepo/pkg/lib/log"
"github.com/spf13/afero"
"github.com/yngvark.com/clonerepo/pkg/lib/config"
"github.com/yngvark.com/clonerepo/pkg/lib"
"github.com/yngvark.com/clonerepo/pkg/clonerepo"
"github.com/spf13/cobra"
)
const (
TemporaryGitDirKey = "temporaryGitDir"
cmdShort = "clonerepo clones git repositores into a pre-determined directory structure, and " +
"then cd-s into the cloned directory."
)
type Opts struct {
Out io.Writer
Err io.Writer
Logger *logrus.Logger
Gitter clonerepo.Gitter
OsOpts config.OsOpts
}
func Run() {
logger := log.New(os.Stdout)
cmd := BuildRootCommand(Opts{
Out: os.Stdout,
Err: os.Stderr,
Logger: logger,
Gitter: git.New(logger),
OsOpts: config.OsOpts{
UserHomeDir: os.UserHomeDir,
LookupEnv: os.LookupEnv,
Fs: afero.NewOsFs(),
},
})
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
//nolint:funlen
func BuildRootCommand(opts Opts) *cobra.Command {
flags := lib.Flags{}
cmd := &cobra.Command{
Use: "clonerepo",
Short: cmdShort,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := config.Init(opts.OsOpts, flags.ConfigFile)
if err != nil {
return fmt.Errorf("initializing config: %w", err)
}
if flags.Verbose {
opts.Logger.SetLevel(logrus.DebugLevel)
}
opts.Logger.Debugln("Using config file:", viper.ConfigFileUsed())
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return cmd.Help()
}
gitDir := getGitDir(flags.Temporary)
clonerepoOpts := clonerepo.Opts{
Out: opts.Out,
Logger: opts.Logger,
Fs: opts.OsOpts.Fs,
Gitter: opts.Gitter,
Flags: flags,
}
return clonerepo.Run(clonerepoOpts, gitDir, args)
},
}
cmd.SetOut(opts.Out)
cmd.SetErr(opts.Err)
cmd.PersistentFlags().StringVar(
&flags.ConfigFile,
"config",
"",
"config file (default is: If $HOME/.config exists, it will be "+
"$HOME/.config/clonerepo/config.yaml. If not, it will be $HOME/.clonerepo.yaml)")
cmd.PersistentFlags().BoolVarP(
&flags.CdToOutputDir,
"cd-to-output-dir",
"o",
true,
"Outputs 'cd <cloned dir>' to stdout, which can be used for sourcing in a shell script.")
cmd.PersistentFlags().BoolVarP(
&flags.DryRun,
"dry-run",
"d",
false,
"Dry run, don't do any changes")
cmd.PersistentFlags().BoolVarP(
&flags.Temporary,
"temporary",
"t",
false,
"Clone to temporary directory, as specified in the config file or /tmp if not specified.")
cmd.PersistentFlags().BoolVarP(
&flags.Verbose,
"verbose",
"v",
false,
"Enables verbose output")
return cmd
}
func getGitDir(temporaryFlag bool) string {
if temporaryFlag {
if viper.IsSet(TemporaryGitDirKey) {
return viper.GetString(TemporaryGitDirKey)
} else {
return "/tmp"
}
} else {
return viper.GetString("gitDir")
}
}