From 2308a4221e0cea3d39e3c0453da0223069bc784f Mon Sep 17 00:00:00 2001 From: Christopher Obbard Date: Thu, 27 Jul 2023 12:00:34 +0100 Subject: [PATCH] actions: apt: Abstract apt commands into separate wrapper The apt commands will also be used by the install-dpkg wrapper, prepare for this by abstracting the generic apt commands into a wrapper class. Also create a wrapper base class to be able to abstract common action commands into generic functions. Signed-off-by: Christopher Obbard --- actions/apt_action.go | 40 +++++----------------------------- wrapper/apt_wrapper.go | 49 ++++++++++++++++++++++++++++++++++++++++++ wrapper/wrapper.go | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 wrapper/apt_wrapper.go create mode 100644 wrapper/wrapper.go diff --git a/actions/apt_action.go b/actions/apt_action.go index 237cf6bd..afa0061c 100644 --- a/actions/apt_action.go +++ b/actions/apt_action.go @@ -28,6 +28,7 @@ package actions import ( "github.com/go-debos/debos" + "github.com/go-debos/debos/wrapper" ) type AptAction struct { @@ -46,50 +47,19 @@ func NewAptAction() *AptAction { func (apt *AptAction) Run(context *debos.DebosContext) error { apt.LogStart() - aptConfig := []string{} - - /* Don't show progress update percentages */ - aptConfig = append(aptConfig, "-o=quiet::NoUpdate=1") - - aptOptions := []string{"apt-get", "-y"} - aptOptions = append(aptOptions, aptConfig...) - - if !apt.Recommends { - aptOptions = append(aptOptions, "--no-install-recommends") - } - - if apt.Unauthenticated { - aptOptions = append(aptOptions, "--allow-unauthenticated") - } - - aptOptions = append(aptOptions, "install") - aptOptions = append(aptOptions, apt.Packages...) - - c := debos.NewChrootCommandForContext(*context) - c.AddEnv("DEBIAN_FRONTEND=noninteractive") + aptCommand := wrapper.NewAptCommand(*context, "apt") if apt.Update { - cmd := []string{"apt-get"} - cmd = append(cmd, aptConfig...) - cmd = append(cmd, "update") - - err := c.Run("apt", cmd...) - if err != nil { + if err := aptCommand.Update(); err != nil { return err } } - err := c.Run("apt", aptOptions...) - if err != nil { + if err := aptCommand.Install(apt.Packages, apt.Recommends, apt.Unauthenticated); err != nil { return err } - cmd := []string{"apt-get"} - cmd = append(cmd, aptConfig...) - cmd = append(cmd, "clean") - - err = c.Run("apt", cmd...) - if err != nil { + if err := aptCommand.Clean(); err != nil { return err } diff --git a/wrapper/apt_wrapper.go b/wrapper/apt_wrapper.go new file mode 100644 index 00000000..19aa87a9 --- /dev/null +++ b/wrapper/apt_wrapper.go @@ -0,0 +1,49 @@ +/* Abstracts the apt command. */ +package wrapper + +import ( + "github.com/go-debos/debos" +) + +type AptCommand struct { + Wrapper +} + +func NewAptCommand(context debos.DebosContext, label string) AptCommand { + command := "apt-get" + + apt := AptCommand{ + Wrapper: NewCommandWrapper(context, command, label), + } + + apt.AddEnv("DEBIAN_FRONTEND=noninteractive") + + /* Don't show progress update percentages */ + apt.AppendGlobalArguments("-o=quiet::NoUpdate=1") + + return apt +} + +func (apt AptCommand) Clean() error { + return apt.Run("clean") +} + +func (apt AptCommand) Install(packages []string, recommends bool, unauthenticated bool) error { + arguments := []string{"install", "--yes"} + + if !recommends { + arguments = append(arguments, "--no-install-recommends") + } + + if unauthenticated { + arguments = append(arguments, "--allow-unauthenticated") + } + + arguments = append(arguments, packages...) + + return apt.Run(arguments...) +} + +func (apt AptCommand) Update() error { + return apt.Run("update") +} diff --git a/wrapper/wrapper.go b/wrapper/wrapper.go new file mode 100644 index 00000000..9346056c --- /dev/null +++ b/wrapper/wrapper.go @@ -0,0 +1,41 @@ +/* Base class to abstract commonly used commands. */ +package wrapper + +import ( + "github.com/go-debos/debos" +) + +type Wrapper struct { + debos.Command + command string + globalArgs []string + label string +} + +func NewCommandWrapper(context debos.DebosContext, command string, label string) Wrapper { + return Wrapper{ + Command: debos.NewChrootCommandForContext(context), + command: command, + label: label, + } +} + +func (cmd *Wrapper) SetCommand(command string) { + cmd.command = command +} + +func (cmd *Wrapper) AppendGlobalArguments(args string) { + cmd.globalArgs = append(cmd.globalArgs, args) +} + +func (cmd *Wrapper) SetLabel(label string) { + cmd.label = label +} + +func (cmd Wrapper) Run(additionalArgs ...string) error { + args := []string{cmd.command} + args = append(args, cmd.globalArgs...) + args = append(args, additionalArgs...) + + return cmd.Command.Run(cmd.label, args...) +}