Skip to content

Commit

Permalink
Increase the generate function
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
  • Loading branch information
zhouhao committed Dec 22, 2016
1 parent ad5fa6e commit 58f765f
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 280 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/oci-create-runtime-bundle
/oci-unpack
/oci-image-validate
/oci-generate
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ EPOCH_TEST_COMMIT ?= v0.2.0
TOOLS := \
oci-create-runtime-bundle \
oci-image-validate \
oci-unpack
oci-unpack \
oci-generate

default: help

Expand Down
100 changes: 100 additions & 0 deletions cmd/oci-generate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"log"
"os"

specs "github.com/opencontainers/image-spec/specs-go"
"github.com/opencontainers/image-tools/image"
"github.com/spf13/cobra"
)

// gitCommit will be the hash that the binary was built from
// and will be populated by the Makefile
var gitCommit = ""

type generateCmd struct {
stdout *log.Logger
stderr *log.Logger
version bool
}

func main() {
stdout := log.New(os.Stdout, "", 0)
stderr := log.New(os.Stderr, "", 0)

cmd := newGenerateCmd(stdout, stderr)
if err := cmd.Execute(); err != nil {
stderr.Println(err)
os.Exit(1)
}
}

func newGenerateCmd(stdout, stderr *log.Logger) *cobra.Command {
v := &generateCmd{
stdout: stdout,
stderr: stderr,
}

cmd := &cobra.Command{
Use: "generate [dest]",
Short: "Generate an OCI imageLayout",
Long: `Generate the OCI imageLayout to the destination directory [dest].`,
Run: v.Run,
}

cmd.Flags().BoolVarP(
&v.version, "version", "v", false,
`Print version information and exit`,
)

origHelp := cmd.HelpFunc()

cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
origHelp(c, args)
stdout.Println("\nMore information:")
stdout.Printf("\treferences\t%s\n", image.SpecURL)
stdout.Printf("\tbug report\t%s\n", image.IssuesURL)
})

return cmd
}

func (v *generateCmd) Run(cmd *cobra.Command, args []string) {
if v.version {
v.stdout.Printf("commit: %s", gitCommit)
v.stdout.Printf("spec: %s", specs.Version)
os.Exit(0)
}

if len(args) != 1 {
v.stderr.Print("dest must be provided")
if err := cmd.Usage(); err != nil {
v.stderr.Println(err)
}
os.Exit(1)
}

err := image.Generate(args[0])

if err != nil {
v.stderr.Printf("generating failed: %v", err)
os.Exit(1)
}

os.Exit(0)
}
Loading

0 comments on commit 58f765f

Please sign in to comment.