-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_unstash.go
161 lines (149 loc) · 4.42 KB
/
step_unstash.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
package cmd
import (
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/cloud/buckets"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"io"
"io/ioutil"
"net/url"
"path/filepath"
"strings"
"time"
)
// StepUnstashOptions contains the command line flags
type StepUnstashOptions struct {
StepOptions
URL string
OutDir string
Timeout time.Duration
}
var (
stepUnstashLong = templates.LongDesc(`
This pipeline step unstashes the files in storage to a local file or the console
` + storageSupportDescription + SeeAlsoText("jx step stash", "jx edit storage"))
stepUnstashExample = templates.Examples(`
# unstash a file to the reports directory
jx step unstash --url s3://mybucket/tests/myOrg/myRepo/mybranch/3/junit.xml -o reports
# unstash the file to the from GCS to the console
jx step unstash -u gs://mybucket/foo/bar/output.log
`)
)
// NewCmdStepUnstash creates the CLI command
func NewCmdStepUnstash(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepUnstashOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "unstash",
Short: "Unstashes files generated as part of a pipeline to a local file or directory or displays on the console",
Aliases: []string{"collect"},
Long: stepUnstashLong,
Example: stepUnstashExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.URL, "url", "u", "", "The fully qualified URL to the file to unstash including the storage host, path and file name")
cmd.Flags().StringVarP(&options.OutDir, "output", "o", "", "The output file or directory")
cmd.Flags().DurationVarP(&options.Timeout, "timeout", "t", time.Second*30, "The timeout period before we should fail unstashing the entry")
return cmd
}
// Run runs the command
func (o *StepUnstashOptions) Run() error {
u := o.URL
if u == "" {
// TODO lets guess from the project etc...
return util.MissingOption("url")
}
file := o.OutDir
if file != "" {
isDir, err := util.DirExists(file)
if err != nil {
return errors.Wrapf(err, "failed to check if %s is a directory", file)
}
if isDir {
u2, err := url.Parse(u)
if err != nil {
return errors.Wrapf(err, "failed to parse URL %s", u)
}
name := u2.Path
if name == "" || strings.HasSuffix(name, "/") {
name += "output.txt"
}
file = filepath.Join(file, name)
}
}
authSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
data, err := buckets.ReadURL(u, o.Timeout, CreateBucketHTTPFn(authSvc))
if err != nil {
return err
}
if file == "" {
log.Infof("%s\n", string(data))
return nil
}
err = ioutil.WriteFile(file, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to write file %s", file)
}
log.Infof("wrote: %s\n", util.ColorInfo(file))
return nil
}
// CreateBucketHTTPFn creates a function to transform a git URL to add the token for accessing a git based bucket
func CreateBucketHTTPFn(authSvc auth.ConfigService) func(string) (string, error) {
return func(urlText string) (string, error) {
token, err := GetTokenForGitURL(authSvc, urlText)
if err != nil {
log.Warnf("Could not find the git token to access urlText %s due to: %s\n", urlText, err)
} else if token != "" {
idx := strings.Index(urlText, "://")
if idx > 0 {
idx += 3
urlText = urlText[0:idx] + token + "@" + urlText[idx:]
}
}
return urlText, nil
}
}
// GetTokenForGitURL returns the git token for the given git URL
func GetTokenForGitURL(authSvc auth.ConfigService, u string) (string, error) {
gitInfo, err := gits.ParseGitURL(u)
if err != nil {
return "", err
}
gitServerURL := gitInfo.HostURL()
auths := authSvc.Config().FindUserAuths(gitServerURL)
for _, auth := range auths {
if auth.ApiToken != "" {
return auth.ApiToken, nil
}
}
if gitServerURL == "https://raw.githubusercontent.com" {
auths := authSvc.Config().FindUserAuths(gits.GitHubURL)
for _, auth := range auths {
if auth.ApiToken != "" {
return auth.ApiToken, nil
}
}
}
return "", nil
}