Skip to content

Commit

Permalink
Refine handler interface
Browse files Browse the repository at this point in the history
With a minor re-ordering of the independant operations in rpmtree we
can reduce the surface area of the handler interface.  This enables us
to simply the interface and remove some redundant bits.
  • Loading branch information
kellyma2 committed Jan 17, 2025
1 parent eb55734 commit 64994ae
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions cmd/rpmtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ type rpmtreeOpts struct {
var rpmtreeopts = rpmtreeOpts{}

type Handler interface {
AddRPMs(pkgs []*api.Package, arch string) error
PruneRPMs(buildfile *build.File)
Process(pkgs []*api.Package, arch string, buildfile *build.File) error
Write() error
}

Expand Down Expand Up @@ -62,12 +61,13 @@ func NewMacroHandler(toMacro string) (Handler, error) {
}, nil
}

func (h *MacroHandler) AddRPMs(pkgs []*api.Package, arch string) error {
return bazel.AddBzlfileRPMs(h.bzlfile, h.defName, pkgs, arch)
}
func (h *MacroHandler) Process(pkgs []*api.Package, arch string, buildfile *build.File) error {
if err := bazel.AddBzlfileRPMs(h.bzlfile, h.defName, pkgs, arch); err != nil {
return err
}

func (h *MacroHandler) PruneRPMs(buildfile *build.File) {
bazel.PruneBzlfileRPMs(buildfile, h.bzlfile, h.defName)
return nil
}

func (h *MacroHandler) Write() error {
Expand All @@ -91,12 +91,13 @@ func NewWorkspaceHandler(workspace string) (Handler, error) {
}, nil
}

func (h *WorkspaceHandler) AddRPMs(pkgs []*api.Package, arch string) error {
return bazel.AddWorkspaceRPMs(h.workspacefile, pkgs, arch)
}
func (h *WorkspaceHandler) Process(pkgs []*api.Package, arch string, buildfile *build.File) error {
if err := bazel.AddWorkspaceRPMs(h.workspacefile, pkgs, arch); err != nil {
return err
}

func (h *WorkspaceHandler) PruneRPMs(buildfile *build.File) {
bazel.PruneWorkspaceRPMs(buildfile, h.workspacefile)
return nil
}

func (h *WorkspaceHandler) Write() error {
Expand All @@ -118,14 +119,10 @@ func NewLockFileHandler(configname, filename string) (Handler, error) {
}, nil
}

func (h *LockFileHandler) AddRPMs(pkgs []*api.Package, arch string) error {
func (h *LockFileHandler) Process(pkgs []*api.Package, arch string, buildfile *build.File) error {
return bazel.AddConfigRPMs(h.config, pkgs, arch)
}

func (h *LockFileHandler) PruneRPMs(buildfile *build.File) {
// we always generate from scratch and have nothing to prune
}

func (h *LockFileHandler) Write() error {
return bazel.WriteLockFile(h.config, h.filename)
}
Expand Down Expand Up @@ -192,15 +189,12 @@ func NewRpmTreeCmd() *cobra.Command {
if err != nil {
return err
}
bazel.AddTree(rpmtreeopts.name, configname, build, install, rpmtreeopts.arch, rpmtreeopts.public)

err = handler.AddRPMs(install, rpmtreeopts.arch)
if err != nil {
if err := handler.Process(install, rpmtreeopts.arch, build); err != nil {
return err
}

bazel.AddTree(rpmtreeopts.name, configname, build, install, rpmtreeopts.arch, rpmtreeopts.public)

handler.PruneRPMs(build)
logrus.Info("Writing bazel files.")
err = handler.Write()
if err != nil {
Expand Down

0 comments on commit 64994ae

Please sign in to comment.