Skip to content

Commit fab1765

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

File tree

10 files changed

+51
-81
lines changed

10 files changed

+51
-81
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/create-runtime-bundle.go

Lines changed: 1 addition & 23 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,
@@ -105,12 +89,6 @@ It is strongly recommended to keep the default value.`,
10589
}
10690

10791
func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {
108-
if v.version {
109-
v.stdout.Printf("commit: %s", gitCommit)
110-
v.stdout.Printf("spec: %s", specs.Version)
111-
os.Exit(0)
112-
113-
}
11492
if len(args) != 2 {
11593
v.stderr.Print("both src and dest must be provided")
11694
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+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

cmd/oci-unpack/main.go renamed to cmd/oci-image-tool/unpack.go

Lines changed: 0 additions & 22 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,
@@ -98,12 +82,6 @@ func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command {
9882
}
9983

10084
func (v *unpackCmd) Run(cmd *cobra.Command, args []string) {
101-
if v.version {
102-
v.stdout.Printf("commit: %s", gitCommit)
103-
v.stdout.Printf("spec: %s", specs.Version)
104-
os.Exit(0)
105-
}
106-
10785
if len(args) != 2 {
10886
v.stderr.Print("both src and dest must be provided")
10987
if err := cmd.Usage(); err != nil {

cmd/oci-image-validate/main.go renamed to cmd/oci-image-tool/validate.go

Lines changed: 1 addition & 23 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
}
@@ -102,12 +86,6 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
10286
}
10387

10488
func (v *validateCmd) Run(cmd *cobra.Command, args []string) {
105-
if v.version {
106-
v.stdout.Printf("commit: %s", gitCommit)
107-
v.stdout.Printf("spec: %s", specs.Version)
108-
os.Exit(0)
109-
}
110-
11189
if len(args) < 1 {
11290
v.stderr.Printf("no files specified")
11391
if err := cmd.Usage(); err != nil {

0 commit comments

Comments
 (0)