-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (93 loc) · 2.84 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/fedragon/cuttlefish/git"
"github.com/fedragon/cuttlefish/globals"
"github.com/fedragon/cuttlefish/paths"
"github.com/fedragon/cuttlefish/ssh"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
)
var k = koanf.New(".")
// ConfigFileName is the name of the config file used by this script
const ConfigFileName = ".cuttlefish.yaml"
var sysTmpDir = os.Getenv("TMPDIR")
// TmpFile is the tmp file where the script stores the path to the previous config, if any
var TmpFile = filepath.Join(sysTmpDir, "cuttlefish-lastvisited")
func main() {
config, err := find(os.Getenv("PWD"))
if err != nil {
// signal that there's nothing to source
os.Exit(1)
}
previousConfig, err := ioutil.ReadFile(TmpFile)
if err != nil {
previousConfig = []byte("")
}
if string(previousConfig) == config {
// nothing to do
os.Exit(1)
}
if err := k.Load(file.Provider(config), yaml.Parser()); err != nil {
log.Fatal(err)
}
var cmds []string
if strings.TrimSpace(string(previousConfig)) == "" {
cmds = withoutPreviousConfig()
} else {
cmds, err = withPreviousConfig(string(previousConfig))
if err != nil {
log.Fatal(err)
}
}
if err = ioutil.WriteFile(TmpFile, []byte(config), 0644); err != nil {
log.Fatal(err)
}
fmt.Println(strings.Join(cmds, "\n"))
}
func find(pwd string) (string, error) {
absPwd, err := filepath.Abs(pwd)
if err != nil {
return "", err
}
if !strings.HasPrefix(absPwd, os.Getenv("HOME")) {
return "", errors.New("only checking in home dir")
}
fullPath := filepath.Join(absPwd, ConfigFileName)
_, err = os.Stat(fullPath)
if os.IsNotExist(err) {
if os.Getenv("HOME") == absPwd {
return "", fmt.Errorf("no %v found in any parent (stopped at %v)", ConfigFileName, os.Getenv("HOME"))
}
return find(filepath.Dir(absPwd))
}
return fullPath, nil
}
func withoutPreviousConfig() []string {
cmds := paths.Set(k.Strings("user_paths"), nil)
cmds = append(cmds, ssh.Set(k.Strings("ssh_identities"), nil)...)
cmds = append(cmds, globals.Set(k.StringMap("global_variables"), nil)...)
return append(cmds, git.SetIdentity(k.String("git_config.email")))
}
func withPreviousConfig(previous string) ([]string, error) {
var pk = koanf.New(".")
if err := pk.Load(file.Provider(previous), yaml.Parser()); err != nil {
return nil, err
}
cmds := paths.Set(k.Strings("user_paths"), pk.Strings("user_paths"))
cmds = append(cmds, git.SetIdentity(k.String("git_config.email")))
cmds = append(cmds, ssh.Set(k.Strings("ssh_identities"), pk.Strings("ssh_identities"))...)
vars := pk.StringMap("global_variables")
keys := make([]string, 0, len(vars))
for key := range vars {
keys = append(keys, key)
}
return append(cmds, globals.Set(k.StringMap("global_variables"), keys)...), nil
}