diff --git a/cmd/rpmtree.go b/cmd/rpmtree.go index a1127ec..ebda4e6 100644 --- a/cmd/rpmtree.go +++ b/cmd/rpmtree.go @@ -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.") diff --git a/pkg/bazel/bazel.go b/pkg/bazel/bazel.go index 86aeee3..c44695e 100644 --- a/pkg/bazel/bazel.go +++ b/pkg/bazel/bazel.go @@ -3,6 +3,7 @@ package bazel import ( "fmt" "io/ioutil" + "net/url" "path/filepath" "sort" "strings" @@ -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{} @@ -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 + } } } @@ -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) { @@ -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) {