-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.go
182 lines (161 loc) · 4.59 KB
/
install.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
/*
* Copyright (c) 2023 Maple Wu <justmaplewu@gmail.com>
* National Electronics and Computer Technology Center, Thailand
*
* 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 (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
zcore "github.com/go-zing/gozz-core"
"github.com/spf13/cobra"
)
const (
installBuildDir = "/tmp/gozz/build/"
)
var (
install = &cobra.Command{
Use: "install",
Short: "install external plugin from provided path or url",
Example: zcore.ExecName + ` install [repository]`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := Install(args[0]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
}
},
}
coreDepPath = reflect.TypeOf(zcore.AnnotatedDecl{}).PkgPath()
installBuildOutput string
installBuildTarget string
)
func init() {
flags := install.Flags()
flags.StringVarP(&installBuildOutput, "output", "o", "", "specify install build output filename")
flags.StringVarP(&installBuildTarget, "filepath", "f", "", "specify install build target relative filepath")
}
func Install(repository string) (err error) {
if strings.Contains(repository, "://") {
return doInstallRemote(repository)
}
// local path
if f, e := os.Stat(repository); e == nil {
if repository, err = filepath.Abs(repository); err != nil {
return
}
if !f.IsDir() {
if len(installBuildTarget) == 0 {
repository = filepath.Dir(repository)
installBuildTarget = f.Name()
} else {
return fmt.Errorf("invalid repository directory %s with filepath: %s ", repository, installBuildTarget)
}
}
return doInstall(repository)
}
return doInstallRemote(repository)
}
func doInstallRemote(repository string) (err error) {
t := time.Now().Format("060102150405")
dir := filepath.Join(installBuildDir, t)
_ = os.MkdirAll(installBuildDir, 0o775)
defer os.RemoveAll(dir)
buildCmd := exec.Command("sh", "-c", fmt.Sprintf("git clone --depth=1 %s %s", repository, t))
buildCmd.Dir = installBuildDir
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err = buildCmd.Run(); err != nil {
return
}
return doInstall(dir)
}
func doInstall(dir string) (err error) {
wd, err := os.Getwd()
if err != nil {
return
}
buildDst := filepath.Join(wd, "tmp.so")
args := []string{"build", "--buildmode=plugin", "-o=" + buildDst}
if len(installBuildTarget) == 0 {
args = append(args, "./")
} else if installBuildTarget, err = filepath.Abs(filepath.Join(dir, installBuildTarget)); err != nil {
return
} else if info, e := os.Stat(installBuildTarget); e != nil {
return e
} else if info.IsDir() {
dir = installBuildTarget
args = append(args, "./")
} else {
// computed relative directory
dir, installBuildTarget = filepath.Split(installBuildTarget)
args = append(args, "./"+installBuildTarget)
}
// get env
goenv, err := getGoenv(dir)
if err != nil {
return
}
// validate runtime
runtimeVersion := runtime.Version()
if v := goenv["GOVERSION"]; runtimeVersion != v {
return fmt.Errorf("gozz runtime GOVERSION %s mismatch: %s", runtimeVersion, v)
}
// replace core mod version
if cv := getCoreVersion(); len(cv) > 0 {
if _, err = zcore.ExecCommand(
fmt.Sprintf("go mod edit -replace=%s=%s@%s && go mod tidy", coreDepPath, coreDepPath, cv),
filepath.Dir(goenv["GOMOD"]),
); err != nil {
return
}
}
// build env
envs := os.Environ()
for i := range envs {
if env := envs[i]; strings.HasPrefix(env, "GOOS=") {
envs[i] = "GOOS=" + runtime.GOOS
} else if strings.HasPrefix(env, "GOARCH=") {
envs[i] = "GOARCH=" + runtime.GOARCH
}
}
// build
buildCmd := exec.Command("go", args...)
buildCmd.Dir = dir
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
buildCmd.Env = envs
if err = buildCmd.Run(); err != nil {
return
}
// validate
name, err := zcore.LoadExtension(buildDst)
if err != nil {
return
}
// install
if len(installBuildOutput) > 0 {
return os.Rename(buildDst, installBuildOutput)
}
if err = os.MkdirAll(pluginDir, 0o755); err != nil {
return
}
return os.Rename(buildDst, filepath.Join(pluginDir, name+".so"))
}