From 9efc2f579de0734954e416e6962f63775ae867c2 Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Thu, 18 Jul 2019 14:29:13 -0700 Subject: [PATCH] Added all profile funcs --- pkg/minikube/config/profile.go | 69 ++++++++++++++++++++++++++++++++++ pkg/minikube/config/types.go | 1 + 2 files changed, 70 insertions(+) create mode 100644 pkg/minikube/config/profile.go diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go new file mode 100644 index 0000000000..50897e16ea --- /dev/null +++ b/pkg/minikube/config/profile.go @@ -0,0 +1,69 @@ +/* +Copyright 2019 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 config + +import ( + "os" + "path/filepath" + + "github.com/golang/glog" + "k8s.io/minikube/pkg/minikube/constants" +) + +// AllProfiles returns all minikube profiles +func AllProfiles() (profiles []*Profile) { + + pDirs, err := allProfileDirs() + if err != nil { + glog.Infof("Error getting profile directories %v", err) + } + for _, n := range pDirs { + p, err := loadProfile(n) + if err != nil { + // TODO warn user to delete this folder or maybe do it for them + glog.Errorf("Invalid profile config in profiles folder (%s):\n error: %v", n, err) + continue + } + profiles = append(profiles, p) + } + return profiles +} + +func loadProfile(n string) (*Profile, error) { + + cfg, err := DefaultLoader.LoadConfigFromFile(n) + profile := &Profile{ + Name: n, + Config: cfg, + } + return profile, err +} + +// allProfileDirs gets all the folders in the user's profiles folder +func allProfileDirs() (dirs []string, err error) { + root := filepath.Join(constants.GetMinipath(), "profiles") + err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + if path != root { + dirs = append(dirs, filepath.Base(path)) + } + } + return nil + }) + return dirs, err + +} diff --git a/pkg/minikube/config/types.go b/pkg/minikube/config/types.go index 68b98d2cc1..5716bb4c33 100644 --- a/pkg/minikube/config/types.go +++ b/pkg/minikube/config/types.go @@ -22,6 +22,7 @@ import ( "k8s.io/minikube/pkg/util" ) +// Profile represents a minikube profile type Profile struct { Name string Config *Config