Skip to content

Commit

Permalink
Merge pull request #39 from andreabolognani/uri-escape
Browse files Browse the repository at this point in the history
Escape URLs before adding them to bazel rules
  • Loading branch information
rmohr authored Mar 15, 2023
2 parents 32db3ee + ced018c commit 1ab2523
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion cmd/rpmtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func NewRpmTreeCmd() *cobra.Command {
if err != nil {
return err
}
bazel.AddRPMs(workspace, install, rpmtreeopts.arch)
err = bazel.AddRPMs(workspace, install, rpmtreeopts.arch)
if err != nil {
return err
}
bazel.AddTree(rpmtreeopts.name, build, install, rpmtreeopts.arch, rpmtreeopts.public)
bazel.PruneRPMs(build, workspace)
logrus.Info("Writing bazel files.")
Expand Down
23 changes: 17 additions & 6 deletions pkg/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bazel
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -63,7 +64,7 @@ func GetRPMs(workspace *build.File) (rpms []*RPMRule) {
return
}

func AddRPMs(workspace *build.File, pkgs []*api.Package, arch string) {
func AddRPMs(workspace *build.File, pkgs []*api.Package, arch string) error {

rpms := map[string]*RPMRule{}

Expand All @@ -83,7 +84,10 @@ func AddRPMs(workspace *build.File, pkgs []*api.Package, arch string) {
rule.SetSHA256(pkg.Checksum.Text)
urls := rule.URLs()
if len(urls) == 0 {
rule.SetURLs(pkg.Repository.Mirrors, pkg.Location.Href)
err := rule.SetURLs(pkg.Repository.Mirrors, pkg.Location.Href)
if err != nil {
return err
}
}
}

Expand All @@ -100,6 +104,8 @@ func AddRPMs(workspace *build.File, pkgs []*api.Package, arch string) {
for _, rule := range rules {
workspace.Stmt = edit.InsertAtEnd(workspace.Stmt, rule.Call)
}

return nil
}

func AddTar2Files(name string, rpmtree string, buildfile *build.File, files []string, public bool) {
Expand Down Expand Up @@ -231,13 +237,18 @@ func (r *RPMRule) URLs() []string {
return nil
}

func (r *RPMRule) SetURLs(urls []string, href string) {
func (r *RPMRule) SetURLs(mirrors []string, href string) error {
urlsAttr := []build.Expr{}
for _, url := range urls {
u := strings.TrimSuffix(url, "/") + "/" + strings.TrimSuffix(href, "/")
urlsAttr = append(urlsAttr, &build.StringExpr{Value: u})
for _, mirror := range mirrors {
u, err := url.Parse(mirror)
if err != nil {
return err
}
u = u.JoinPath(href)
urlsAttr = append(urlsAttr, &build.StringExpr{Value: u.String()})
}
r.Rule.SetAttr("urls", &build.ListExpr{List: urlsAttr})
return nil
}

func (r *RPMRule) SetName(name string) {
Expand Down

0 comments on commit 1ab2523

Please sign in to comment.