Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit c73b6c2

Browse files
committed
ocm command and some ctf cmd extension
1 parent d6846d8 commit c73b6c2

File tree

10 files changed

+92
-63
lines changed

10 files changed

+92
-63
lines changed

cmd/component-cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"fmt"
1010
"os"
1111

12-
"github.com/gardener/component-cli/cmd/component-cli/app"
12+
"github.com/gardener/component-cli/cmd/ocm/app"
1313
)
1414

1515
func main() {
File renamed without changes.

cmd/ocm/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
12+
"github.com/gardener/component-cli/cmd/ocm/app"
13+
)
14+
15+
func main() {
16+
ctx := context.Background()
17+
defer ctx.Done()
18+
cmd := app.NewComponentsCliCommand(ctx)
19+
20+
if err := cmd.Execute(); err != nil {
21+
fmt.Print(err)
22+
os.Exit(1)
23+
}
24+
}

docs/reference/component-cli_ctf_add.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Adds component archives to a ctf
44

55
```
6-
component-cli ctf add CTF_PATH [-f component-archive]... [flags]
6+
component-cli ctf add <CTF_PATH> <component-archive> ... [flags]
77
```
88

99
### Options

docs/reference/component-cli_ctf_push.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Note: Currently only component archives are supoprted. Generic OCI Artifacts wil
1313

1414

1515
```
16-
component-cli ctf push CTF_PATH [flags]
16+
component-cli ctf push <CTF_PATH> ... [flags]
1717
```
1818

1919
### Options

hack/generate-docs/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
"github.com/spf13/cobra/doc"
1313

14-
"github.com/gardener/component-cli/cmd/component-cli/app"
14+
"github.com/gardener/component-cli/cmd/ocm/app"
1515
)
1616

1717
func main() {
1818
fmt.Println("> Generate Docs for ComponentCli")
1919

2020
if len(os.Args) != 2 { // expect 2 as the first one is the programm name
21-
fmt.Printf("Expected exactly one argument, but got %d", len(os.Args) - 1)
21+
fmt.Printf("Expected exactly one argument, but got %d", len(os.Args)-1)
2222
os.Exit(1)
2323
}
2424
outputDir := os.Args[1]

ociclient/mock/client_mock.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/commands/ctf/add.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type AddOptions struct {
3636
func NewAddCommand(ctx context.Context) *cobra.Command {
3737
opts := &AddOptions{}
3838
cmd := &cobra.Command{
39-
Use: "add CTF_PATH [-f component-archive]...",
40-
Args: cobra.RangeArgs(1, 4),
39+
Use: "add <CTF_PATH> <component-archive> ...",
40+
Args: cobra.MinimumNArgs(1),
4141
Short: "Adds component archives to a ctf",
4242
Run: func(cmd *cobra.Command, args []string) {
4343
if err := opts.Complete(args); err != nil {
@@ -117,6 +117,7 @@ It is expected that the given path points to a CTF Archive`, o.CTFPath)
117117
func (o *AddOptions) Complete(args []string) error {
118118
o.CTFPath = args[0]
119119

120+
o.ComponentArchives = append(o.ComponentArchives, args[1:]...)
120121
if err := o.Validate(); err != nil {
121122
return err
122123
}

pkg/commands/ctf/push.go

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
)
2828

2929
type PushOptions struct {
30-
// CTFPath is the path to the directory containing the ctf archive.
31-
CTFPath string
30+
// CTFPaths is the path to the directory containing the ctf archive.
31+
CTFPaths []string
3232
// BaseUrl is the repository context base url for all included component descriptors.
3333
BaseUrl string
3434
// AdditionalTags defines additional tags that the oci artifact should be tagged with.
@@ -42,8 +42,8 @@ type PushOptions struct {
4242
func NewPushCommand(ctx context.Context) *cobra.Command {
4343
opts := &PushOptions{}
4444
cmd := &cobra.Command{
45-
Use: "push CTF_PATH",
46-
Args: cobra.ExactArgs(1),
45+
Use: "push <CTF_PATH> ...",
46+
Args: cobra.MinimumNArgs(1),
4747
Short: "Pushes all archives of a ctf to a remote repository",
4848
Long: `
4949
Push pushes all component archives and oci artifacts to the defined oci repository.
@@ -73,69 +73,74 @@ Note: Currently only component archives are supoprted. Generic OCI Artifacts wil
7373
}
7474

7575
func (o *PushOptions) Run(ctx context.Context, log logr.Logger, fs vfs.FileSystem) error {
76-
info, err := fs.Stat(o.CTFPath)
77-
if err != nil {
78-
return fmt.Errorf("unable to get info for %s: %w", o.CTFPath, err)
79-
}
80-
if info.IsDir() {
81-
return fmt.Errorf(`%q is a directory.
82-
It is expected that the given path points to a CTF Archive`, o.CTFPath)
83-
}
84-
85-
ociClient, cache, err := o.OciOptions.Build(log, fs)
86-
if err != nil {
87-
return fmt.Errorf("unable to build oci client: %s", err.Error())
88-
}
89-
90-
ctfArchive, err := ctf.NewCTF(fs, o.CTFPath)
91-
if err != nil {
92-
return fmt.Errorf("unable to open ctf at %q: %s", o.CTFPath, err.Error())
93-
}
94-
95-
err = ctfArchive.Walk(func(ca *ctf.ComponentArchive) error {
96-
// update repository context
97-
if len(o.BaseUrl) != 0 {
98-
if err := cdv2.InjectRepositoryContext(ca.ComponentDescriptor, cdv2.NewOCIRegistryRepository(o.BaseUrl, "")); err != nil {
99-
return fmt.Errorf("unable to add repository context: %w", err)
100-
}
76+
for _, path := range o.CTFPaths {
77+
info, err := fs.Stat(path)
78+
if err != nil {
79+
return fmt.Errorf("unable to get info for %s: %w", path, err)
80+
}
81+
if info.IsDir() {
82+
return fmt.Errorf(`%q is a directory.
83+
It is expected that the given path points to a CTF Archive`, path)
10184
}
10285

103-
manifest, err := cdoci.NewManifestBuilder(cache, ca).Build(ctx)
86+
ociClient, cache, err := o.OciOptions.Build(log, fs)
10487
if err != nil {
105-
return fmt.Errorf("unable to build oci artifact for component acrchive: %w", err)
88+
return fmt.Errorf("unable to build oci client: %s", err.Error())
10689
}
10790

108-
ref, err := components.OCIRef(ca.ComponentDescriptor.GetEffectiveRepositoryContext(), ca.ComponentDescriptor.GetName(), ca.ComponentDescriptor.GetVersion())
91+
ctfArchive, err := ctf.NewCTF(fs, path)
10992
if err != nil {
110-
return fmt.Errorf("unable to calculate oci ref for %q: %s", ca.ComponentDescriptor.GetName(), err.Error())
111-
}
112-
if err := ociClient.PushManifest(ctx, ref, manifest); err != nil {
113-
return fmt.Errorf("unable to upload component archive to %q: %s", ref, err.Error())
93+
return fmt.Errorf("unable to open ctf at %q: %s", o.CTFPaths, err.Error())
11494
}
115-
log.Info(fmt.Sprintf("Successfully uploaded component archive to %q", ref))
11695

117-
for _, tag := range o.AdditionalTags {
118-
ref, err := components.OCIRef(ca.ComponentDescriptor.GetEffectiveRepositoryContext(), ca.ComponentDescriptor.GetName(), tag)
96+
err = ctfArchive.Walk(func(ca *ctf.ComponentArchive) error {
97+
// update repository context
98+
if len(o.BaseUrl) != 0 {
99+
if err := cdv2.InjectRepositoryContext(ca.ComponentDescriptor, cdv2.NewOCIRegistryRepository(o.BaseUrl, "")); err != nil {
100+
return fmt.Errorf("unable to add repository context: %w", err)
101+
}
102+
}
103+
104+
manifest, err := cdoci.NewManifestBuilder(cache, ca).Build(ctx)
105+
if err != nil {
106+
return fmt.Errorf("unable to build oci artifact for component acrchive: %w", err)
107+
}
108+
109+
ref, err := components.OCIRef(ca.ComponentDescriptor.GetEffectiveRepositoryContext(), ca.ComponentDescriptor.GetName(), ca.ComponentDescriptor.GetVersion())
119110
if err != nil {
120111
return fmt.Errorf("unable to calculate oci ref for %q: %s", ca.ComponentDescriptor.GetName(), err.Error())
121112
}
122113
if err := ociClient.PushManifest(ctx, ref, manifest); err != nil {
123114
return fmt.Errorf("unable to upload component archive to %q: %s", ref, err.Error())
124115
}
125-
log.Info(fmt.Sprintf("Successfully tagged component archive with %q", ref))
126-
}
116+
log.Info(fmt.Sprintf("Successfully uploaded component archive to %q", ref))
117+
118+
for _, tag := range o.AdditionalTags {
119+
ref, err := components.OCIRef(ca.ComponentDescriptor.GetEffectiveRepositoryContext(), ca.ComponentDescriptor.GetName(), tag)
120+
if err != nil {
121+
return fmt.Errorf("unable to calculate oci ref for %q: %s", ca.ComponentDescriptor.GetName(), err.Error())
122+
}
123+
if err := ociClient.PushManifest(ctx, ref, manifest); err != nil {
124+
return fmt.Errorf("unable to upload component archive to %q: %s", ref, err.Error())
125+
}
126+
log.Info(fmt.Sprintf("Successfully tagged component archive with %q", ref))
127+
}
127128

128-
return nil
129-
})
130-
if err != nil {
131-
return fmt.Errorf("error while reading component archives in ctf: %w", err)
129+
return nil
130+
})
131+
if err != nil {
132+
return fmt.Errorf("error while reading component archives in ctf: %w", err)
133+
}
134+
err = ctfArchive.Close()
135+
if err != nil {
136+
return fmt.Errorf("error while closing component archives in ctf: %w", err)
137+
}
132138
}
133-
134-
return ctfArchive.Close()
139+
return nil
135140
}
136141

137142
func (o *PushOptions) Complete(args []string) error {
138-
o.CTFPath = args[0]
143+
o.CTFPaths = args
139144

140145
var err error
141146
o.OciOptions.CacheDir, err = utils.CacheDir()
@@ -152,7 +157,7 @@ func (o *PushOptions) Complete(args []string) error {
152157

153158
// Validate validates push options
154159
func (o *PushOptions) Validate() error {
155-
if len(o.CTFPath) == 0 {
160+
if len(o.CTFPaths) == 0 {
156161
return errors.New("a path to the component descriptor must be defined")
157162
}
158163
return nil

pkg/commands/ctf/push_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ var _ = Describe("Add", func() {
5858
Expect(vfs.WriteFile(testdataFs, "/auth.json", cf, os.ModePerm))
5959

6060
opts := cmd.PushOptions{
61-
CTFPath: "/component.ctf",
62-
BaseUrl: testenv.Addr + "/test",
61+
CTFPaths: []string{"/component.ctf"},
62+
BaseUrl: testenv.Addr + "/test",
6363
OciOptions: options.Options{
6464
AllowPlainHttp: false,
6565
RegistryConfigPath: "/auth.json",
@@ -117,8 +117,8 @@ var _ = Describe("Add", func() {
117117
Expect(vfs.WriteFile(testdataFs, "/auth.json", cf, os.ModePerm))
118118

119119
opts := cmd.PushOptions{
120-
CTFPath: "/component.ctf",
121-
BaseUrl: testenv.Addr + "/test",
120+
CTFPaths: []string{"/component.ctf"},
121+
BaseUrl: testenv.Addr + "/test",
122122
OciOptions: options.Options{
123123
AllowPlainHttp: false,
124124
RegistryConfigPath: "/auth.json",

0 commit comments

Comments
 (0)