-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
189 lines (159 loc) · 5.04 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2019 NVI Inc. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package fs
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
versions "github.com/nvi-inc/fsgo/versions"
_ "github.com/nvi-inc/fsgo/versions/all"
)
const DefaultPath = "/usr2/fs"
type FieldSystem versions.FieldSystem
type Rdbe versions.Rdbe
var ErrVersionNotSupported = errors.New("version not supported")
// Attach attaches to the shared memory of the Field System installed at
// DefaultPath, attempting to automatically detect the version. Returns an
// error if the detected version is not supported or the system call fails.
func Attach() (fs FieldSystem, err error) {
return AttachPath(DefaultPath)
}
// AttachPath attaches to the shared memory of the Field System installed at
// path, attempting to automatically detect the version. Returns an error if
// the detected version is not supported or the system call fails.
func AttachPath(path string) (fs FieldSystem, err error) {
version, err := InstalledVersion(path)
if err != nil {
return nil, err
}
return AttachVersionPath(version, path)
}
// AttachPath attaches to the shared memory of the Field System installed at
// DefaultPath, using the shared memory version specified. Returns an error if
// the version is not supported or the system call fails.
func AttachVersion(version string) (fs FieldSystem, err error) {
return AttachVersionPath(version, DefaultPath)
}
// AttachVersionPath attaches to the shared memory of the Field System installed at
// path, using the shared memory version specified. Returns an error if the
// version is not supported or the system call fails.
func AttachVersionPath(version, path string) (fs FieldSystem, err error) {
creator, ok := versions.Creators[version]
if !ok {
return nil, fmt.Errorf("load version %s: %w", version, ErrVersionNotSupported)
}
fs = creator()
err = fs.Attach()
if err != nil {
return nil, fmt.Errorf("attaching shared memory: %w", err)
}
return fs, nil
}
// SupportedVersions list the versions of the Field System supported
func SupportedVersions() []string {
v := make([]string, 0, len(versions.Creators))
for k := range versions.Creators {
v = append(v, k)
}
return v
}
// InstalledVersion attempts to detect the installed version of the Field System by
// checking:
// - if path is a git repository, then using the tag.
// - if path is a symlink to /usr2/fs-(version) and using (version)
// - if path/Makefile contains the variables VERSION SUBLEVEL PATCHLEVEL
func InstalledVersion(path string) (string, error) {
version, errgit := InstalledVersionFromGit(path)
if errgit == nil {
return version, nil
}
if os.IsNotExist(errgit) {
return "", errgit
}
version, errpath := InstalledVersionFromPath(path)
if errpath == nil {
return version, nil
}
version, errmake := InstalledVersionFromMakefile(path)
if errmake == nil {
return version, nil
}
return "",
fmt.Errorf("git method: %v, path method: %v, makefile method: %v",
errgit, errpath, errmake)
}
var ErrNotGitDir = errors.New("not a git directory")
// InstalledVersionFromGit attemps to detect the FS version installed in path
// by using the git tags. This requires git to be in the path.
func InstalledVersionFromGit(path string) (string, error) {
var out bytes.Buffer
if _, err := os.Stat(path); err != nil {
return "", err
}
if _, err := os.Stat(path + "/.git"); os.IsNotExist(err) {
return "", ErrNotGitDir
}
gitargs := []string{
fmt.Sprintf("--git-dir=%s/.git", path),
"describe",
"--always",
"--tags",
}
cmd := exec.Command("git", gitargs...)
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("calling git: %w", err)
}
return strings.TrimSpace(out.String()), nil
}
var pathRegex = regexp.MustCompile(`fs-(\d+\.\d+\.\d+)$`)
var ErrPathMatch = errors.New("not a git directory")
// InstalledVersionFromPath attemps to detect the version of the installed
// Field System by examining the sympolic link "path"
func InstalledVersionFromPath(path string) (string, error) {
name, err := os.Readlink(path)
if err != nil {
return "", err
}
m := pathRegex.FindStringSubmatch(filepath.Base(name))
if m == nil || len(m) == 0 {
return "", ErrPathMatch
}
return m[1], nil
}
// InstalledVersionFromMakefile attempts to detect the installed version of the
// Field System by parsing the Makefile in "/usr2/fs" directory.
func InstalledVersionFromMakefile(path string) (string, error) {
r, err := regexp.Compile(`^(\w+)\s*=\s*(\w+)$`)
if err != nil {
return "", err
}
vars := make(map[string]int)
f, err := os.Open(path + "/Makefile")
if err != nil {
return "", err
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
m := r.FindStringSubmatch(line)
if m == nil {
continue
}
i, err := strconv.Atoi(m[2])
if err != nil {
continue
}
vars[m[1]] = i
}
// TODO: check if these are digits
return fmt.Sprintf("%d.%d.%d", vars["VERSION"], vars["SUBLEVEL"], vars["PATCHLEVEL"]), nil
}