test pulling private git repos using mod get command
Signed-off-by: Tihomir Jovicic <tihomir.jovicic.develop@gmail.com>
This commit is contained in:
parent
050d1fcb3f
commit
2102e78c1f
@ -22,7 +22,7 @@ type file struct {
|
|||||||
require []*require
|
require []*require
|
||||||
}
|
}
|
||||||
|
|
||||||
func readModFile(workspacePath string) (*file, error) {
|
func readPath(workspacePath string) (*file, error) {
|
||||||
f, err := os.Open(path.Join(workspacePath, filePath))
|
f, err := os.Open(path.Join(workspacePath, filePath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -93,7 +93,7 @@ func nonEmptyLines(b []byte) []string {
|
|||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeModFile(workspacePath string, f *file) error {
|
func (f *file) write(workspacePath string) error {
|
||||||
return ioutil.WriteFile(path.Join(workspacePath, filePath), f.contents().Bytes(), 0600)
|
return ioutil.WriteFile(path.Join(workspacePath, filePath), f.contents().Bytes(), 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +40,12 @@ var getCmd = &cobra.Command{
|
|||||||
Value: args,
|
Value: args,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// read mod file in the current dir
|
||||||
|
modFile, err := readPath(workspace.Path)
|
||||||
|
if err != nil {
|
||||||
|
lg.Fatal().Err(err).Msg("error loading module file")
|
||||||
|
}
|
||||||
|
|
||||||
// parse packages to install
|
// parse packages to install
|
||||||
var packages []*require
|
var packages []*require
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
@ -52,72 +58,81 @@ var getCmd = &cobra.Command{
|
|||||||
packages = append(packages, p)
|
packages = append(packages, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// read mod file in the current dir
|
|
||||||
modFile, err := readModFile(workspace.Path)
|
|
||||||
if err != nil {
|
|
||||||
lg.Fatal().Err(err).Msgf("error loading module file")
|
|
||||||
}
|
|
||||||
|
|
||||||
// download packages
|
// download packages
|
||||||
for _, p := range packages {
|
for _, p := range packages {
|
||||||
if err := processRequire(workspace.Path, p, modFile); err != nil {
|
isNew, err := processRequire(workspace.Path, p, modFile)
|
||||||
|
if err != nil {
|
||||||
lg.Error().Err(err).Msg("error processing package")
|
lg.Error().Err(err).Msg("error processing package")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isNew {
|
||||||
|
lg.Info().Msgf("downloading %s:%v", p.repo, p.version)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to mod file in the current dir
|
// write to mod file in the current dir
|
||||||
if err = writeModFile(workspace.Path, modFile); err != nil {
|
if err = modFile.write(workspace.Path); err != nil {
|
||||||
lg.Error().Err(err).Msg("error writing to mod file")
|
lg.Error().Err(err).Msg("error writing to mod file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lg.Info().Msg("checking for new versions...")
|
||||||
|
|
||||||
<-doneCh
|
<-doneCh
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func processRequire(workspacePath string, req *require, modFile *file) error {
|
func processRequire(workspacePath string, req *require, modFile *file) (bool, error) {
|
||||||
|
var isNew bool
|
||||||
|
|
||||||
tmpPath := path.Join(workspacePath, tmpBasePath, req.repo)
|
tmpPath := path.Join(workspacePath, tmpBasePath, req.repo)
|
||||||
if err := os.MkdirAll(tmpPath, 0755); err != nil {
|
if err := os.MkdirAll(tmpPath, 0755); err != nil {
|
||||||
return fmt.Errorf("error creating tmp dir for cloning package")
|
return false, fmt.Errorf("error creating tmp dir for cloning package")
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tmpPath)
|
defer os.RemoveAll(tmpPath)
|
||||||
|
|
||||||
|
// clone the repo
|
||||||
privateKeyFile := viper.GetString("private-key-file")
|
privateKeyFile := viper.GetString("private-key-file")
|
||||||
privateKeyPassword := viper.GetString("private-key-password")
|
privateKeyPassword := viper.GetString("private-key-password")
|
||||||
r, err := clone(req, tmpPath, privateKeyFile, privateKeyPassword)
|
r, err := clone(req, tmpPath, privateKeyFile, privateKeyPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error downloading package %s: %w", req, err)
|
return isNew, fmt.Errorf("error downloading package %s: %w", req, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
existing := modFile.search(req)
|
existing := modFile.search(req)
|
||||||
destPath := path.Join(workspacePath, destBasePath)
|
destPath := path.Join(workspacePath, destBasePath)
|
||||||
|
|
||||||
// requirement is new, so we should move the files and add it to the module.cue
|
// requirement is new, so we should move the files and add it to the mod file
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
if err := move(req, tmpPath, destPath); err != nil {
|
if err := move(req, tmpPath, destPath); err != nil {
|
||||||
return err
|
return isNew, err
|
||||||
}
|
}
|
||||||
modFile.require = append(modFile.require, req)
|
modFile.require = append(modFile.require, req)
|
||||||
return nil
|
isNew = true
|
||||||
|
return isNew, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := compareVersions(existing.version, req.version)
|
c, err := compareVersions(existing.version, req.version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return isNew, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// the existing requirement is newer so we skip installation
|
// the existing requirement is newer so we skip installation
|
||||||
if c > 0 {
|
if c > 0 {
|
||||||
return nil
|
return isNew, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// the new requirement is newer so we checkout the cloned repo to that tag, change the version in the existing
|
// the new requirement is newer so we checkout the cloned repo to that tag, change the version in the existing
|
||||||
// requirement and replace the code in the /pkg folder
|
// requirement and replace the code in the /pkg folder
|
||||||
existing.version = req.version
|
existing.version = req.version
|
||||||
if err = r.checkout(req.version); err != nil {
|
if err = r.checkout(req.version); err != nil {
|
||||||
return err
|
return isNew, err
|
||||||
}
|
}
|
||||||
|
if err = replace(req, tmpPath, destPath); err != nil {
|
||||||
|
return isNew, err
|
||||||
|
}
|
||||||
|
isNew = true
|
||||||
|
|
||||||
return replace(req, tmpPath, destPath)
|
return isNew, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareVersions(reqV1, reqV2 string) (int, error) {
|
func compareVersions(reqV1, reqV2 string) (int, error) {
|
||||||
@ -138,7 +153,7 @@ func compareVersions(reqV1, reqV2 string) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
getCmd.Flags().String("private-key-file", "~/.ssh/id_rsa", "Private ssh key")
|
getCmd.Flags().String("private-key-file", "", "Private ssh key")
|
||||||
getCmd.Flags().String("private-key-password", "", "Private ssh key password")
|
getCmd.Flags().String("private-key-password", "", "Private ssh key password")
|
||||||
|
|
||||||
if err := viper.BindPFlags(getCmd.Flags()); err != nil {
|
if err := viper.BindPFlags(getCmd.Flags()); err != nil {
|
||||||
|
@ -45,10 +45,16 @@ func clone(require *require, dir string, privateKeyFile, privateKeyPassword stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
if require.version == "" {
|
if require.version == "" {
|
||||||
require.version, err = rr.latestTag()
|
latestTag, err := rr.latestTag()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if latestTag == "" {
|
||||||
|
return nil, fmt.Errorf("no git tags found in the repo")
|
||||||
|
}
|
||||||
|
|
||||||
|
require.version = latestTag
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := rr.checkout(require.version); err != nil {
|
if err := rr.checkout(require.version); err != nil {
|
||||||
@ -114,5 +120,9 @@ func (r *repo) latestTag() (string, error) {
|
|||||||
|
|
||||||
sort.Sort(version.Collection(versions))
|
sort.Sort(version.Collection(versions))
|
||||||
|
|
||||||
|
if len(versions) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
return versions[len(versions)-1].Original(), nil
|
return versions[len(versions)-1].Original(), nil
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,10 @@ import (
|
|||||||
|
|
||||||
func TestClone(t *testing.T) {
|
func TestClone(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
require require
|
require require
|
||||||
|
privateKeyFile string
|
||||||
|
privateKeyPassword string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "resolving shorter hash version",
|
name: "resolving shorter hash version",
|
||||||
@ -36,14 +38,14 @@ func TestClone(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "alpha.dagger.io",
|
name: "Dagger private test repo",
|
||||||
require: require{
|
require: require{
|
||||||
cloneRepo: "github.com/dagger/dagger",
|
cloneRepo: "github.com/dagger/test",
|
||||||
clonePath: "",
|
clonePath: "",
|
||||||
version: "",
|
version: "v0.2",
|
||||||
|
|
||||||
repo: "alpha.dagger.io",
|
|
||||||
},
|
},
|
||||||
|
privateKeyFile: "./test-ssh-keys/id_ed25519_test",
|
||||||
|
privateKeyPassword: "",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +58,7 @@ func TestClone(t *testing.T) {
|
|||||||
|
|
||||||
defer os.Remove(tmpDir)
|
defer os.Remove(tmpDir)
|
||||||
|
|
||||||
_, err = clone(&c.require, tmpDir, "", "")
|
_, err = clone(&c.require, tmpDir, c.privateKeyFile, c.privateKeyPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
7
cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test
Normal file
7
cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||||
|
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||||
|
QyNTUxOQAAACCpGsk8WLx7gXCXX1muGhKjlkqaqykF1X198WQMkBO2pwAAAKC5Ec8WuRHP
|
||||||
|
FgAAAAtzc2gtZWQyNTUxOQAAACCpGsk8WLx7gXCXX1muGhKjlkqaqykF1X198WQMkBO2pw
|
||||||
|
AAAEBXE9Uht+QHuyK7+yYcZFVWOJ3qkhUh/wn289nDKDPHKakayTxYvHuBcJdfWa4aEqOW
|
||||||
|
SpqrKQXVfX3xZAyQE7anAAAAGnRpaG9taXIuam92aWNpY0B0b3B0YWwuY29tAQID
|
||||||
|
-----END OPENSSH PRIVATE KEY-----
|
1
cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test.pub
Normal file
1
cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test.pub
Normal file
@ -0,0 +1 @@
|
|||||||
|
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKkayTxYvHuBcJdfWa4aEqOWSpqrKQXVfX3xZAyQE7an tihomir.jovicic@toptal.com
|
Reference in New Issue
Block a user