forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
66 lines (60 loc) · 1.68 KB
/
cmd.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
package cmd
import (
"fmt"
"io"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kubecmd "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/spf13/cobra"
)
// Bulk provides helpers for iterating over a list of items
type Bulk struct {
Factory *kubecmd.Factory
Command *cobra.Command
After func(*resource.Info, error)
}
func NewPrintNameOrErrorAfter(out, errs io.Writer) func(*resource.Info, error) {
return func(info *resource.Info, err error) {
if err == nil {
fmt.Fprintf(out, "%s\n", info.Name)
} else {
fmt.Fprintf(errs, "%v\n", err)
}
}
}
// Create attempts to create each item generically, gathering all errors in the
// event a failure occurs. The contents of list will be updated to include the
// version from the server.
func (b *Bulk) Create(list *kapi.List, namespace string) []error {
mapper, typer := b.Factory.Object(b.Command)
resourceMapper := &resource.Mapper{typer, mapper, b.Factory.ClientMapperForCommand(b.Command)}
after := b.After
if after == nil {
after = func(*resource.Info, error) {}
}
errs := []error{}
for i, item := range list.Items {
info, err := resourceMapper.InfoForObject(item)
if err != nil {
errs = append(errs, err)
after(info, err)
continue
}
data, err := info.Mapping.Codec.Encode(item)
if err != nil {
errs = append(errs, err)
after(info, err)
continue
}
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(namespace, false, data)
if err != nil {
errs = append(errs, err)
after(info, err)
continue
}
info.Refresh(obj, true)
list.Items[i] = obj
after(info, nil)
}
return errs
}