Skip to content

Commit 9f72032

Browse files
committed
add util.go for download from git
Signed-off-by: Stephanie <[email protected]>
1 parent a86df62 commit 9f72032

File tree

504 files changed

+89444
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

504 files changed

+89444
-50
lines changed

index/generator/go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module github.com/devfile/registry-support/index/generator
33
go 1.14
44

55
require (
6-
github.com/devfile/api/v2 v2.0.0-20211021164004-dabee4e633ed
7-
github.com/devfile/library v1.2.1-0.20211104222135-49d635cb492f
6+
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c
7+
github.com/devfile/library v1.2.1-0.20220215173721-fc8a5b4f2768
8+
github.com/go-git/go-git/v5 v5.4.2
89
github.com/mitchellh/go-homedir v1.1.0
910
github.com/spf13/cobra v1.1.1
1011
github.com/spf13/viper v1.7.1

index/generator/go.sum

Lines changed: 52 additions & 0 deletions
Large diffs are not rendered by default.

index/generator/library/util.go

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package library
2+
3+
import (
4+
"github.com/devfile/registry-support/index/generator/schema"
5+
gitpkg "github.com/go-git/go-git/v5"
6+
"github.com/go-git/go-git/v5/plumbing"
7+
"io"
8+
"io/ioutil"
9+
"os"
10+
"os/signal"
11+
"path"
12+
"path/filepath"
13+
"syscall"
14+
"fmt"
15+
"github.com/devfile/library/pkg/testingutil/filesystem"
16+
)
17+
18+
// downloadRemoteStack downloads the stack version outside of the registry repo
19+
func downloadRemoteStack(git *schema.Git, path string, verbose bool) (err error) {
20+
21+
// convert revision to referenceName type, ref name could be a branch or tag
22+
// if revision is not specified it would be the default branch of the project
23+
revision := git.Revision
24+
refName := plumbing.ReferenceName(git.Revision)
25+
26+
if plumbing.IsHash(revision) {
27+
// Specifying commit in the reference name is not supported by the go-git library
28+
// while doing git.PlainClone()
29+
fmt.Printf("Specifying commit in 'revision' is not yet supported.")
30+
// overriding revision to empty as we do not support this
31+
revision = ""
32+
}
33+
34+
if revision != "" {
35+
// lets consider revision to be a branch name first
36+
refName = plumbing.NewBranchReferenceName(revision)
37+
}
38+
39+
40+
cloneOptions := &gitpkg.CloneOptions{
41+
URL: git.Url,
42+
RemoteName: git.RemoteName,
43+
ReferenceName: refName,
44+
SingleBranch: true,
45+
// we don't need history for starter projects
46+
Depth: 1,
47+
}
48+
49+
originalPath := ""
50+
if git.SubDir != "" {
51+
originalPath = path
52+
path, err = ioutil.TempDir("", "")
53+
if err != nil {
54+
return err
55+
}
56+
}
57+
58+
_, err = gitpkg.PlainClone(path, false, cloneOptions)
59+
60+
if err != nil {
61+
62+
// it returns the following error if no matching ref found
63+
// if we get this error, we are trying again considering revision as tag, only if revision is specified.
64+
if _, ok := err.(gitpkg.NoMatchingRefSpecError); !ok || revision == "" {
65+
return err
66+
}
67+
68+
// try again to consider revision as tag name
69+
cloneOptions.ReferenceName = plumbing.NewTagReferenceName(revision)
70+
// remove if any .git folder downloaded in above try
71+
_ = os.RemoveAll(filepath.Join(path, ".git"))
72+
_, err = gitpkg.PlainClone(path, false, cloneOptions)
73+
if err != nil {
74+
return err
75+
}
76+
}
77+
78+
// we don't want to download project be a git repo
79+
err = os.RemoveAll(filepath.Join(path, ".git"))
80+
if err != nil {
81+
// we don't need to return (fail) if this happens
82+
fmt.Printf("Unable to delete .git from cloned devfile repository")
83+
}
84+
85+
if git.SubDir != "" {
86+
err = GitSubDir(path, originalPath,
87+
git.SubDir)
88+
if err != nil {
89+
return err
90+
}
91+
}
92+
93+
return nil
94+
95+
}
96+
97+
// GitSubDir handles subDir for git components using the default filesystem
98+
func GitSubDir(srcPath, destinationPath, subDir string) error {
99+
return gitSubDir(srcPath, destinationPath, subDir, filesystem.DefaultFs{})
100+
}
101+
102+
// gitSubDir handles subDir for git components
103+
func gitSubDir(srcPath, destinationPath, subDir string, fs filesystem.Filesystem) error {
104+
go StartSignalWatcher([]os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt}, func(_ os.Signal) {
105+
err := cleanDir(destinationPath, map[string]bool{
106+
"devfile.yaml": true,
107+
}, fs)
108+
if err != nil {
109+
fmt.Printf("error %v occurred while calling handleInterruptedSubDir", err)
110+
}
111+
err = fs.RemoveAll(srcPath)
112+
if err != nil {
113+
fmt.Printf("error %v occurred during temp folder clean up", err)
114+
}
115+
})
116+
117+
err := func() error {
118+
// Open the directory.
119+
outputDirRead, err := fs.Open(filepath.Join(srcPath, subDir))
120+
if err != nil {
121+
return err
122+
}
123+
defer func() {
124+
if err1 := outputDirRead.Close(); err1 != nil {
125+
fmt.Printf("err occurred while closing temp dir: %v", err1)
126+
127+
}
128+
}()
129+
// Call Readdir to get all files.
130+
outputDirFiles, err := outputDirRead.Readdir(0)
131+
if err != nil {
132+
return err
133+
}
134+
135+
// Loop over files.
136+
for outputIndex := range outputDirFiles {
137+
outputFileHere := outputDirFiles[outputIndex]
138+
139+
// Get name of file.
140+
fileName := outputFileHere.Name()
141+
142+
oldPath := filepath.Join(srcPath, subDir, fileName)
143+
144+
if outputFileHere.IsDir() {
145+
err = copyDirWithFS(oldPath, filepath.Join(destinationPath, fileName), fs)
146+
} else {
147+
err = copyFileWithFs(oldPath, filepath.Join(destinationPath, fileName), fs)
148+
}
149+
150+
if err != nil {
151+
return err
152+
}
153+
}
154+
return nil
155+
}()
156+
if err != nil {
157+
return err
158+
}
159+
return fs.RemoveAll(srcPath)
160+
}
161+
162+
// copyFileWithFs copies a single file from src to dst
163+
func copyFileWithFs(src, dst string, fs filesystem.Filesystem) error {
164+
var err error
165+
var srcinfo os.FileInfo
166+
167+
srcfd, err := fs.Open(src)
168+
if err != nil {
169+
return err
170+
}
171+
defer func() {
172+
if e := srcfd.Close(); e != nil {
173+
fmt.Printf("err occurred while closing file: %v", e)
174+
}
175+
}()
176+
177+
dstfd, err := fs.Create(dst)
178+
if err != nil {
179+
return err
180+
}
181+
defer func() {
182+
if e := dstfd.Close(); e != nil {
183+
fmt.Printf("err occurred while closing file: %v", e)
184+
}
185+
}()
186+
187+
if _, err = io.Copy(dstfd, srcfd); err != nil {
188+
return err
189+
}
190+
if srcinfo, err = fs.Stat(src); err != nil {
191+
return err
192+
}
193+
return fs.Chmod(dst, srcinfo.Mode())
194+
}
195+
196+
// copyDirWithFS copies a whole directory recursively
197+
func copyDirWithFS(src string, dst string, fs filesystem.Filesystem) error {
198+
var err error
199+
var fds []os.FileInfo
200+
var srcinfo os.FileInfo
201+
202+
if srcinfo, err = fs.Stat(src); err != nil {
203+
return err
204+
}
205+
206+
if err = fs.MkdirAll(dst, srcinfo.Mode()); err != nil {
207+
return err
208+
}
209+
210+
if fds, err = fs.ReadDir(src); err != nil {
211+
return err
212+
}
213+
for _, fd := range fds {
214+
srcfp := path.Join(src, fd.Name())
215+
dstfp := path.Join(dst, fd.Name())
216+
217+
if fd.IsDir() {
218+
if err = copyDirWithFS(srcfp, dstfp, fs); err != nil {
219+
return err
220+
}
221+
} else {
222+
if err = copyFileWithFs(srcfp, dstfp, fs); err != nil {
223+
return err
224+
}
225+
}
226+
}
227+
return nil
228+
}
229+
230+
// StartSignalWatcher watches for signals and handles the situation before exiting the program
231+
func StartSignalWatcher(watchSignals []os.Signal, handle func(receivedSignal os.Signal)) {
232+
signals := make(chan os.Signal, 1)
233+
signal.Notify(signals, watchSignals...)
234+
defer signal.Stop(signals)
235+
236+
receivedSignal := <-signals
237+
handle(receivedSignal)
238+
// exit here to stop spinners from rotating
239+
os.Exit(1)
240+
}
241+
242+
// cleanDir cleans the original folder during events like interrupted copy etc
243+
// it leaves the given files behind for later use
244+
func cleanDir(originalPath string, leaveBehindFiles map[string]bool, fs filesystem.Filesystem) error {
245+
// Open the directory.
246+
outputDirRead, err := fs.Open(originalPath)
247+
if err != nil {
248+
return err
249+
}
250+
251+
// Call Readdir to get all files.
252+
outputDirFiles, err := outputDirRead.Readdir(0)
253+
if err != nil {
254+
return err
255+
}
256+
257+
// Loop over files.
258+
for _, file := range outputDirFiles {
259+
if value, ok := leaveBehindFiles[file.Name()]; ok && value {
260+
continue
261+
}
262+
err = fs.RemoveAll(filepath.Join(originalPath, file.Name()))
263+
if err != nil {
264+
return err
265+
}
266+
}
267+
return err
268+
}

index/generator/schema/schema.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ type Devfile struct {
116116

117117
// Git stores the information of remote repositories
118118
type Git struct {
119-
Remotes map[string]string `yaml:"remotes,omitempty" json:"remotes,omitempty"`
119+
Remotes map[string]string `yaml:"remotes,omitempty" json:"remotes,omitempty"`
120+
Url string `yaml:"url,omitempty" json:"url,omitempty"`
121+
RemoteName string `yaml:"remoteName,omitempty" json:"remoteName,omitempty"`
122+
SubDir string `yaml:"subDir,omitempty" json:"subDir,omitempty"`
123+
Revision string `yaml:"revision,omitempty" json:"revision,omitempty"`
120124
}
121125

122126
// ExtraDevfileEntries is the extraDevfileEntries structure that is used by index component

index/generator/vendor/github.com/Microsoft/go-winio/.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index/generator/vendor/github.com/Microsoft/go-winio/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index/generator/vendor/github.com/Microsoft/go-winio/README.md

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)