github.com/coreos/mantle@v0.13.0/auth/packet.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 PacketConfigPath = ".config/packet.json" 26 27 // PacketProfile represents a parsed Packet profile. This is a custom format 28 // specific to Mantle. 29 type PacketProfile struct { 30 ApiKey string `json:"api_key"` 31 Project string `json:"project"` 32 } 33 34 // ReadPacketConfig decodes a Packet config file, which is a custom format 35 // used by Mantle to hold API keys. 36 // 37 // If path is empty, $HOME/.config/packet.json is read. 38 func ReadPacketConfig(path string) (map[string]PacketProfile, error) { 39 if path == "" { 40 user, err := user.Current() 41 if err != nil { 42 return nil, err 43 } 44 path = filepath.Join(user.HomeDir, PacketConfigPath) 45 } 46 47 f, err := os.Open(path) 48 if err != nil { 49 return nil, err 50 } 51 defer f.Close() 52 53 var profiles map[string]PacketProfile 54 if err := json.NewDecoder(f).Decode(&profiles); err != nil { 55 return nil, err 56 } 57 if len(profiles) == 0 { 58 return nil, fmt.Errorf("Packet config %q contains no profiles", path) 59 } 60 61 return profiles, nil 62 }