Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase the generate function #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions cmd/oci-generate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 (
"fmt"
"log"
"os"
"strings"

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 = ""

var generateTypes = []string{
"imageLayout",
"image",
}

type generateCmd struct {
stdout *log.Logger
stderr *log.Logger
typ string //the type to generate, can be empty string
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 image or an imageLayout",
Long: `Generate the OCI iamge or imageLayout to the destination directory [dest].`,
Run: v.Run,
}

cmd.Flags().StringVar(
&v.typ, "type", "imageLayout",
fmt.Sprintf(
`Type of the file to generate, one of "%s".`,
strings.Join(generateTypes, ","),
),
)

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)
}

var err error
switch v.typ {
case "imageLayout":
err = image.GenerateLayout(args[0])
case "image":
err = image.Generate(args[0])
default:
err = fmt.Errorf("unsupport type %q", v.typ)
}

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

os.Exit(0)
}
40 changes: 40 additions & 0 deletions cmd/oci-generate/oci-generate.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
% OCI(1) OCI-UNPACK User Manuals
% OCI Community
% JULY 2016
# NAME
oci-generate \- Generate an OCI image or an OCI imageLayout

# SYNOPSIS
**oci-unpack** [dest] [flags]
**oci-unpack** [--help|--version]

# DESCRIPTION
`oci-generate` generate an application/vnd.oci.image.manifest.v1+json or application/vnd.oci.image.manifest.list+json to `dest`.

# OPTIONS
**--help**
Print usage statement

**--type**
Type of the file to generate.One of "imageLayout,image".(detault "imageLayout")

**--version**
Print version information and exit.

# EXAMPLES
```
$ oci-generate example
$ tree example
example
├── blobs
│   └── sha256
│   ├── 5d82e6cbf19cd18f40301df14ffbd62ba1acae8a097e75011ec603ff38a7450a
│   ├── 89b46f9224fbcedd165dd6b317329b69182471c6706993bedc2c2d2fd2d76fba
│   └── a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
├── oci-layout
└── refs
└── latest
[...]
```
# HISTORY
Sept 2016, Originally compiled by Antonio Murdaca (runcom at redhat dot com)
Loading