-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
63 lines (56 loc) · 1.21 KB
/
config.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
52
53
54
55
56
57
58
59
60
61
62
63
package scpw
import (
"fmt"
"gopkg.in/yaml.v2"
"os"
"os/user"
"path/filepath"
)
type SCPWType = string
const (
PUT SCPWType = "PUT"
GET SCPWType = "GET"
)
type Node struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
User string `yaml:"user"`
Port string `yaml:"port"`
KeyPath string `yaml:"keypath"`
Password string `yaml:"password"`
Children []*Node `yaml:"children"`
LRMap []LRMap `yaml:"lr-map"`
Typ SCPWType `yaml:"type"`
}
type LRMap struct {
Local string `yaml:"local"`
Remote string `yaml:"remote"`
}
func LoadConfig() ([]*Node, error) {
b, err := LoadConfigBytes(".scpw", ".scpw.yml", ".scpw.yaml")
if err != nil {
return nil, err
}
var config []*Node
err = yaml.Unmarshal(b, &config)
return config, err
}
func LoadConfigBytes(names ...string) ([]byte, error) {
u, err := user.Current()
if err != nil {
return nil, err
}
// homedir
for i := range names {
if sshw, e := os.ReadFile(filepath.Join(u.HomeDir, names[i])); e == nil {
return sshw, nil
}
}
// relative
for i := range names {
if sshw, e := os.ReadFile(names[i]); e == nil {
return sshw, nil
}
}
return nil, fmt.Errorf("cannot find config from %s", u.HomeDir)
}