-
Notifications
You must be signed in to change notification settings - Fork 786
/
edit_storage.go
145 lines (119 loc) · 5.34 KB
/
edit_storage.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
package edit
import (
jenkinsv1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
editStorageLong = templates.LongDesc(`
Configures the storage location used by your team to stashing files or storing build logs.
If you don't specify any specific storage for a classifier it will try the classifier 'default'. If there is still no configuration then it will default to the git repository for a project.'
` + step.StorageSupportDescription + helper.SeeAlsoText("jx step stash", "jx get storage"))
editStorageExample = templates.Examples(`
# Be prompted what classification to edit
jx edit storage
# Configure the where to store logs prompting the user to ask for more data
jx edit storage -c logs
# Configure the git URL of where to store logs (defaults to gh-pages branch)
jx edit storage -c logs --git-url https://github.com/myorg/mylogs.git'
# Configure the git URL and branch of where to store logs
jx edit storage -c logs --git-url https://github.com/myorg/mylogs.git' --git-branch cheese
# Configure the git URL of where all storage goes to by default unless a specific classifier has a config
jx edit storage -c default --git-url https://github.com/myorg/mylogs.git'
# Configure the tests to be stored in cloud storage (using S3 / GCS / Azure Blobs etc)
jx edit storage -c tests --bucket-url s3://myExistingBucketName
# Creates a new GCS bucket and configures the logs to be stored in it
jx edit storage -c logs --bucket myBucketName
`)
)
// EditStorageOptions the options for the create spring command
type EditStorageOptions struct {
*opts.CommonOptions
StorageLocation jenkinsv1.StorageLocation
CreateBucketValues opts.CreateBucketValues
}
// NewCmdEditStorage creates a command object for the "create" command
func NewCmdEditStorage(commonOpts *opts.CommonOptions) *cobra.Command {
options := &EditStorageOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "storage",
Short: "Configures the storage location for stashing files or storing build logs for your team",
Aliases: []string{"store"},
Long: editStorageLong,
Example: editStorageExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
addStorageLocationFlags(cmd, &options.StorageLocation)
options.CreateBucketValues.AddCreateBucketFlags(cmd)
return cmd
}
func addStorageLocationFlags(cmd *cobra.Command, location *jenkinsv1.StorageLocation) {
cmd.Flags().StringVarP(&location.Classifier, "classifier", "c", "", "A name which classifies this type of file. Example values: "+kube.ClassificationValues)
cmd.Flags().StringVarP(&location.BucketURL, "bucket-url", "", "", "Specify the cloud storage bucket URL to send each file to. e.g. use 's3://nameOfBucket' on AWS, gs://anotherBucket' on GCP or on Azure 'azblob://thatBucket'")
cmd.Flags().StringVarP(&location.GitURL, "git-url", "", "", "Specify the Git URL to of the repository to use for storage")
cmd.Flags().StringVarP(&location.GitBranch, "git-branch", "", "gh-pages", "The branch to use to store files in the git repository")
}
// Run implements the command
func (o *EditStorageOptions) Run() error {
settings, err := o.TeamSettings()
if err != nil {
return err
}
classifier := o.StorageLocation.Classifier
if classifier == "" && !o.BatchMode {
o.StorageLocation.Classifier, err = util.PickName(kube.Classifications, "Pick the content classification name", "The name is used as a key to store content in different locations", o.GetIOFileHandles())
if err != nil {
return errors.Wrapf(err, "failed to pick the classification name")
}
}
if classifier == "" {
return util.MissingOption("classifier")
}
currentLocation := settings.StorageLocationOrDefault(classifier)
if o.StorageLocation.BucketURL == "" && o.StorageLocation.GitURL == "" {
if !o.CreateBucketValues.IsEmpty() {
o.StorageLocation.BucketURL, err = o.CreateBucket(&o.CreateBucketValues, settings)
if err != nil {
return err
}
}
if o.StorageLocation.BucketURL == "" {
o.StorageLocation.BucketURL, err = util.PickValue("Bucket URL:", o.StorageLocation.BucketURL, false, "The go-cloud bucket URL for storage such as 'gs://mybucket/ or s3://bucket2/", o.GetIOFileHandles())
if err != nil {
return errors.Wrapf(err, "failed to pick the bucket URL")
}
}
if o.StorageLocation.BucketURL == "" {
if o.BatchMode {
if currentLocation.GitURL == "" {
return util.MissingOption("git-url")
}
o.StorageLocation.GitURL = currentLocation.GitURL
} else {
o.StorageLocation.GitURL, err = util.PickValue("Git repository URL to store content:", currentLocation.GitURL, false, "The Git URL will be used to clone and push the storage to", o.GetIOFileHandles())
if err != nil {
return errors.Wrapf(err, "failed to pick the git URL")
}
}
}
}
callback := func(env *v1.Environment) error {
env.Spec.TeamSettings.SetStorageLocation(classifier, o.StorageLocation)
return nil
}
return o.ModifyDevEnvironment(callback)
}