-
Notifications
You must be signed in to change notification settings - Fork 694
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
Changes from all commits
f01c6a0
2d55583
f2dc0e2
4c68e76
002f708
89a36d8
1696995
c92ec50
469bbb6
be239fd
87d6a53
b9becbd
7e96d03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just call this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There are a few things in that line. Do you mean ‘Run’ → ‘cmd’? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for being inprecise. I meant the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
What do you want me to use for *cobra.Command? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
getCmd would cause collisions between the current casGetCmd and |
||
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 | ||
} |
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 | ||
} |
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 | ||
} |
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) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
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.