Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit 2c7d8d8

Browse files
author
zhouhao
committed
merging of subcommands
Signed-off-by: zhouhao <[email protected]>
1 parent b809c3b commit 2c7d8d8

File tree

10 files changed

+51
-94
lines changed

10 files changed

+51
-94
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
/oci-create-runtime-bundle
2-
/oci-unpack
3-
/oci-image-validate
1+
/oci-image-tool

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ script:
2121
- make lint
2222
- make check-license
2323
- make test
24-
- make tools
24+
- make oci-image-tool

Makefile

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ TOOLS := \
88
oci-create-runtime-bundle \
99
oci-image-validate \
1010
oci-unpack
11-
1211
default: help
1312

1413
help:
1514
@echo "Usage: make <target>"
1615
@echo
17-
@echo " * 'tools' - Build the oci image tools binaries"
16+
@echo " * 'oci-image-tool' - Build the oci-image-tool binary"
1817
@echo " * 'check-license' - Check license headers in source files"
1918
@echo " * 'lint' - Execute the source code linter"
2019
@echo " * 'test' - Execute the unit tests"
@@ -24,10 +23,8 @@ check-license:
2423
@echo "checking license headers"
2524
@./.tool/check-license
2625

27-
tools: $(TOOLS)
28-
29-
$(TOOLS): oci-%:
30-
go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/$@
26+
oci-image-tool:
27+
go build ./cmd/oci-image-tool
3128

3229
lint:
3330
@echo "checking lint"
@@ -73,11 +70,11 @@ install.tools: .install.gitvalidation .install.glide .install.glide-vc .install.
7370
gometalinter --install --update
7471

7572
clean:
76-
rm -rf *~ $(OUTPUT_DIRNAME) $(TOOLS)
73+
rm -rf *~ $(OUTPUT_DIRNAME)
74+
rm -fr oci-image-tool
7775

7876
.PHONY: \
79-
tools \
80-
$(TOOLS) \
77+
oci-image-tool \
8178
check-license \
8279
clean \
8380
lint \

cmd/oci-create-runtime-bundle/main.go renamed to cmd/oci-image-tool/cerate-runtime-bundle.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@ import (
2020
"os"
2121
"strings"
2222

23-
specs "github.com/opencontainers/image-spec/specs-go"
2423
"github.com/opencontainers/image-tools/image"
2524
"github.com/spf13/cobra"
2625
)
2726

28-
// gitCommit will be the hash that the binary was built from
29-
// and will be populated by the Makefile
30-
var gitCommit = ""
31-
3227
// supported bundle types
3328
var bundleTypes = []string{
3429
image.TypeImageLayout,
@@ -44,25 +39,14 @@ type bundleCmd struct {
4439
version bool
4540
}
4641

47-
func main() {
48-
stdout := log.New(os.Stdout, "", 0)
49-
stderr := log.New(os.Stderr, "", 0)
50-
51-
cmd := newBundleCmd(stdout, stderr)
52-
if err := cmd.Execute(); err != nil {
53-
stderr.Println(err)
54-
os.Exit(1)
55-
}
56-
}
57-
5842
func newBundleCmd(stdout, stderr *log.Logger) *cobra.Command {
5943
v := &bundleCmd{
6044
stdout: stdout,
6145
stderr: stderr,
6246
}
6347

6448
cmd := &cobra.Command{
65-
Use: "oci-create-runtime-bundle [src] [dest]",
49+
Use: "create-runtime-bundle [src] [dest]",
6650
Short: "Create an OCI image runtime bundle",
6751
Long: `Creates an OCI image runtime bundle at the destination directory [dest] from an OCI image present at [src].`,
6852
Run: v.Run,
@@ -87,20 +71,10 @@ func newBundleCmd(stdout, stderr *log.Logger) *cobra.Command {
8771
It is strongly recommended to keep the default value.`,
8872
)
8973

90-
cmd.Flags().BoolVarP(
91-
&v.version, "version", "v", false,
92-
`Print version information and exit`,
93-
)
9474
return cmd
9575
}
9676

9777
func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {
98-
if v.version {
99-
v.stdout.Printf("commit: %s", gitCommit)
100-
v.stdout.Printf("spec: %s", specs.Version)
101-
os.Exit(0)
102-
103-
}
10478
if len(args) != 2 {
10579
v.stderr.Print("both src and dest must be provided")
10680
if err := cmd.Usage(); err != nil {

cmd/oci-image-tool/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"log"
19+
"os"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
func main() {
25+
cmd := &cobra.Command{
26+
Use: "oci-image-tool",
27+
Short: "A tool for working with OCI images",
28+
}
29+
30+
stdout := log.New(os.Stdout, "", 0)
31+
stderr := log.New(os.Stderr, "", 0)
32+
33+
cmd.AddCommand(newValidateCmd(stdout, stderr))
34+
cmd.AddCommand(newUnpackCmd(stdout, stderr))
35+
cmd.AddCommand(newBundleCmd(stdout, stderr))
36+
37+
if err := cmd.Execute(); err != nil {
38+
stderr.Println(err)
39+
os.Exit(1)
40+
}
41+
}

cmd/oci-create-runtime-bundle/oci-create-runtime-bundle.1.md renamed to cmd/oci-image-tool/man/oci-create-runtime-bundle.1.md

File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@ import (
2020
"os"
2121
"strings"
2222

23-
specs "github.com/opencontainers/image-spec/specs-go"
2423
"github.com/opencontainers/image-tools/image"
2524
"github.com/spf13/cobra"
2625
)
2726

28-
// gitCommit will be the hash that the binary was built from
29-
// and will be populated by the Makefile
30-
var gitCommit = ""
31-
3227
// supported unpack types
3328
var unpackTypes = []string{
3429
image.TypeImageLayout,
@@ -43,17 +38,6 @@ type unpackCmd struct {
4338
version bool
4439
}
4540

46-
func main() {
47-
stdout := log.New(os.Stdout, "", 0)
48-
stderr := log.New(os.Stderr, "", 0)
49-
50-
cmd := newUnpackCmd(stdout, stderr)
51-
if err := cmd.Execute(); err != nil {
52-
stderr.Println(err)
53-
os.Exit(1)
54-
}
55-
}
56-
5741
func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command {
5842
v := &unpackCmd{
5943
stdout: stdout,
@@ -80,20 +64,10 @@ func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command {
8064
`The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image.`,
8165
)
8266

83-
cmd.Flags().BoolVarP(
84-
&v.version, "version", "v", false,
85-
`Print version information and exit`,
86-
)
8767
return cmd
8868
}
8969

9070
func (v *unpackCmd) Run(cmd *cobra.Command, args []string) {
91-
if v.version {
92-
v.stdout.Printf("commit: %s", gitCommit)
93-
v.stdout.Printf("spec: %s", specs.Version)
94-
os.Exit(0)
95-
}
96-
9771
if len(args) != 2 {
9872
v.stderr.Print("both src and dest must be provided")
9973
if err := cmd.Usage(); err != nil {
Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ import (
2121
"strings"
2222

2323
"github.com/opencontainers/image-spec/schema"
24-
specs "github.com/opencontainers/image-spec/specs-go"
2524
"github.com/opencontainers/image-tools/image"
2625
"github.com/pkg/errors"
2726
"github.com/spf13/cobra"
2827
)
2928

30-
// gitCommit will be the hash that the binary was built from
31-
// and will be populated by the Makefile
32-
var gitCommit = ""
33-
3429
// supported validation types
3530
var validateTypes = []string{
3631
image.TypeImageLayout,
@@ -48,25 +43,14 @@ type validateCmd struct {
4843
version bool
4944
}
5045

51-
func main() {
52-
stdout := log.New(os.Stdout, "", 0)
53-
stderr := log.New(os.Stderr, "", 0)
54-
55-
cmd := newValidateCmd(stdout, stderr)
56-
if err := cmd.Execute(); err != nil {
57-
stderr.Println(err)
58-
os.Exit(1)
59-
}
60-
}
61-
6246
func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
6347
v := &validateCmd{
6448
stdout: stdout,
6549
stderr: stderr,
6650
}
6751

6852
cmd := &cobra.Command{
69-
Use: "oci-image-validate FILE...",
53+
Use: "validate FILE...",
7054
Short: "Validate one or more image files",
7155
Run: v.Run,
7256
}
@@ -84,21 +68,10 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
8468
`A set of refs pointing to the manifests to be validated. Each reference must be present in the "refs" subdirectory of the image. Only applicable if type is image or imageLayout.`,
8569
)
8670

87-
cmd.Flags().BoolVarP(
88-
&v.version, "version", "v", false,
89-
`Print version information and exit`,
90-
)
91-
9271
return cmd
9372
}
9473

9574
func (v *validateCmd) Run(cmd *cobra.Command, args []string) {
96-
if v.version {
97-
v.stdout.Printf("commit: %s", gitCommit)
98-
v.stdout.Printf("spec: %s", specs.Version)
99-
os.Exit(0)
100-
}
101-
10275
if len(args) < 1 {
10376
v.stderr.Printf("no files specified")
10477
if err := cmd.Usage(); err != nil {

0 commit comments

Comments
 (0)