Skip to content

Commit c740cbd

Browse files
committed
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 <[email protected]>
1 parent 3aebd28 commit c740cbd

File tree

3 files changed

+95
-35
lines changed

3 files changed

+95
-35
lines changed

actions/apt_action.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package actions
2828

2929
import (
3030
"github.com/go-debos/debos"
31+
"github.com/go-debos/debos/wrapper"
3132
)
3233

3334
type AptAction struct {
@@ -44,50 +45,19 @@ func NewAptAction() *AptAction {
4445
}
4546

4647
func (apt *AptAction) Run(context *debos.DebosContext) error {
47-
aptConfig := []string{}
48-
49-
/* Don't show progress update percentages */
50-
aptConfig = append(aptConfig, "-o=quiet::NoUpdate=1")
51-
52-
aptOptions := []string{"apt-get", "-y"}
53-
aptOptions = append(aptOptions, aptConfig...)
54-
55-
if !apt.Recommends {
56-
aptOptions = append(aptOptions, "--no-install-recommends")
57-
}
58-
59-
if apt.Unauthenticated {
60-
aptOptions = append(aptOptions, "--allow-unauthenticated")
61-
}
62-
63-
aptOptions = append(aptOptions, "install")
64-
aptOptions = append(aptOptions, apt.Packages...)
65-
66-
c := debos.NewChrootCommandForContext(*context)
67-
c.AddEnv("DEBIAN_FRONTEND=noninteractive")
48+
aptCommand := wrapper.NewAptCommand(*context, "apt")
6849

6950
if apt.Update {
70-
cmd := []string{"apt-get"}
71-
cmd = append(cmd, aptConfig...)
72-
cmd = append(cmd, "update")
73-
74-
err := c.Run("apt", cmd...)
75-
if err != nil {
51+
if err := aptCommand.Update(); err != nil {
7652
return err
7753
}
7854
}
7955

80-
err := c.Run("apt", aptOptions...)
81-
if err != nil {
56+
if err := aptCommand.Install(apt.Packages, apt.Recommends, apt.Unauthenticated); err != nil {
8257
return err
8358
}
8459

85-
cmd := []string{"apt-get"}
86-
cmd = append(cmd, aptConfig...)
87-
cmd = append(cmd, "clean")
88-
89-
err = c.Run("apt", cmd...)
90-
if err != nil {
60+
if err := aptCommand.Clean(); err != nil {
9161
return err
9262
}
9363

wrapper/apt_wrapper.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Abstracts the apt command. */
2+
package wrapper
3+
4+
import (
5+
"github.com/go-debos/debos"
6+
)
7+
8+
type AptCommand struct {
9+
Wrapper
10+
}
11+
12+
func NewAptCommand(context debos.DebosContext, label string) AptCommand {
13+
command := "apt-get"
14+
15+
apt := AptCommand{
16+
Wrapper: NewCommandWrapper(context, command, label),
17+
}
18+
19+
apt.AddEnv("DEBIAN_FRONTEND=noninteractive")
20+
21+
/* Don't show progress update percentages */
22+
apt.AppendGlobalArguments("-o=quiet::NoUpdate=1")
23+
24+
return apt
25+
}
26+
27+
func (apt AptCommand) Clean() error {
28+
return apt.Run("clean")
29+
}
30+
31+
func (apt AptCommand) Install(packages []string, recommends bool, unauthenticated bool) error {
32+
arguments := []string{"install", "--yes"}
33+
34+
if !recommends {
35+
arguments = append(arguments, "--no-install-recommends")
36+
}
37+
38+
if unauthenticated {
39+
arguments = append(arguments, "--allow-unauthenticated")
40+
}
41+
42+
arguments = append(arguments, packages...)
43+
44+
return apt.Run(arguments...)
45+
}
46+
47+
func (apt AptCommand) Update() error {
48+
return apt.Run("update")
49+
}

wrapper/wrapper.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Base class to abstract commonly used commands. */
2+
package wrapper
3+
4+
import (
5+
"github.com/go-debos/debos"
6+
)
7+
8+
type Wrapper struct {
9+
debos.Command
10+
command string
11+
globalArgs []string
12+
label string
13+
}
14+
15+
func NewCommandWrapper(context debos.DebosContext, command string, label string) Wrapper {
16+
return Wrapper{
17+
Command: debos.NewChrootCommandForContext(context),
18+
command: command,
19+
label: label,
20+
}
21+
}
22+
23+
func (cmd *Wrapper) SetCommand(command string) {
24+
cmd.command = command
25+
}
26+
27+
func (cmd *Wrapper) AppendGlobalArguments(args string) {
28+
cmd.globalArgs = append(cmd.globalArgs, args)
29+
}
30+
31+
func (cmd *Wrapper) SetLabel(label string) {
32+
cmd.label = label
33+
}
34+
35+
func (cmd Wrapper) Run(additionalArgs ...string) error {
36+
args := []string{cmd.command}
37+
args = append(args, cmd.globalArgs...)
38+
args = append(args, additionalArgs...)
39+
40+
return cmd.Command.Run(cmd.label, args...)
41+
}

0 commit comments

Comments
 (0)