|
| 1 | +/* |
| 2 | +Copyright 2018 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/GoogleContainerTools/kaniko/pkg/cache" |
| 24 | + "github.com/GoogleContainerTools/kaniko/pkg/config" |
| 25 | + "github.com/GoogleContainerTools/kaniko/pkg/constants" |
| 26 | + "github.com/GoogleContainerTools/kaniko/pkg/util" |
| 27 | + "github.com/pkg/errors" |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + opts = &config.WarmerOptions{} |
| 33 | + logLevel string |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic") |
| 38 | + addKanikoOptionsFlags(RootCmd) |
| 39 | + addHiddenFlags(RootCmd) |
| 40 | +} |
| 41 | + |
| 42 | +var RootCmd = &cobra.Command{ |
| 43 | + Use: "cache warmer", |
| 44 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 45 | + if err := util.ConfigureLogging(logLevel); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + if len(opts.Images) == 0 { |
| 49 | + return errors.New("You must select at least one image to cache") |
| 50 | + } |
| 51 | + return nil |
| 52 | + }, |
| 53 | + Run: func(cmd *cobra.Command, args []string) { |
| 54 | + if err := cache.WarmCache(opts); err != nil { |
| 55 | + exit(errors.Wrap(err, "Failed warming cache")) |
| 56 | + } |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +// addKanikoOptionsFlags configures opts |
| 61 | +func addKanikoOptionsFlags(cmd *cobra.Command) { |
| 62 | + RootCmd.PersistentFlags().VarP(&opts.Images, "image", "i", "Image to cache. Set it repeatedly for multiple images.") |
| 63 | + RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "c", "/cache", "Directory of the cache.") |
| 64 | +} |
| 65 | + |
| 66 | +// addHiddenFlags marks certain flags as hidden from the executor help text |
| 67 | +func addHiddenFlags(cmd *cobra.Command) { |
| 68 | + RootCmd.PersistentFlags().MarkHidden("azure-container-registry-config") |
| 69 | +} |
| 70 | + |
| 71 | +func exit(err error) { |
| 72 | + fmt.Println(err) |
| 73 | + os.Exit(1) |
| 74 | +} |
0 commit comments