-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathrepo_index.go
More file actions
170 lines (146 loc) · 5.1 KB
/
Copy pathrepo_index.go
File metadata and controls
170 lines (146 loc) · 5.1 KB
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
package cmd
import (
"errors"
"fmt"
"io"
"time"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/kudobuilder/kudo/pkg/kudoctl/files"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/repo"
)
const (
repoIndexDesc = `
Read the provided directory and generate an index file based on the packages found.
This tool is used for creating an 'index.yaml' file for a KUDO package repository. To
set an absolute URL to the operators, use '--url' or '--url-repo' flag. The '--url-repo'
will look up the the url of the named repo and will provide the absolute URL by repo name.
To merge the generated index with an existing index file, use the '--merge' flag. In this
case, the operator packages found in the current directory will be merged into the existing
index, with local operators taking priority over existing operators. No content of the existing
index file is modified. The use of '--url' or 'url-repo' will only apply to local operator
definitions in the index file. When using '--merge' it is necessary to specify the URL of the
index file to merge. The '--merge-repo' is the same as merge where the url is looked up by
repo name out of the local repositories list.
# Create an index file for all KUDO packages in the repo-dir.
$ kubectl kudo repo index repo-dir`
repoIndexExample = ` #simple index of operators in /opt/repo
kubectl kudo repo index /opt/repo
kubectl kudo repo index /opt/repo --url https://kudo-repository.storage.googleapis.com
# merge with community repo
kubectl kudo repo index /opt/repo --merge https://kudo-repository.storage.googleapis.com
kubectl kudo repo index /opt/repo --merge-repo community
`
)
type repoIndexCmd struct {
path string
url string
urlRepoName string
overwrite bool
mergeRepoName string
mergePath string
out io.Writer
time *time.Time
fs afero.Fs
}
// newRepoIndexCmd for repo commands such as building a repo index file.
func newRepoIndexCmd(fs afero.Fs, out io.Writer, time *time.Time) *cobra.Command {
index := &repoIndexCmd{out: out, fs: fs, time: time}
cmd := &cobra.Command{
Use: "index [flags] <DIR>",
Short: "Generate an index file given a directory containing KUDO operator packages",
Long: repoIndexDesc,
Example: repoIndexExample,
RunE: func(cmd *cobra.Command, args []string) error {
if err := index.validate(args); err != nil {
return err
}
index.path = args[0]
if err := index.run(); err != nil {
return err
}
return nil
},
SilenceUsage: true,
}
f := cmd.Flags()
f.StringVar(&index.url, "url", "", "URL of the operators to reference in the index file")
f.StringVar(&index.urlRepoName, "url-repo", "", "Name of the repo to use URL for operator urls")
f.StringVar(&index.mergePath, "merge", "", "URL or path location of index file to merge with")
f.StringVar(&index.mergeRepoName, "merge-repo", "", "Name of the repo to use as merge URL")
f.BoolVarP(&index.overwrite, "overwrite", "w", false, "Overwrite existing package")
return cmd
}
func (ri *repoIndexCmd) validate(args []string) error {
if len(args) != 1 {
return fmt.Errorf("expecting exactly one argument - directory containing the operators to package")
}
// we do not allow the setting of merge and merge-repo
if ri.mergeRepoName != "" && ri.mergePath != "" {
return errors.New("specify either 'merge' or 'merge-repo', not both")
}
// we do not allow the setting of merge and merge-repo
if ri.url != "" && ri.urlRepoName != "" {
return errors.New("specify either 'url' or 'url-repo', not both")
}
return nil
}
// run returns the errors associated with cmd env
func (ri *repoIndexCmd) run() error {
if ri.urlRepoName != "" {
config, err := ri.repoConfig(ri.urlRepoName)
if err != nil {
return err
}
if config == nil {
return fmt.Errorf("configuration for repositories does not contain %s", ri.urlRepoName)
}
ri.url = config.URL
}
target, err := files.FullPathToTarget(ri.fs, ri.path, "index.yaml", ri.overwrite)
if err != nil {
return err
}
index, err := repo.IndexDirectory(ri.fs, ri.path, ri.url, ri.time)
if err != nil {
return err
}
// if we have a merge path... lets get it
if ri.mergePath != "" || ri.mergeRepoName != "" {
config, err := ri.mergeRepoConfig()
if err != nil {
return err
}
client, err := repo.NewClient(config)
if err != nil {
return err
}
// valid the url and that we can pull and index is valid
mergeIndex, err := client.DownloadIndexFile()
if err != nil {
return err
}
client.Merge(index, mergeIndex)
}
if err := index.WriteFile(ri.fs, target); err != nil {
return err
}
fmt.Fprintf(ri.out, "index %v created.\n", target)
return nil
}
func (ri *repoIndexCmd) mergeRepoConfig() (*repo.Configuration, error) {
if ri.mergeRepoName != "" {
return ri.repoConfig(ri.mergeRepoName)
}
return &repo.Configuration{
URL: ri.mergePath,
Name: "temp-merge-path",
}, nil
}
func (ri *repoIndexCmd) repoConfig(repoName string) (*repo.Configuration, error) {
repos, err := repo.LoadRepositories(ri.fs, Settings.Home.RepositoryFile())
if err != nil {
return nil, err
}
return repos.GetConfiguration(repoName), nil
}