-
Notifications
You must be signed in to change notification settings - Fork 51
/
get.go
171 lines (149 loc) · 5.07 KB
/
get.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package get
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/go-scm/scm"
jxc "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
"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/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/giturl"
"github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/options"
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
cmdLong = templates.LongDesc(`
Gets a file from a git repository or environment git repository
`)
cmdExample = templates.Examples(`
%s git get --file jx-values.yaml --dev dev
`)
info = termcolor.ColorInfo
)
// Options the options for the command
type Options struct {
scmhelpers.Options
Env string
FromRepository string
Path string
To string
Ref string
Namespace string
JXClient jxc.Interface
}
// NewCmdGitGet creates a command object for the command
func NewCmdGitGet() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "get",
Short: "Gets a file from a git repository or environment git repository",
Long: cmdLong,
Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
o.Options.AddFlags(cmd)
cmd.Flags().StringVarP(&o.FromRepository, "from", "", "", "the git repository of the form owner/name to find the file")
cmd.Flags().StringVarP(&o.Env, "env", "e", "", "the name of the Environment to find the git repository URL")
cmd.Flags().StringVarP(&o.Path, "file", "f", "", "the file in the git repository")
cmd.Flags().StringVarP(&o.To, "to", "", "", "the destination of the file. If not specified defaults to the path")
cmd.Flags().StringVarP(&o.Ref, "ref", "", "master", "the git reference (branch, tag or SHA) to query the file")
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
err := o.Validate()
if err != nil {
return errors.Wrapf(err, "failed to validate options")
}
ctx := context.Background()
sha := o.Ref
path := o.Path
repo := o.FromRepository
c, r, err := o.ScmClient.Contents.Find(ctx, repo, path, sha)
if err != nil {
if r != nil && r.Status == 404 {
return errors.Errorf("no file %s in repo %s for ref %s", path, repo, sha)
}
return errors.Wrapf(err, "failed to find file %s in repo %s with ref %s status %d", path, repo, sha, r.Status)
}
to := o.To
if to == "" {
to = filepath.Join(o.Dir, path)
}
dir := filepath.Dir(to)
err = os.MkdirAll(dir, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to create dir %s", dir)
}
err = ioutil.WriteFile(to, c.Data, files.DefaultFileWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", to)
}
log.Logger().Infof("saved file %s from repository %s ref %s", info(to), info(repo), info(sha))
return nil
}
// Validate validates the inputs are valid
func (o *Options) Validate() error {
if o.Options.CommandRunner == nil {
o.Options.CommandRunner = cmdrunner.QuietCommandRunner
}
var err error
if o.FromRepository == "" && o.Env != "" {
err = o.findEnvironmentRepository()
if err != nil {
return errors.Wrapf(err, "failed to find repository of environment %s", o.Env)
}
}
err = o.Options.Validate()
if err != nil {
return errors.Wrapf(err, "failed to validate repository options")
}
if o.FromRepository == "" {
return options.MissingOption("from")
}
if o.Path == "" {
return options.MissingOption("file")
}
return nil
}
func (o *Options) findEnvironmentRepository() error {
var err error
o.JXClient, o.Namespace, err = jxclient.LazyCreateJXClientAndNamespace(o.JXClient, o.Namespace)
if err != nil {
return errors.Wrapf(err, "failed to create jx client")
}
envName := o.Env
env, err := o.JXClient.JenkinsV1().Environments(o.Namespace).Get(context.TODO(), envName, metav1.GetOptions{})
if err != nil {
log.Logger().Warnf("could not find environment %s in namespace %s ", envName, o.Namespace)
return errors.Wrapf(err, "failed to load Environment %s in namespace %s", envName, o.Namespace)
}
gitURL := env.Spec.Source.URL
log.Logger().Infof("environment %s in namespace %s has git URL %s", info(envName), info(o.Namespace), info(gitURL))
if gitURL == "" {
return errors.Errorf("no env.Spec.Source.URL for environment %s", envName)
}
o.SourceURL = gitURL
gitInfo, err := giturl.ParseGitURL(gitURL)
if err != nil {
return errors.Wrapf(err, "failed to parse environment %s git URL %s", env.Name, gitURL)
}
o.FromRepository = scm.Join(gitInfo.Organisation, gitInfo.Name)
o.Owner = gitInfo.Organisation
o.Repository = gitInfo.Name
return nil
}