Skip to content

Commit 932dc8b

Browse files
committed
image/refs: Implement Engine.Put
This is pretty straightforward with the new WriteTarEntryByName helper. I considered pulling the ref name -> path conversion (%s -> ./refs/%s) out into a helper function to stay DRY, but the logic is simple enough that it seemed more intuitive to leave it inline. Signed-off-by: W. Trevor King <[email protected]>
1 parent 5271cb6 commit 932dc8b

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

cmd/oci-image-tool/refs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func newRefsCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
2727
Short: "Name-based reference manipulation",
2828
}
2929

30+
cmd.AddCommand(newRefsPutCmd(stdout, stderr))
3031
cmd.AddCommand(newRefsGetCmd(stdout, stderr))
3132
cmd.AddCommand(newRefsListCmd(stdout, stderr))
3233

cmd/oci-image-tool/refs_put.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"encoding/json"
19+
"io"
20+
"log"
21+
"os"
22+
23+
"github.com/opencontainers/image-spec/image/refs/layout"
24+
"github.com/opencontainers/image-spec/specs-go"
25+
"github.com/spf13/cobra"
26+
"golang.org/x/net/context"
27+
)
28+
29+
type refsPutCmd struct {
30+
stdout io.Writer
31+
stderr *log.Logger
32+
path string
33+
name string
34+
}
35+
36+
func newRefsPutCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
37+
state := &refsPutCmd{
38+
stdout: stdout,
39+
stderr: stderr,
40+
}
41+
42+
return &cobra.Command{
43+
Use: "put PATH NAME",
44+
Short: "Write a reference to the store",
45+
Long: "Read descriptor JSON from stdin and write it to the store.",
46+
Run: state.Run,
47+
}
48+
}
49+
50+
func (state *refsPutCmd) Run(cmd *cobra.Command, args []string) {
51+
if len(args) != 2 {
52+
if err := cmd.Usage(); err != nil {
53+
state.stderr.Println(err)
54+
}
55+
os.Exit(1)
56+
}
57+
58+
state.path = args[0]
59+
state.name = args[1]
60+
61+
err := state.run()
62+
if err != nil {
63+
state.stderr.Println(err)
64+
os.Exit(1)
65+
}
66+
67+
os.Exit(0)
68+
}
69+
70+
func (state *refsPutCmd) run() (err error) {
71+
ctx := context.Background()
72+
73+
engine, err := layout.NewEngine(ctx, state.path)
74+
if err != nil {
75+
return err
76+
}
77+
defer engine.Close()
78+
79+
decoder := json.NewDecoder(os.Stdin)
80+
var descriptor specs.Descriptor
81+
err = decoder.Decode(&descriptor)
82+
if err != nil {
83+
return err
84+
}
85+
86+
return engine.Put(ctx, state.name, &descriptor)
87+
}

image/refs/layout/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
// NewEngine instantiates an engine with the appropriate backend (tar,
2929
// HTTP, ...).
3030
func NewEngine(ctx context.Context, path string) (engine refs.Engine, err error) {
31-
file, err := os.Open(path)
31+
file, err := os.OpenFile(path, os.O_RDWR, 0)
3232
if err != nil {
3333
return nil, err
3434
}

image/refs/layout/tar.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package layout
1616

1717
import (
1818
"archive/tar"
19+
"bytes"
1920
"encoding/json"
2021
"errors"
2122
"fmt"
@@ -51,8 +52,15 @@ func NewTarEngine(ctx context.Context, file caslayout.ReadWriteSeekCloser) (eng
5152

5253
// Put adds a new reference to the store.
5354
func (engine *TarEngine) Put(ctx context.Context, name string, descriptor *specs.Descriptor) (err error) {
54-
// FIXME
55-
return errors.New("TarEngine.Put is not supported yet")
55+
data, err := json.Marshal(descriptor)
56+
if err != nil {
57+
return err
58+
}
59+
60+
size := int64(len(data))
61+
reader := bytes.NewReader(data)
62+
targetName := fmt.Sprintf("./refs/%s", name)
63+
return imagelayout.WriteTarEntryByName(ctx, engine.file, targetName, reader, &size)
5664
}
5765

5866
// Get returns a reference from the store.

0 commit comments

Comments
 (0)