|
| 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 | + "log" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + specs "github.com/opencontainers/image-spec/specs-go" |
| 24 | + "github.com/opencontainers/image-tools/image" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +// gitCommit will be the hash that the binary was built from |
| 29 | +// and will be populated by the Makefile |
| 30 | +var gitCommit = "" |
| 31 | + |
| 32 | +var generateTypes = []string{ |
| 33 | + "imageLayout", |
| 34 | + "image", |
| 35 | +} |
| 36 | + |
| 37 | +type generateCmd struct { |
| 38 | + stdout *log.Logger |
| 39 | + stderr *log.Logger |
| 40 | + typ string //the type to generate, can be empty string |
| 41 | + version bool |
| 42 | +} |
| 43 | + |
| 44 | +func main() { |
| 45 | + stdout := log.New(os.Stdout, "", 0) |
| 46 | + stderr := log.New(os.Stderr, "", 0) |
| 47 | + |
| 48 | + cmd := newGenerateCmd(stdout, stderr) |
| 49 | + if err := cmd.Execute(); err != nil { |
| 50 | + stderr.Println(err) |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func newGenerateCmd(stdout, stderr *log.Logger) *cobra.Command { |
| 56 | + v := &generateCmd{ |
| 57 | + stdout: stdout, |
| 58 | + stderr: stderr, |
| 59 | + } |
| 60 | + |
| 61 | + cmd := &cobra.Command{ |
| 62 | + Use: "generate [dest]", |
| 63 | + Short: "Generate an image or an imageLayout", |
| 64 | + Long: `Generate the OCI iamge or imageLayout to the destination directory [dest].`, |
| 65 | + Run: v.Run, |
| 66 | + } |
| 67 | + |
| 68 | + cmd.Flags().StringVar( |
| 69 | + &v.typ, "type", "imageLayout", |
| 70 | + fmt.Sprintf( |
| 71 | + `Type of the file to generate, one of "%s".`, |
| 72 | + strings.Join(generateTypes, ","), |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + cmd.Flags().BoolVarP( |
| 77 | + &v.version, "version", "v", false, |
| 78 | + `Print version information and exit`, |
| 79 | + ) |
| 80 | + |
| 81 | + origHelp := cmd.HelpFunc() |
| 82 | + |
| 83 | + cmd.SetHelpFunc(func(c *cobra.Command, args []string) { |
| 84 | + origHelp(c, args) |
| 85 | + stdout.Println("\nMore information:") |
| 86 | + stdout.Printf("\treferences\t%s\n", image.SpecURL) |
| 87 | + stdout.Printf("\tbug report\t%s\n", image.IssuesURL) |
| 88 | + }) |
| 89 | + |
| 90 | + return cmd |
| 91 | +} |
| 92 | + |
| 93 | +func (v *generateCmd) Run(cmd *cobra.Command, args []string) { |
| 94 | + if v.version { |
| 95 | + v.stdout.Printf("commit: %s", gitCommit) |
| 96 | + v.stdout.Printf("spec: %s", specs.Version) |
| 97 | + os.Exit(0) |
| 98 | + } |
| 99 | + |
| 100 | + if len(args) != 1 { |
| 101 | + v.stderr.Print("dest must be provided") |
| 102 | + if err := cmd.Usage(); err != nil { |
| 103 | + v.stderr.Println(err) |
| 104 | + } |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + |
| 108 | + var err error |
| 109 | + switch v.typ { |
| 110 | + case "imageLayout": |
| 111 | + err = image.GenerateLayout(args[0]) |
| 112 | + case "image": |
| 113 | + err = image.Generate(args[0]) |
| 114 | + default: |
| 115 | + err = fmt.Errorf("unsupport type %q", v.typ) |
| 116 | + } |
| 117 | + |
| 118 | + if err != nil { |
| 119 | + v.stderr.Printf("generating failed: %v", err) |
| 120 | + os.Exit(1) |
| 121 | + } |
| 122 | + |
| 123 | + os.Exit(0) |
| 124 | +} |
0 commit comments