github.com/coreos/mantle@v0.13.0/auth/esx.go (about) 1 // Copyright 2017 CoreOS, Inc. 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 auth 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "os" 21 "os/user" 22 "path/filepath" 23 ) 24 25 const ESXConfigPath = ".config/esx.json" 26 27 // ESXProfile represents a parsed ESX profile. This is a custom format 28 // specific to Mantle. 29 type ESXProfile struct { 30 Server string `json:"server"` 31 User string `json:"user"` 32 Password string `json:"password"` 33 } 34 35 // ReadESXConfig decodes a ESX config file, which is a custom format 36 // used by Mantle to hold ESX server information. 37 // 38 // If path is empty, $HOME/.config/esx.json is read. 39 func ReadESXConfig(path string) (map[string]ESXProfile, error) { 40 if path == "" { 41 user, err := user.Current() 42 if err != nil { 43 return nil, err 44 } 45 path = filepath.Join(user.HomeDir, ESXConfigPath) 46 } 47 48 f, err := os.Open(path) 49 if err != nil { 50 return nil, err 51 } 52 defer f.Close() 53 54 var profiles map[string]ESXProfile 55 if err := json.NewDecoder(f).Decode(&profiles); err != nil { 56 return nil, err 57 } 58 if len(profiles) == 0 { 59 return nil, fmt.Errorf("ESX config %q contains no profiles", path) 60 } 61 62 return profiles, nil 63 }