-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpacket.go
51 lines (43 loc) · 1.09 KB
/
packet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright The Mantle Authors
// SPDX-License-Identifier: Apache-2.0
package auth
import (
"encoding/json"
"fmt"
"os"
"os/user"
"path/filepath"
)
const PacketConfigPath = ".config/packet.json"
// PacketProfile represents a parsed Packet profile. This is a custom format
// specific to Mantle.
type PacketProfile struct {
ApiKey string `json:"api_key"`
Project string `json:"project"`
}
// ReadPacketConfig decodes a Packet config file, which is a custom format
// used by Mantle to hold API keys.
//
// If path is empty, $HOME/.config/packet.json is read.
func ReadPacketConfig(path string) (map[string]PacketProfile, error) {
if path == "" {
user, err := user.Current()
if err != nil {
return nil, err
}
path = filepath.Join(user.HomeDir, PacketConfigPath)
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var profiles map[string]PacketProfile
if err := json.NewDecoder(f).Decode(&profiles); err != nil {
return nil, err
}
if len(profiles) == 0 {
return nil, fmt.Errorf("Packet config %q contains no profiles", path)
}
return profiles, nil
}