-
Notifications
You must be signed in to change notification settings - Fork 71
/
version.go
executable file
·226 lines (192 loc) · 5.72 KB
/
version.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// SPDX-License-Identifier: Apache-2.0
//go:build (linux && cgo) || (darwin && cgo) || (freebsd && cgo)
// +build linux,cgo darwin,cgo freebsd,cgo
/*
Copyright (C) 2023 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/spf13/pflag"
)
/*
#cgo linux LDFLAGS: -ldl
#include <dlfcn.h>
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
static uintptr_t pluginOpen(const char* path, char** err) {
void* h = dlopen(path, RTLD_NOW|RTLD_GLOBAL);
if (h == NULL) {
*err = (char*)dlerror();
}
return (uintptr_t)h;
}
static char* get_name(uintptr_t h, char** err) {
void* s = dlsym((void*)h, "plugin_get_name");
if (s == NULL) {
*err = (char*)dlerror();
return NULL;
}
typedef char* (*fptr)();
fptr f = (fptr)s;
return f();
}
static char* get_version(uintptr_t h, char** err) {
void* s = dlsym((void*)h, "plugin_get_version");
if (s == NULL) {
*err = (char*)dlerror();
return NULL;
}
typedef char* (*fptr)();
fptr f = (fptr)s;
return f();
}
*/
import "C"
var rgxVersion = regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?$`)
var rgxHash = regexp.MustCompile(`^[0-9a-z]+$`)
var rgxName = regexp.MustCompile(`^[a-z]+[a-z0-9_\-]*$`)
func pluginInfo(path string) (name, version string, err error) {
path, err = filepath.Abs(path)
if err != nil {
return
}
cPath := C.CString(path)
var cErr *C.char
h := C.pluginOpen(cPath, &cErr)
if h == 0 {
err = errors.New("cannot open " + path + ": " + C.GoString(cErr))
return
}
cName := C.get_name(h, &cErr)
if cName == nil {
err = errors.New("cannot get name of " + path + ": " + C.GoString(cErr))
return
}
// we need to convert immediately the pointer into a string
// otherwise the pointer could change see plugin sdk cpp
name = C.GoString(cName)
cVer := C.get_version(h, &cErr)
if cVer == nil {
err = errors.New("cannot get version of " + path + ": " + C.GoString(cErr))
return
}
// Same as before
version = C.GoString(cVer)
return
}
func git(args ...string) (output []string, err error) {
fmt.Println("git ", strings.Join(args, " "))
stdout, err := exec.Command("git", args...).Output()
fmt.Println(string(stdout))
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return nil, errors.New("git (" + exitErr.String() + "): " + string(exitErr.Stderr))
}
return nil, err
}
return strings.Split(string(stdout), "\n"), nil
}
func fail(err error) {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
func main() {
var path string
var pre bool
pflag.StringVar(&path, "path", "", "path to the .so file")
pflag.BoolVar(&pre, "pre-release", false, "if set, output a pre-release version")
pflag.Parse()
name, version, err := pluginInfo(path)
if err != nil {
fail(err)
}
if !rgxVersion.MatchString(version) {
fail(errors.New("plugin declared version is not compatible with SemVer: " + version))
}
if !rgxName.MatchString(name) {
fail(errors.New("plugin declared name is not correctly-formatted: " + name))
}
if pre {
// pre-releases MUST adhere to VP-VT-n+hash format, given:
// - VP is the SemVer-compatible plugin declared version
// - VT is the SemVer-compatible latest released version of the plugin (git tagged)
// - n is the numeber of commits since the latest released version
// - hash is the git commit id (abbrev to 7 digits)
lastVer := "0.0.0" // fallback value
var n int
var hash string
// get last tag
// It matches the old tag in "pluginName-version" or the new one "plugins/pluginName/semver"
tags, err := git("describe", "--tags", "--abbrev=0", "--match", name+`-[0-9]*`, "--match", "plugins/"+name+"/v*")
if err == nil {
if len(tags) == 0 {
fail(errors.New("no git tag found for: " + name))
}
lastTag := tags[0]
if strings.HasPrefix(lastTag, name) {
lastVer = strings.Replace(lastTag, name+"-", "", 1)
} else {
lastVer = strings.Replace(lastTag, "plugins/"+name+"/v", "", 1)
}
if !rgxVersion.MatchString(lastVer) {
fail(errors.New("plugin latest released version not compatible with SemVer: " + lastTag))
}
// get number of commits since the last tag
counts, err := git("rev-list", lastTag+"..", "--count")
if err != nil {
fail(err)
}
if len(counts) > 0 {
n, _ = strconv.Atoi(counts[0])
}
}
refs, err := git("rev-parse", "--short=7", "HEAD")
if err != nil {
fail(err)
}
if len(refs) == 0 {
fail(errors.New("no commit id found"))
}
hash = refs[0]
if !rgxHash.MatchString(hash) {
fail(errors.New("commit hash not in correct hex form: " + hash))
}
fmt.Printf("%s-%s-%d+%s\n", version, lastVer, n, hash)
} else {
// stable versions MUST have a precise tag matching plugin name and version
expectedTag := "plugins/" + name + "/v" + version
tags, err := git("--no-pager", "tag", "--points-at", "HEAD")
if err != nil {
fail(err)
}
if len(tags) == 0 || len(tags[0]) == 0 {
fail(errors.New("there are no tags pointing at HEAD"))
}
for _, tag := range tags {
if tag == expectedTag {
fmt.Println(version)
return
}
}
fail(errors.New("no git tag found: " + expectedTag))
}
}