Skip to content

Commit

Permalink
actions: apt: Abstract apt commands into separate wrapper
Browse files Browse the repository at this point in the history
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 <chris.obbard@collabora.com>
  • Loading branch information
obbardc committed Jul 27, 2023
1 parent 1ea5f88 commit 2308a42
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 35 deletions.
40 changes: 5 additions & 35 deletions actions/apt_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package actions

import (
"github.com/go-debos/debos"
"github.com/go-debos/debos/wrapper"
)

type AptAction struct {
Expand All @@ -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
}

Expand Down
49 changes: 49 additions & 0 deletions wrapper/apt_wrapper.go
Original file line number Diff line number Diff line change
@@ -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")
}
41 changes: 41 additions & 0 deletions wrapper/wrapper.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit 2308a42

Please sign in to comment.