Skip to content

Commit 59ec62f

Browse files
author
zhouhao
committed
Increase the generate function
Signed-off-by: zhouhao <[email protected]>
1 parent 7575a09 commit 59ec62f

File tree

7 files changed

+579
-280
lines changed

7 files changed

+579
-280
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/oci-create-runtime-bundle
2-
/oci-unpack
2+
/oci-generate
33
/oci-image-validate
4+
/oci-unpack

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true)
66
EPOCH_TEST_COMMIT ?= v0.2.0
77
TOOLS := \
88
oci-create-runtime-bundle \
9+
oci-generate \
910
oci-image-validate \
1011
oci-unpack
12+
1113
MAN := $(TOOLS:%=%.1)
1214

1315
default: help

cmd/oci-generate/main.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
"fmt"
19+
"log"
20+
"os"
21+
"strings"
22+
23+
specs "github.com/opencontainers/image-spec/specs-go"
24+
"github.com/opencontainers/image-tools/image"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
// gitCommit will be the hash that the binary was built from
29+
// and will be populated by the Makefile
30+
var gitCommit = ""
31+
32+
var generateTypes = []string{
33+
"imageLayout",
34+
"image",
35+
}
36+
37+
type generateCmd struct {
38+
stdout *log.Logger
39+
stderr *log.Logger
40+
typ string //the type to generate, can be empty string
41+
version bool
42+
}
43+
44+
func main() {
45+
stdout := log.New(os.Stdout, "", 0)
46+
stderr := log.New(os.Stderr, "", 0)
47+
48+
cmd := newGenerateCmd(stdout, stderr)
49+
if err := cmd.Execute(); err != nil {
50+
stderr.Println(err)
51+
os.Exit(1)
52+
}
53+
}
54+
55+
func newGenerateCmd(stdout, stderr *log.Logger) *cobra.Command {
56+
v := &generateCmd{
57+
stdout: stdout,
58+
stderr: stderr,
59+
}
60+
61+
cmd := &cobra.Command{
62+
Use: "generate [dest]",
63+
Short: "Generate an image or an imageLayout",
64+
Long: `Generate the OCI iamge or imageLayout to the destination directory [dest].`,
65+
Run: v.Run,
66+
}
67+
68+
cmd.Flags().StringVar(
69+
&v.typ, "type", "imageLayout",
70+
fmt.Sprintf(
71+
`Type of the file to generate, one of "%s".`,
72+
strings.Join(generateTypes, ","),
73+
),
74+
)
75+
76+
cmd.Flags().BoolVarP(
77+
&v.version, "version", "v", false,
78+
`Print version information and exit`,
79+
)
80+
81+
origHelp := cmd.HelpFunc()
82+
83+
cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
84+
origHelp(c, args)
85+
stdout.Println("\nMore information:")
86+
stdout.Printf("\treferences\t%s\n", image.SpecURL)
87+
stdout.Printf("\tbug report\t%s\n", image.IssuesURL)
88+
})
89+
90+
return cmd
91+
}
92+
93+
func (v *generateCmd) Run(cmd *cobra.Command, args []string) {
94+
if v.version {
95+
v.stdout.Printf("commit: %s", gitCommit)
96+
v.stdout.Printf("spec: %s", specs.Version)
97+
os.Exit(0)
98+
}
99+
100+
if len(args) != 1 {
101+
v.stderr.Print("dest must be provided")
102+
if err := cmd.Usage(); err != nil {
103+
v.stderr.Println(err)
104+
}
105+
os.Exit(1)
106+
}
107+
108+
var err error
109+
switch v.typ {
110+
case "imageLayout":
111+
err = image.GenerateLayout(args[0])
112+
case "image":
113+
err = image.Generate(args[0])
114+
default:
115+
err = fmt.Errorf("unsupport type %q", v.typ)
116+
}
117+
118+
if err != nil {
119+
v.stderr.Printf("generating failed: %v", err)
120+
os.Exit(1)
121+
}
122+
123+
os.Exit(0)
124+
}

cmd/oci-generate/oci-generate.1.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
% OCI(1) OCI-UNPACK User Manuals
2+
% OCI Community
3+
% JULY 2016
4+
# NAME
5+
oci-generate \- Generate an OCI image or an OCI imageLayout
6+
7+
# SYNOPSIS
8+
**oci-unpack** [dest] [flags]
9+
**oci-unpack** [--help|--version]
10+
11+
# DESCRIPTION
12+
`oci-generate` generate an application/vnd.oci.image.manifest.v1+json or application/vnd.oci.image.manifest.list+json to `dest`.
13+
14+
# OPTIONS
15+
**--help**
16+
Print usage statement
17+
18+
**--type**
19+
Type of the file to generate.One of "imageLayout,image".(detault "imageLayout")
20+
21+
**--version**
22+
Print version information and exit.
23+
24+
# EXAMPLES
25+
```
26+
$ oci-generate example
27+
$ tree example
28+
example
29+
├── blobs
30+
│   └── sha256
31+
│   ├── 5d82e6cbf19cd18f40301df14ffbd62ba1acae8a097e75011ec603ff38a7450a
32+
│   ├── 89b46f9224fbcedd165dd6b317329b69182471c6706993bedc2c2d2fd2d76fba
33+
│   └── a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
34+
├── oci-layout
35+
└── refs
36+
└── latest
37+
[...]
38+
```
39+
# HISTORY
40+
Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com)

0 commit comments

Comments
 (0)