Skip to content

Commit d2c026b

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 7ea54f2 commit d2c026b

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

cmd/oci-refs/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func main() {
2727
Short: "Name-based reference manipulation",
2828
}
2929

30+
cmd.AddCommand(newPutCmd())
3031
cmd.AddCommand(newGetCmd())
3132
cmd.AddCommand(newListCmd())
3233

cmd/oci-refs/put.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
"fmt"
20+
"os"
21+
22+
"github.com/opencontainers/image-spec/specs-go"
23+
"github.com/opencontainers/image-tools/image/refs/layout"
24+
"github.com/spf13/cobra"
25+
"golang.org/x/net/context"
26+
)
27+
28+
type putCmd struct {
29+
path string
30+
name string
31+
}
32+
33+
func newPutCmd() *cobra.Command {
34+
state := &putCmd{}
35+
36+
return &cobra.Command{
37+
Use: "put PATH NAME",
38+
Short: "Write a reference to the store",
39+
Long: "Read descriptor JSON from stdin and write it to the store.",
40+
Run: state.Run,
41+
}
42+
}
43+
44+
func (state *putCmd) Run(cmd *cobra.Command, args []string) {
45+
if len(args) != 2 {
46+
if err := cmd.Usage(); err != nil {
47+
fmt.Fprintln(os.Stderr, err)
48+
}
49+
os.Exit(1)
50+
}
51+
52+
state.path = args[0]
53+
state.name = args[1]
54+
55+
err := state.run()
56+
if err != nil {
57+
fmt.Fprintln(os.Stderr, err)
58+
os.Exit(1)
59+
}
60+
61+
os.Exit(0)
62+
}
63+
64+
func (state *putCmd) run() (err error) {
65+
ctx := context.Background()
66+
67+
engine, err := layout.NewEngine(ctx, state.path)
68+
if err != nil {
69+
return err
70+
}
71+
defer engine.Close()
72+
73+
decoder := json.NewDecoder(os.Stdin)
74+
var descriptor specs.Descriptor
75+
err = decoder.Decode(&descriptor)
76+
if err != nil {
77+
return err
78+
}
79+
80+
return engine.Put(ctx, state.name, &descriptor)
81+
}

image/refs/layout/main.go

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

image/refs/layout/tar.go

Lines changed: 13 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
"io"
@@ -50,8 +51,18 @@ func NewTarEngine(ctx context.Context, file caslayout.ReadWriteSeekCloser) (eng
5051

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

5768
// Get returns a reference from the store.

0 commit comments

Comments
 (0)