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

image: Refactor to use cas/ref engines instead of walkers #159

Closed
wants to merge 13 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .tool/lint
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ for d in $(find . -type d -not -iwholename '*.git*' -a -not -iname '.tool' -a -n
--exclude='error return value not checked.*(Close|Log|Print).*\(errcheck\)$' \
--exclude='.*_test\.go:.*error return value not checked.*\(errcheck\)$' \
--exclude='duplicate of.*_test.go.*\(dupl\)$' \
--exclude='^cmd/oci-image-tool/cas_get.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-image-tool/cas_put.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-image-tool/refs_get.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-image-tool/refs_list.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-image-tool/refs_put.go:.* duplicate of .* \(dupl\)$' \
--exclude='schema/fs.go' \
--disable=aligncheck \
--disable=gotype \
Expand Down
3 changes: 1 addition & 2 deletions cmd/oci-image-tool/autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

// supported autodetection types
const (
typeImageLayout = "imageLayout"
typeImage = "image"
typeManifest = "manifest"
typeManifestList = "manifestList"
Expand All @@ -43,7 +42,7 @@ func autodetect(path string) (string, error) {
}

if fi.IsDir() {
return typeImageLayout, nil
return typeImage, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right to just remove. Was this not correct before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Mon, Aug 29, 2016 at 06:52:32PM -0700, Stephen Day wrote:

@@ -43,7 +42,7 @@ func autodetect(path string) (string, error) {
}

if fi.IsDir() {
  •   return typeImageLayout, nil
    

This doesn't seem right to just remove. Was this not correct before?

The new code handles the distinction between typeImageLayout and
typeImage (which is whether the target is a directory or tarball)
inside the NewEngine constructors [1,2]. I haven't added support for
the directory-based engines yet, since there was already enough going
on. But I'm happy to add them if it would help with review.


f, err := os.Open(path)
Expand Down
34 changes: 34 additions & 0 deletions cmd/oci-image-tool/cas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 (
"io"
"log"

"github.com/spf13/cobra"
)

func newCASCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "cas",
Short: "Content-addressable storage manipulation",
}

cmd.AddCommand(newCASGetCmd(stdout, stderr))
cmd.AddCommand(newCASPutCmd(stdout, stderr))

return cmd
}
100 changes: 100 additions & 0 deletions cmd/oci-image-tool/cas_get.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 (
"fmt"
"io"
"io/ioutil"
"log"
"os"

"github.com/opencontainers/image-spec/image/cas/layout"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

type casGetCmd struct {
stdout io.Writer
stderr *log.Logger
path string
digest string
}

func newCASGetCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
state := &casGetCmd{
stdout: stdout,
stderr: stderr,
}

return &cobra.Command{
Use: "get PATH DIGEST",
Short: "Retrieve a blob from the store",
Long: "Retrieve a blob from the store and write it to stdout.",
Run: state.Run,
}
}

func (state *casGetCmd) Run(cmd *cobra.Command, args []string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just call this cmd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Mon, Jun 20, 2016 at 05:30:34AM -0700, Sergiusz Urbaniak wrote:

+func (state *casGetCmd) Run(cmd *cobra.Command, args []string) {

just call this cmd

There are a few things in that line. Do you mean ‘Run’ → ‘cmd’?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being inprecise. I meant the state variable, I'd call this cmd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Mon, Jun 20, 2016 at 02:09:20PM -0700, Sergiusz Urbaniak wrote:

+func (state *casGetCmd) Run(cmd *cobra.Command, args []string) {

Sorry for being inprecise. I meant the state variable, I'd call this cmd.

What do you want me to use for *cobra.Command?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, sorry, it's 11pm here in old Europe, my eyes are starting to blur ;-) In this case I'd recommend calling it getCmd, or even cgc which is quite idiomatic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Mon, Jun 20, 2016 at 02:19:47PM -0700, Sergiusz Urbaniak wrote:

+func (state *casGetCmd) Run(cmd *cobra.Command, args []string) {

Haha, sorry, it's 11pm here in old Europe, my eyes are starting to
blur ;-) In this case I'd recommend calling it getCmd, or even
cgc which is quite idiomatic.

getCmd would cause collisions between the current casGetCmd and
refsGetCmd. Do you really prefer types ‘cgc’ and ‘rgc’ to casGetCmt
and refsGetCmd? I can reroll (it's not my project ;), but the short
versions are a bit dense for my taste ;).

if len(args) != 2 {
state.stderr.Print("both PATH and DIGEST must be provided")
if err := cmd.Usage(); err != nil {
state.stderr.Println(err)
}
os.Exit(1)
}

state.path = args[0]
state.digest = args[1]

err := state.run()
if err != nil {
state.stderr.Println(err)
os.Exit(1)
}

os.Exit(0)
}

func (state *casGetCmd) run() (err error) {
ctx := context.Background()

engine, err := layout.NewEngine(ctx, state.path)
if err != nil {
return err
}
defer engine.Close()

reader, err := engine.Get(ctx, state.digest)
if err != nil {
return err
}
defer reader.Close()

bytes, err := ioutil.ReadAll(reader)
if err != nil {
return err
}

n, err := state.stdout.Write(bytes)
if err != nil {
return err
}
if n < len(bytes) {
return fmt.Errorf("wrote %d of %d bytes", n, len(bytes))
}

return nil
}
90 changes: 90 additions & 0 deletions cmd/oci-image-tool/cas_put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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"
"io"
"log"
"os"

"github.com/opencontainers/image-spec/image/cas/layout"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

type casPutCmd struct {
stdout io.Writer
stderr *log.Logger
path string
}

func newCASPutCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
state := &casPutCmd{
stdout: stdout,
stderr: stderr,
}

return &cobra.Command{
Use: "put PATH",
Short: "Write a blob to the store",
Long: "Read a blob from stdin, write it to the store, and print the digest to stdout.",
Run: state.Run,
}
}

func (state *casPutCmd) Run(cmd *cobra.Command, args []string) {
if len(args) != 1 {
if err := cmd.Usage(); err != nil {
state.stderr.Println(err)
}
os.Exit(1)
}

state.path = args[0]

err := state.run()
if err != nil {
state.stderr.Println(err)
os.Exit(1)
}

os.Exit(0)
}

func (state *casPutCmd) run() (err error) {
ctx := context.Background()

engine, err := layout.NewEngine(ctx, state.path)
if err != nil {
return err
}
defer engine.Close()

digest, err := engine.Put(ctx, os.Stdin)
if err != nil {
return err
}

n, err := fmt.Fprintln(state.stdout, digest)
if err != nil {
return err
}
if n < len(digest) {
return fmt.Errorf("wrote %d of %d bytes", n, len(digest))
}

return nil
}
9 changes: 4 additions & 5 deletions cmd/oci-image-tool/create_runtime_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (

"github.com/opencontainers/image-spec/image"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

// supported bundle types
var bundleTypes = []string{
typeImageLayout,
typeImage,
}

Expand Down Expand Up @@ -82,6 +82,8 @@ func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {
os.Exit(1)
}

ctx := context.Background()

if _, err := os.Stat(args[1]); os.IsNotExist(err) {
v.stderr.Printf("destination path %s does not exist", args[1])
os.Exit(1)
Expand All @@ -98,11 +100,8 @@ func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {

var err error
switch v.typ {
case typeImageLayout:
err = image.CreateRuntimeBundleLayout(args[0], args[1], v.ref, v.root)

case typeImage:
err = image.CreateRuntimeBundle(args[0], args[1], v.ref, v.root)
err = image.CreateRuntimeBundle(ctx, args[0], args[1], v.ref, v.root)
}

if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions cmd/oci-image-tool/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 (
"io"
"log"

"github.com/spf13/cobra"
)

func newInitCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize an OCI image",
}

cmd.AddCommand(newInitImageLayoutCmd(stdout, stderr))

return cmd
}
62 changes: 62 additions & 0 deletions cmd/oci-image-tool/init_image_layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 (
"io"
"log"
"os"

"github.com/opencontainers/image-spec/image/layout"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

type initImageLayout struct {
stderr *log.Logger
}

func newInitImageLayoutCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
state := &initImageLayout{
stderr: stderr,
}

return &cobra.Command{
Use: "image-layout PATH",
Short: "Initialize an OCI image-layout repository",
Run: state.Run,
}
}

func (state *initImageLayout) Run(cmd *cobra.Command, args []string) {
if len(args) != 1 {
if err := cmd.Usage(); err != nil {
state.stderr.Println(err)
}
os.Exit(1)
}

path := args[0]

ctx := context.Background()

err := layout.CreateTarFile(ctx, path)
if err != nil {
state.stderr.Println(err)
os.Exit(1)
}

os.Exit(0)
}
Loading