Skip to content

Commit 5560dae

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 8d4e2a8 commit 5560dae

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-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
@@ -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)