Skip to content

Commit

Permalink
Add support for +v2 modules
Browse files Browse the repository at this point in the history
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
Signed-off-by: Waldemar Quevedo <waldemar.quevedo@gmail.com>
  • Loading branch information
wallyqs committed May 15, 2020
1 parent 1d14a73 commit 6f511f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
25 changes: 24 additions & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/tj/gobinaries"
Expand Down Expand Up @@ -71,7 +72,7 @@ func Write(w io.Writer, bin gobinaries.Binary) error {
}

// add the dependency
err = addModuleDep(dir, bin.Module+"@"+bin.Version)
err = addModuleDep(dir, normalizeModuleDep(bin))
if err != nil {
return fmt.Errorf("adding dependency: %w", err)
}
Expand Down Expand Up @@ -143,6 +144,28 @@ func addModule(dir string) error {
return command(cmd)
}

// getMajorVersion tries to detect the major version of the package.
func getMajorVersion(tag string) (int, error) {
major := strings.Split(tag, ".")[0]
if len(major) < 1 {
return 0, fmt.Errorf("invalid major version")
}
return strconv.Atoi(major[1:])
}

func normalizeModuleDep(bin gobinaries.Binary) string {
mod := bin.Module
version := bin.Version
var dep string
major, err := getMajorVersion(version)
if err == nil && major > 1 {
dep = fmt.Sprintf("%s/v%d@%s", mod, major, version)
} else {
dep = fmt.Sprintf("%s@%s", mod, version)
}
return dep
}

// addModuleDep creates a module dependency.
func addModuleDep(dir, dep string) error {
cmd := exec.Command("go", "mod", "edit", "-require", dep)
Expand Down
12 changes: 12 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package server
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -146,6 +147,17 @@ func (s *Server) getScript(w http.ResponseWriter, r *http.Request) {
logs = logs.WithField("resolved", resolved)
logs.Info("resolved version")

// rename package into go mod compatible name if v2 and above
major, err := getMajorVersion(resolved)
if err == nil && major > 1 {
modp := strings.Split(pkg, "/")
if len(modp) >= 3 {
mod := strings.Join(modp[:3], "/")
nested := strings.Join(modp[3:], "/")
pkg = fmt.Sprintf("%s/v%d/%s", mod, major, nested)
}
}

s.render(w, "install.sh", struct {
URL string
Package string
Expand Down
11 changes: 11 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package server

import (
"fmt"
"strconv"
"strings"
)

// getMajorVersion tries to detect the major version of the package.
func getMajorVersion(tag string) (int, error) {
major := strings.Split(tag, ".")[0]
if len(major) < 1 {
return 0, fmt.Errorf("invalid major version")
}
return strconv.Atoi(major[1:])
}

// parsePackage returns package information parsed from the path.
func parsePackage(path string) (pkg, mod, version, bin string) {
p := strings.Split(path, "@")
Expand Down

0 comments on commit 6f511f2

Please sign in to comment.