forked from FDio/govpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
246 lines (216 loc) · 6.85 KB
/
input.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// 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 vppapi
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
// ResolveVppInput parses an input string and returns VppInput or an error if a problem
// occurs durig parsing. The input string consists of path, which can be followed
// by '#' and one or more options separated by comma. The actual format can be
// specified explicitely by an option 'format', if that is not the case then the
// format will be detected from path automatically if possible.
//
// - `path`
// - `path#option1=val,option2=val`
//
// Available formats:
//
// * Directory: `dir`
// - absolute: `/usr/share/vpp/api`
// - relative: `path/to/apidir`
//
// * Git repository: `git`
// - local repository: `.git`
// - remote repository: `https://github.com/FDio/vpp.git`
//
// * Tarball/Zip Archive (`tar`/`zip`)
// - local archive: `api.tar.gz`
// - remote archive: `https://example.com/api.tar.gz`
// - standard input: `-`
//
// Format options:
//
// * Git repository
// - `branch`: name of branch
// - `tag`: specific git tag
// - `ref`: git reference
// - `depth`: git depth
// - `subdir`: subdirectory to use as base directory
//
// * Tarball/ZIP Archive
// - `compression`: compression to use (`gzip`)
// - `subdir`: subdirectory to use as base directory
// - 'strip': strip first N directories, applied before `subdir`
//
// Returns VppInput on success.
func ResolveVppInput(input string) (*VppInput, error) {
inputRef, err := ParseInputRef(input)
if err != nil {
return nil, err
}
return inputRef.Retrieve()
}
// VppInput defines VPP API input source.
type VppInput struct {
ApiDirectory string
Schema Schema
}
func resolveVppInputFromDir(path string) (*VppInput, error) {
vppInput := new(VppInput)
apidir := ResolveApiDir(path)
vppInput.ApiDirectory = apidir
logrus.WithField("path", path).Tracef("resolved API dir: %q", apidir)
apiFiles, err := ParseDir(apidir)
if err != nil {
//logrus.Warnf("vppapi parsedir error: %v", err)
return nil, fmt.Errorf("parsing API dir %s failed: %w", apidir, err)
}
vppInput.Schema.Files = apiFiles
logrus.Tracef("resolved %d apifiles", len(apiFiles))
vppVersion := ResolveVPPVersion(path)
if vppVersion == "" {
vppVersion = "unknown"
}
vppInput.Schema.Version = vppVersion
return vppInput, nil
}
const (
cacheDir = "./.cache"
)
func cloneRepoLocally(repo string, commit string, branch string, depth int) (string, error) {
repoPath := strings.ReplaceAll(repo, "/", "_")
repoPath = strings.ReplaceAll(repoPath, ":", "_")
cachePath := filepath.Join(cacheDir, repoPath)
// Clone the repository or use cached one
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return "", fmt.Errorf("failed to create cache directory: %w", err)
}
args := []string{"--single-branch"}
if depth > 0 {
args = append(args, fmt.Sprintf("--depth=%d", depth))
}
args = append(args, repo, cachePath)
logrus.Debugf("cloning repo: %v", args)
cmd := exec.Command("git", append([]string{"clone"}, args...)...)
if output, err := cmd.CombinedOutput(); err != nil {
return "", fmt.Errorf("failed to clone repository: %w\nOutput: %s", err, output)
}
} else if err != nil {
return "", fmt.Errorf("failed to check if cache exists: %w", err)
}
logrus.Debugf("using local repo dir: %q, fetching %q", cachePath, commit)
cmd := exec.Command("git", "fetch", "--tags", "origin")
cmd.Dir = cachePath
if output, err := cmd.CombinedOutput(); err != nil {
logrus.Debugf("ERROR: failed to fetch tags: %v\nOutput: %s", err, output)
}
cmd = exec.Command("git", "fetch", "-f", "origin", commit)
cmd.Dir = cachePath
if output, err := cmd.CombinedOutput(); err != nil {
return "", fmt.Errorf("failed to fetch commit: %w\nOutput: %s", err, output)
}
// Resolve the commit hash for the given branch/tag
ref := commit
if branch != "" {
ref = "origin/" + branch
}
commitHash, err := resolveCommitHash(cachePath, ref)
if err != nil {
return "", fmt.Errorf("failed to resolve commit hash: %w", err)
}
// Check out the repository at the resolved commit
cmd = exec.Command("git", "checkout", commitHash)
cmd.Dir = cachePath
if output, err := cmd.CombinedOutput(); err != nil {
return "", fmt.Errorf("failed to check out repository: %w\nOutput: %s", err, output)
}
return cachePath, nil
}
func resolveCommitHash(repoPath, ref string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--verify", ref)
cmd.Dir = repoPath
outputBytes, err := cmd.Output()
if err != nil {
logrus.Tracef("[ERR] command %q output: %s", cmd, outputBytes)
return "", fmt.Errorf("failed to run command: %w", err)
} else {
logrus.Tracef("[OK] command %q output: %s", cmd, outputBytes)
}
return strings.TrimSpace(string(outputBytes)), nil
}
func extractTar(reader io.Reader, gzipped bool, strip int) (string, error) {
tempDir, err := os.MkdirTemp("", "govpp-vppapi-archive-")
if err != nil {
return "", err
}
if gzipped {
gzReader, err := gzip.NewReader(reader)
if err != nil {
return "", err
}
defer gzReader.Close()
reader = gzReader
}
tarReader := tar.NewReader(reader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
nameList := strings.Split(header.Name, "/")
if len(nameList) < strip {
return "", fmt.Errorf("cannot strip first %d directories for file: %s", strip, header.Name)
}
fileName := filepath.Join(nameList[strip:]...)
filePath := filepath.Join(tempDir, fileName)
fileInfo := header.FileInfo()
logrus.Tracef(" - extracting tar file: %s into: %s", header.Name, filePath)
if fileInfo.IsDir() {
err = os.MkdirAll(filePath, fileInfo.Mode())
if err != nil {
return "", err
}
continue
} else {
err = os.MkdirAll(filepath.Dir(filePath), 0750)
if err != nil {
return "", err
}
}
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fileInfo.Mode())
if err != nil {
return "", fmt.Errorf("open file error: %w", err)
}
_, err = io.Copy(file, tarReader)
if err != nil {
return "", fmt.Errorf("copy data error: %w", err)
}
if err := file.Close(); err != nil {
return "", fmt.Errorf("close file error: %w", err)
}
}
return tempDir, nil
}