-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.go
173 lines (151 loc) · 5.24 KB
/
app.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
172
173
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package support
import (
"fmt"
"strings"
_ "github.com/open-component-model/ocm/v2/pkg/contexts/clictx/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
common2 "github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/common"
"github.com/open-component-model/ocm/v2/pkg/cobrautils"
"github.com/open-component-model/ocm/v2/pkg/cobrautils/logopts"
"github.com/open-component-model/ocm/v2/pkg/common"
"github.com/open-component-model/ocm/v2/pkg/contexts/credentials"
"github.com/open-component-model/ocm/v2/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/v2/pkg/contexts/datacontext/attrs/vfsattr"
datactg "github.com/open-component-model/ocm/v2/pkg/contexts/datacontext/config/attrs"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm"
"github.com/open-component-model/ocm/v2/pkg/errors"
"github.com/open-component-model/ocm/v2/pkg/out"
"github.com/open-component-model/ocm/v2/pkg/toi"
"github.com/open-component-model/ocm/v2/pkg/toi/install"
"github.com/open-component-model/ocm/v2/pkg/version"
)
type BootstrapperCLIOptions struct {
ExecutorOptions
logopts.Options
CredentialSettings []string
Settings []string
}
func NewCLICommand(ctx ocm.Context, name string, exec func(options *ExecutorOptions) error) *cobra.Command {
if ctx == nil {
ctx = ocm.DefaultContext()
}
opts := &BootstrapperCLIOptions{
ExecutorOptions: ExecutorOptions{
Context: ctx,
},
}
cmd := &cobra.Command{
Use: name + " {<options>} <action> <component version>",
Short: "Bootstrapper using the OCM bootstrap mechanism",
Version: version.Get().String(),
TraverseChildren: true,
SilenceUsage: true,
DisableFlagsInUseLine: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
action := ""
if len(args) > 0 {
action = args[0]
}
opts.Action = action
if len(args) > 1 {
opts.ComponentVersionName = args[1]
}
return opts.Complete()
},
RunE: func(cmd *cobra.Command, args []string) error {
out.Outf(opts.OutputContext, "This is %s (%s)\n", name, version.Get().String())
e := &Executor{Completed: true, Options: &opts.ExecutorOptions, Run: exec}
return e.Execute()
},
}
cobrautils.TweakCommand(cmd, nil)
cmd.AddCommand(NewVersionCommand())
opts.AddFlags(cmd.Flags())
cmd.InitDefaultHelpCmd()
return cmd
}
func (o *BootstrapperCLIOptions) AddFlags(fs *pflag.FlagSet) {
o.Options.AddFlags(fs)
fs.StringVarP(&o.OCMConfig, "ocmconfig", "", "", "ocm configuration file")
fs.StringArrayVarP(&o.CredentialSettings, "cred", "C", nil, "credential setting")
fs.StringArrayVarP(&o.Settings, "attribute", "X", nil, "attribute setting")
fs.StringVarP(&o.Inputs, "inputs", "", "", "input path")
fs.StringVarP(&o.Outputs, "outputs", "", "", "output path")
fs.StringVarP(&o.Root, "bootstraproot", "", install.PathTOI, "bootstrapper contract root folder")
fs.StringVarP(&o.Config, "config", "", "", "bootstrapper configuration input file")
fs.StringVarP(&o.Parameters, "parameters", "", "", "bootstrapper parameter input file")
fs.StringVarP(&o.RepoPath, "ctf", "", "", "bootstrapper transport archive")
}
func (o *BootstrapperCLIOptions) Complete() error {
o.Options.Configure(o.Context, o.Context.LoggingContext())
if err := o.ExecutorOptions.Complete(); err != nil {
return fmt.Errorf("unable to complete options: %w", err)
}
id := credentials.ConsumerIdentity{}
attrs := common.Properties{}
for _, s := range o.CredentialSettings {
i := strings.Index(s, "=")
if i < 0 {
return errors.ErrInvalid("credential setting", s)
}
name := s[:i]
value := s[i+1:]
if strings.HasPrefix(name, ":") {
if len(attrs) != 0 {
o.Context.CredentialsContext().SetCredentialsForConsumer(id, credentials.NewCredentials(attrs))
id = credentials.ConsumerIdentity{}
attrs = common.Properties{}
}
name = name[1:]
id[name] = value
} else {
attrs[name] = value
}
if len(name) == 0 {
return errors.ErrInvalid("credential setting", s)
}
}
if len(attrs) != 0 {
o.Context.CredentialsContext().SetCredentialsForConsumer(id, credentials.NewCredentials(attrs))
} else if len(id) != 0 {
return errors.Newf("empty credential attribute set for %s", id.String())
}
set, err := common2.ParseLabels(vfsattr.Get(o.Context), o.Settings, "attribute setting")
if err == nil && len(set) > 0 {
ctx := o.Context.ConfigContext()
spec := datactg.New()
for _, s := range set {
attr := s.Name
eff := datacontext.DefaultAttributeScheme.Shortcuts()[attr]
if eff != "" {
attr = eff
}
err = spec.AddRawAttribute(attr, s.Value)
if err != nil {
return errors.Wrapf(err, "attribute %s", s.Name)
}
}
err = ctx.ApplyConfig(spec, "cli")
}
if err != nil {
return fmt.Errorf("unable to parse labels: %w", err)
}
o.Logger = toi.Log
return nil
}
func NewVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "displays the version",
Run: func(cmd *cobra.Command, args []string) {
v := version.Get()
//nolint:forbidigo // It's an intentional Printf.
fmt.Printf("%#v", v)
},
}
}