From 1966dab625da912a303cbd71ee5e209b65bea71e Mon Sep 17 00:00:00 2001 From: kairen Date: Tue, 5 Dec 2017 15:14:23 +0800 Subject: [PATCH] Add cache list support --- cmd/minikube/cmd/cache_list.go | 75 +++++++++++++++++++++++++++++++ cmd/minikube/cmd/config/config.go | 15 +++++++ 2 files changed, 90 insertions(+) create mode 100644 cmd/minikube/cmd/cache_list.go diff --git a/cmd/minikube/cmd/cache_list.go b/cmd/minikube/cmd/cache_list.go new file mode 100644 index 0000000000..dfb44d4b5e --- /dev/null +++ b/cmd/minikube/cmd/cache_list.go @@ -0,0 +1,75 @@ +/* +Copyright 2017 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "fmt" + "os" + "text/template" + + "github.com/golang/glog" + "github.com/spf13/cobra" + cmdConfig "k8s.io/minikube/cmd/minikube/cmd/config" + "k8s.io/minikube/pkg/minikube/constants" +) + +const cacheListFormat = "- {{.CacheImageName}}\n" + +type CacheListTemplate struct { + CacheImageName string +} + +// listCacheCmd represents the cache list command +var listCacheCmd = &cobra.Command{ + Use: "list", + Short: "List all available images from the local cache.", + Long: "List all available images from the local cache.", + Run: func(cmd *cobra.Command, args []string) { + // list images from config file + images, err := cmdConfig.ListConfigMap(constants.Cache) + if err != nil { + fmt.Fprintf(os.Stderr, "Error listing images: %s\n", err) + os.Exit(1) + } + if err := cacheList(images); err != nil { + fmt.Fprintf(os.Stderr, "Error listing images: %s\n", err) + os.Exit(1) + } + }, +} + +func init() { + cacheCmd.AddCommand(listCacheCmd) + RootCmd.AddCommand(cacheCmd) +} + +func cacheList(images map[string]interface{}) error { + for imageName := range images { + tmpl, err := template.New("list").Parse(cacheListFormat) + if err != nil { + glog.Errorln("Error creating list template:", err) + os.Exit(1) + } + listTmplt := CacheListTemplate{imageName} + err = tmpl.Execute(os.Stdout, listTmplt) + if err != nil { + glog.Errorln("Error executing list template:", err) + os.Exit(1) + } + } + return nil +} diff --git a/cmd/minikube/cmd/config/config.go b/cmd/minikube/cmd/config/config.go index 582027d5a7..6a50b8a581 100644 --- a/cmd/minikube/cmd/config/config.go +++ b/cmd/minikube/cmd/config/config.go @@ -219,6 +219,21 @@ func configurableFields() string { return strings.Join(fields, "\n") } +// ListConfigMap list entries from config file +func ListConfigMap(name string) (map[string]interface{}, error) { + configFile, err := config.ReadConfig() + if err != nil { + return nil, err + } + newImages := make(map[string]interface{}) + if values, ok := configFile[name].(map[string]interface{}); ok { + for key := range values { + newImages[key] = nil + } + } + return newImages, nil +} + // AddToConfigMap adds entries to a map in the config file func AddToConfigMap(name string, images []string) error { s, err := findSetting(name)