-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd.go
104 lines (89 loc) · 3.72 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
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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package transfer
import (
"github.com/spf13/cobra"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/common/options/formatoption"
ocmcommon "github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/common"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/common/options/lookupoption"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/common/options/overwriteoption"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/common/options/rscbyvalueoption"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/common/options/srcbyvalueoption"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/ocmcmds/names"
"github.com/open-component-model/ocm/v2/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/v2/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/v2/pkg/common"
"github.com/open-component-model/ocm/v2/pkg/common/accessobj"
"github.com/open-component-model/ocm/v2/pkg/contexts/clictx"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/repositories/comparch"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/transfer"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/transfer/transferhandler"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/transfer/transferhandler/standard"
)
var (
Names = names.ComponentArchive
Verb = verbs.Transfer
)
type Command struct {
utils.BaseCommand
Path string
TargetName string
}
// NewCommand creates a new transfer command.
func NewCommand(ctx clictx.Context, names ...string) *cobra.Command {
return utils.SetupCommand(&Command{BaseCommand: utils.NewBaseCommand(ctx, formatoption.New(), lookupoption.New(), overwriteoption.New(), rscbyvalueoption.New(), srcbyvalueoption.New())}, utils.Names(Names, names...)...)
}
func (o *Command) ForName(name string) *cobra.Command {
return &cobra.Command{
Use: "[<options>] <source> <target>",
Args: cobra.MinimumNArgs(2),
Short: "transfer component archive to some component repository",
Long: `
Transfer a component archive to some component repository. This might
be a CTF Archive or a regular repository.
If the type CTF is specified the target must already exist, if CTF flavor
is specified it will be created if it does not exist.
Besides those explicitly known types a complete repository spec might be configured,
either via inline argument or command configuration file and name.
`,
}
}
func (o *Command) Complete(args []string) error {
o.Path = args[0]
o.TargetName = args[1]
return nil
}
func (o *Command) Run() error {
session := ocm.NewSession(nil)
defer session.Close()
session.Finalize(o.OCMContext())
err := o.ProcessOnOptions(ocmcommon.CompleteOptionsWithSession(o, session))
if err != nil {
return err
}
source, err := comparch.Open(o.Context.OCMContext(), accessobj.ACC_READONLY, o.Path, 0, o.Context)
if err != nil {
return err
}
session.Closer(source)
format := formatoption.From(o).ChangedFormat()
target, err := ocm.AssureTargetRepository(session, o.Context.OCMContext(), o.TargetName, ocm.CommonTransportFormat, format, o.Context.FileSystem())
if err != nil {
return err
}
transferopts := &standard.Options{}
transferhandler.From(o.ConfigContext(), transferopts)
transferhandler.ApplyOptions(transferopts,
lookupoption.From(o),
overwriteoption.From(o),
rscbyvalueoption.From(o),
srcbyvalueoption.From(o),
)
thdlr, err := standard.New(transferopts)
if err != nil {
return err
}
return transfer.TransferVersion(common.NewPrinter(o.Context.StdOut()), nil, source, target, thdlr)
}