diff --git a/cmd/dagger/cmd/mod/file.go b/cmd/dagger/cmd/mod/file.go index d26727e8..6d905f09 100644 --- a/cmd/dagger/cmd/mod/file.go +++ b/cmd/dagger/cmd/mod/file.go @@ -22,7 +22,7 @@ type file struct { require []*require } -func readModFile(workspacePath string) (*file, error) { +func readPath(workspacePath string) (*file, error) { f, err := os.Open(path.Join(workspacePath, filePath)) if err != nil { return nil, err @@ -93,7 +93,7 @@ func nonEmptyLines(b []byte) []string { 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) } diff --git a/cmd/dagger/cmd/mod/get.go b/cmd/dagger/cmd/mod/get.go index 5ded1cf1..b1b73228 100644 --- a/cmd/dagger/cmd/mod/get.go +++ b/cmd/dagger/cmd/mod/get.go @@ -40,6 +40,12 @@ var getCmd = &cobra.Command{ 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 var packages []*require for _, arg := range args { @@ -52,72 +58,81 @@ var getCmd = &cobra.Command{ 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 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") } + + if isNew { + lg.Info().Msgf("downloading %s:%v", p.repo, p.version) + } } // 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.Info().Msg("checking for new versions...") + <-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) 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) + // clone the repo privateKeyFile := viper.GetString("private-key-file") privateKeyPassword := viper.GetString("private-key-password") r, err := clone(req, tmpPath, privateKeyFile, privateKeyPassword) 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) 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 err := move(req, tmpPath, destPath); err != nil { - return err + return isNew, err } modFile.require = append(modFile.require, req) - return nil + isNew = true + return isNew, nil } c, err := compareVersions(existing.version, req.version) if err != nil { - return err + return isNew, err } // the existing requirement is newer so we skip installation 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 // requirement and replace the code in the /pkg folder existing.version = req.version 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) { @@ -138,7 +153,7 @@ func compareVersions(reqV1, reqV2 string) (int, error) { } 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") if err := viper.BindPFlags(getCmd.Flags()); err != nil { diff --git a/cmd/dagger/cmd/mod/repo.go b/cmd/dagger/cmd/mod/repo.go index e869dceb..a5b7f3f2 100644 --- a/cmd/dagger/cmd/mod/repo.go +++ b/cmd/dagger/cmd/mod/repo.go @@ -45,10 +45,16 @@ func clone(require *require, dir string, privateKeyFile, privateKeyPassword stri } if require.version == "" { - require.version, err = rr.latestTag() + latestTag, err := rr.latestTag() if err != nil { 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 { @@ -114,5 +120,9 @@ func (r *repo) latestTag() (string, error) { sort.Sort(version.Collection(versions)) + if len(versions) == 0 { + return "", nil + } + return versions[len(versions)-1].Original(), nil } diff --git a/cmd/dagger/cmd/mod/repo_test.go b/cmd/dagger/cmd/mod/repo_test.go index e28c6516..7fdbb1ea 100644 --- a/cmd/dagger/cmd/mod/repo_test.go +++ b/cmd/dagger/cmd/mod/repo_test.go @@ -8,8 +8,10 @@ import ( func TestClone(t *testing.T) { cases := []struct { - name string - require require + name string + require require + privateKeyFile string + privateKeyPassword string }{ { 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{ - cloneRepo: "github.com/dagger/dagger", + cloneRepo: "github.com/dagger/test", clonePath: "", - version: "", - - repo: "alpha.dagger.io", + version: "v0.2", }, + privateKeyFile: "./test-ssh-keys/id_ed25519_test", + privateKeyPassword: "", }, } @@ -56,7 +58,7 @@ func TestClone(t *testing.T) { defer os.Remove(tmpDir) - _, err = clone(&c.require, tmpDir, "", "") + _, err = clone(&c.require, tmpDir, c.privateKeyFile, c.privateKeyPassword) if err != nil { t.Error(err) } diff --git a/cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test b/cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test new file mode 100644 index 00000000..58f9f087 --- /dev/null +++ b/cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test @@ -0,0 +1,7 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACCpGsk8WLx7gXCXX1muGhKjlkqaqykF1X198WQMkBO2pwAAAKC5Ec8WuRHP +FgAAAAtzc2gtZWQyNTUxOQAAACCpGsk8WLx7gXCXX1muGhKjlkqaqykF1X198WQMkBO2pw +AAAEBXE9Uht+QHuyK7+yYcZFVWOJ3qkhUh/wn289nDKDPHKakayTxYvHuBcJdfWa4aEqOW +SpqrKQXVfX3xZAyQE7anAAAAGnRpaG9taXIuam92aWNpY0B0b3B0YWwuY29tAQID +-----END OPENSSH PRIVATE KEY----- diff --git a/cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test.pub b/cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test.pub new file mode 100644 index 00000000..28bde217 --- /dev/null +++ b/cmd/dagger/cmd/mod/test-ssh-keys/id_ed25519_test.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKkayTxYvHuBcJdfWa4aEqOWSpqrKQXVfX3xZAyQE7an tihomir.jovicic@toptal.com