/
config.go
79 lines (65 loc) · 1.92 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"strings"
"github.com/msolo/git-mg/gitapi"
"github.com/pkg/errors"
)
type config struct {
// sshControlPath is used to explicitly set the control socket for our usage.
sshControlPath string
gitLocalPath string
gitRemotePath string
rsyncLocalPath string
rsyncRemotePath string
fsmonitorLocalPath string
excludePaths []string
remoteName string
remoteURL string
gitConfig gitapi.GitConfig
}
func (cfg config) remoteSSHAddr() string {
return strings.Split(cfg.remoteURL, ":")[0]
}
func (cfg config) remoteDir() string {
return strings.Split(cfg.remoteURL, ":")[1]
}
func (cfg config) fsmonitorEnabled() bool {
return cfg.fsmonitorLocalPath != ""
}
var defaultConfig = config{
// ssh -G <host> | awk '/^controlpath/{print $2}'
sshControlPath: "/tmp/ssh_mux_%h_%p_%r",
gitRemotePath: "git",
gitLocalPath: "git",
rsyncRemotePath: "rsync",
rsyncLocalPath: "rsync", // Assume a satisfactory rsync is in the path.
remoteName: "sync",
}
func readConfigFromGit(remoteName string) (*config, error) {
wd := gitapi.NewGitWorkdir()
gitConfig, err := wd.GitConfig()
if err != nil {
return nil, err
}
cfg := defaultConfig
cfg.gitConfig = gitConfig
if remoteName == "" {
remoteName = gitConfig.Get("sync.remotename")
}
if remoteName != "" {
cfg.remoteName = remoteName
}
if excludePaths := gitConfig.Get("sync.excludepaths"); excludePaths != "" {
cfg.excludePaths = strings.Split(strings.TrimSpace(excludePaths), ":")
}
if rpath := gitConfig.Get("sync.rsyncremotepath"); rpath != "" {
cfg.rsyncRemotePath = rpath
}
remoteURLKey := "remote." + cfg.remoteName + ".url"
cfg.remoteURL = strings.TrimSpace(gitConfig.Get(remoteURLKey))
if cfg.remoteURL == "" {
return nil, errors.Errorf("no url specified for remote name %q %#v", cfg.remoteName, gitConfig)
}
cfg.fsmonitorLocalPath = gitConfig.Get("core.fsmonitor")
return &cfg, nil
}