-
Notifications
You must be signed in to change notification settings - Fork 51
/
clone.go
99 lines (87 loc) · 2.84 KB
/
clone.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
package clone
import (
"fmt"
"os"
"path/filepath"
"github.com/jenkins-x-plugins/jx-gitops/pkg/cmd/git/setup"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
info = termcolor.ColorInfo
cmdLong = templates.LongDesc(`
Clones the cluster git repository from the URL provided to the jx-git-operator watching the said repository, and
authenticating as git user with token provided by jx-boot secret, also provided by jx-git-operator installation.
Effectively this command runs jx gitops git setup before proceeding to simply clone the repository into either the
folder passed in via --clone-dir or into the ./setup directory.
`)
cmdExample = templates.Examples(`
%s git clone
`)
)
// Options the options for the command
type Options struct {
setup.Options
CloneDir string
}
// NewCmdGitClone creates a command object for the command
func NewCmdGitClone() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "clone",
Short: "Clones the cluster git repository using the URL, git user and token from the Secret",
Long: cmdLong,
Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.CloneDir, "clone-dir", "", "", "the directory to clone the repository to. Default value used is ./source relative to the directory in which the command is running")
o.Options.AddFlags(cmd)
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
err := o.Options.Run()
if err != nil {
return errors.Wrapf(err, "failed to setup git")
}
if o.CloneDir == "" {
o.CloneDir = filepath.Join(o.Dir, "source")
}
u := o.GitURL
if u == "" {
return errors.Errorf("no git URL found in th eboot secret")
}
gitInitCommands := o.GitInitCommands
if o.CommandRunner == nil {
o.CommandRunner = cmdrunner.QuietCommandRunner
}
if gitInitCommands != "" {
c := &cmdrunner.Command{
Name: "sh",
Args: []string{"-c", gitInitCommands},
Out: os.Stdout,
Err: os.Stderr,
}
_, err = o.CommandRunner(c)
if err != nil {
return errors.Wrapf(err, "failed to run git init commands: %s", c.CLI())
}
log.Logger().Infof("ran git init commands: %s", gitInitCommands)
}
_, err = gitclient.CloneToDir(o.GitClient(), u, o.CloneDir)
if err != nil {
return errors.Wrapf(err, "failed to git clone URL %s to dir %s", u, o.CloneDir)
}
log.Logger().Infof("cloned repository %s to dir %s", info(u), info(o.CloneDir))
return nil
}