mirror of https://github.com/databricks/cli.git
Add support for cloneing public repositories from github
This commit is contained in:
parent
3c1e69a064
commit
1cc50968bb
|
@ -0,0 +1,59 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/databricks/cli/libs/zip"
|
||||
)
|
||||
|
||||
func githubZipUrl(org string, name string, ref string) string {
|
||||
return fmt.Sprintf(`https://github.com/%s/%s/archive/%s.zip`, org, name, ref)
|
||||
}
|
||||
|
||||
func download(url string, dest string) error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return fmt.Errorf("repository not found: %s", url)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to download ZIP archive: %s. %s", url, resp.Status)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(dest, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
func Clone(org string, repoName string, targetDir string) error {
|
||||
zipDst := filepath.Join(targetDir, repoName+".zip")
|
||||
zipUrl := githubZipUrl(org, repoName, "main")
|
||||
|
||||
// Download public repository from github as a ZIP file
|
||||
err := download(zipUrl, zipDst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Decompress the ZIP file
|
||||
err = zip.Extract(zipDst, targetDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove the ZIP file
|
||||
return os.Remove(zipDst)
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGitClonePublicRepository(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
var err error
|
||||
|
||||
err = Clone("databricks", "cli", tmpDir)
|
||||
assert.NoError(t, err)
|
||||
assert.DirExists(t, filepath.Join(tmpDir, "cli-main"))
|
||||
|
||||
b, err := os.ReadFile(filepath.Join(tmpDir, "cli-main/NOTICE"))
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, string(b), "Copyright (2023) Databricks, Inc.")
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Extract(src string, dst string) error {
|
||||
zipReader, err := zip.OpenReader(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fs.WalkDir(zipReader, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(dst, path)
|
||||
if d.IsDir() {
|
||||
return os.MkdirAll(targetPath, os.ModePerm)
|
||||
}
|
||||
|
||||
targetFile, err := os.Create(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sourceFile, err := zipReader.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(targetFile, sourceFile)
|
||||
return err
|
||||
})
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package zip
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestZipExtract(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
var err error
|
||||
|
||||
err = Extract("./testdata/dir.zip", tmpDir)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.DirExists(t, filepath.Join(tmpDir, "dir"))
|
||||
|
||||
b, err := os.ReadFile(filepath.Join(tmpDir, "dir/a"))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello a\n", string(b))
|
||||
|
||||
b, err = os.ReadFile(filepath.Join(tmpDir, "dir/b"))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello b\n", string(b))
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue