|
| 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 | +} |
0 commit comments