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 #5

Open
wants to merge 14 commits 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/oci-cas
/oci-create-runtime-bundle
Copy link
Member

Choose a reason for hiding this comment

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

Why did you remove this from .gitignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's down below, I just alphabetized it.

/oci-unpack
/oci-image-init
/oci-image-validate
/oci-refs
/oci-unpack
6 changes: 6 additions & 0 deletions .tool/lint
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ 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-cas/delete.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-cas/get.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-refs/delete.go:.* duplicate of .* \(dupl\)$' \
--exclude='^cmd/oci-refs/get.go:.* duplicate of .* \(dupl\)$' \
--exclude='^image/cas/layout/dir.go:.* duplicate of .* \(dupl\)$' \
--exclude='^image/refs/layout/dir.go:.* duplicate of .* \(dupl\)$' \
--exclude='schema/fs.go' \
--exclude='duplicate of.*main.go.*\(dupl\)$' \
--disable=aligncheck \
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true)

EPOCH_TEST_COMMIT ?= v0.2.0
TOOLS := \
oci-cas \
oci-create-runtime-bundle \
oci-image-init \
oci-image-validate \
oci-refs \
oci-unpack

default: help
Expand Down
72 changes: 72 additions & 0 deletions cmd/oci-cas/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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"
"os"

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

type deleteCmd struct {
path string
digest string
}

func newDeleteCmd() *cobra.Command {
state := &deleteCmd{}

return &cobra.Command{
Use: "delete PATH DIGEST",
Short: "Remove a blob from from the store",
Run: state.Run,
}
}

func (state *deleteCmd) Run(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "both PATH and DIGEST must be provided")
if err := cmd.Usage(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}

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

err := state.run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

os.Exit(0)
}

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

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

return engine.Delete(ctx, state.digest)
}
93 changes: 93 additions & 0 deletions cmd/oci-cas/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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/ioutil"
"os"

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

type getCmd struct {
path string
digest string
}

func newGetCmd() *cobra.Command {
state := &getCmd{}

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 *getCmd) Run(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "both PATH and DIGEST must be provided")
if err := cmd.Usage(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}

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

err := state.run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

os.Exit(0)
}

func (state *getCmd) 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 := os.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
}
39 changes: 39 additions & 0 deletions cmd/oci-cas/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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"
"os"

"github.com/spf13/cobra"
)

func main() {
cmd := &cobra.Command{
Use: "oci-cas",
Short: "Content-addressable storage manipulation",
}

cmd.AddCommand(newGetCmd())
cmd.AddCommand(newPutCmd())
cmd.AddCommand(newDeleteCmd())

err := cmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
27 changes: 27 additions & 0 deletions cmd/oci-cas/oci-cas-delete.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
% OCI(1) OCI-IMAGE-TOOL User Manuals
% OCI Community
% SEPTEMBER 2016
# NAME

oci-cas-get \- Remove a blob from the store

# SYNOPSIS

**oci-cas delete** [OPTIONS] PATH DIGEST

# DESCRIPTION

`oci-cas delete` removes the blob referenced by `DIGEST` from the store at `PATH`.

# OPTIONS

**--help**
Print usage statement

# SEE ALSO

**oci-cas**(1), **oci-cas-get**(1), **oci-cas-put**(1)

# HISTORY

September 2016, Originally compiled by W. Trevor King (wking at tremily dot us)
27 changes: 27 additions & 0 deletions cmd/oci-cas/oci-cas-get.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
% OCI(1) OCI-IMAGE-TOOL User Manuals
% OCI Community
% AUGUST 2016
# NAME

oci-cas-get \- Retrieve a blob from the store

# SYNOPSIS

**oci-cas get** [OPTIONS] PATH DIGEST

# DESCRIPTION

`oci-cas get` retrieves a blob referenced by `DIGEST` from the store at `PATH` and writes it to standard output.

# OPTIONS

**--help**
Print usage statement

# SEE ALSO

**oci-cas**(1), **oci-cas-put**(1), **oci-cas-delete**(1)

# HISTORY

August 2016, Originally compiled by W. Trevor King (wking at tremily dot us)
27 changes: 27 additions & 0 deletions cmd/oci-cas/oci-cas-put.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
% OCI(1) OCI-IMAGE-TOOL User Manuals
% OCI Community
% AUGUST 2016
# NAME

oci-cas-put \- Write a blob to the store

# SYNOPSIS

**oci-cas put** [OPTIONS] PATH

# DESCRIPTION

`oci-cas put` reads a blob from stdin, writes it to the store at `PATH`, and prints the digest to standard output.

# OPTIONS

**--help**
Print usage statement

# SEE ALSO

**oci-cas**(1), **oci-cas-get**(1), **oci-cas-delete**(1)

# HISTORY

August 2016, Originally compiled by W. Trevor King (wking at tremily dot us)
52 changes: 52 additions & 0 deletions cmd/oci-cas/oci-cas.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
% OCI(1) OCI-User Manuals
% OCI Community
% AUGUST 2016
# NAME

oci-cas \- Content-addressable storage manipulation

# SYNOPSIS

**oci-cas** [command]

# DESCRIPTION

`oci-cas` manipulates content-addressable storage.

# OPTIONS

**--help**
Print usage statement

# COMMANDS

**get**
Retrieve a blob from the store.
See **oci-cas-get**(1) for full documentation on the **get** command.

**put**
Write a blob to the store.
See **oci-cas-put**(1) for full documentation on the **put** command.

**delete**
Remove a blob from the store.
See **oci-cas-delete**(1) for full documentation on the **delete** command.

# EXAMPLES

```
$ oci-image-init image-layout image
$ echo hello | oci-cas put image
sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
$ oci-cas get image sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
hello
$ oci-cas delete image sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
```

# SEE ALSO

**oci-image-tools**(7), **oci-cas-get**(1), **oci-cas-put**(1), **oci-cas-delete**(1), **oci-image-init**(1)

# HISTORY

August 2016, Originally compiled by W. Trevor King (wking at tremily dot us)
Loading