|
| 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 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/opencontainers/image-tools/image/cas/layout" |
| 22 | + "github.com/spf13/cobra" |
| 23 | + "golang.org/x/net/context" |
| 24 | +) |
| 25 | + |
| 26 | +type deleteCmd struct { |
| 27 | + path string |
| 28 | + digest string |
| 29 | +} |
| 30 | + |
| 31 | +func newDeleteCmd() *cobra.Command { |
| 32 | + state := &deleteCmd{} |
| 33 | + |
| 34 | + return &cobra.Command{ |
| 35 | + Use: "delete PATH DIGEST", |
| 36 | + Short: "Remove a blob from from the store", |
| 37 | + Run: state.Run, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (state *deleteCmd) Run(cmd *cobra.Command, args []string) { |
| 42 | + if len(args) != 2 { |
| 43 | + fmt.Fprintln(os.Stderr, "both PATH and DIGEST must be provided") |
| 44 | + if err := cmd.Usage(); err != nil { |
| 45 | + fmt.Fprintln(os.Stderr, err) |
| 46 | + } |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + state.path = args[0] |
| 51 | + state.digest = args[1] |
| 52 | + |
| 53 | + err := state.run() |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintln(os.Stderr, err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + os.Exit(0) |
| 60 | +} |
| 61 | + |
| 62 | +func (state *deleteCmd) run() (err error) { |
| 63 | + ctx := context.Background() |
| 64 | + |
| 65 | + engine, err := layout.NewEngine(ctx, state.path) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + defer engine.Close() |
| 70 | + |
| 71 | + return engine.Delete(ctx, state.digest) |
| 72 | +} |
0 commit comments