Skip to content

Commit bc17388

Browse files
authored
fix: add context param to operations (#48)
* refactor: add command context to commander interface * fix: add context to updatestackcatalog * fix: add context to image build service * refactor: de-duplicate exec initiation * fix: add context to installer runscripts * fix: add context to sesion script updation
1 parent 7f9726e commit bc17388

13 files changed

+72
-32
lines changed

cmd/appstreamfile/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func run(ctx context.Context, opts *RunOptions) error {
8383
return fmt.Errorf("config file validation failed: %w", err)
8484
}
8585

86-
err = service.ImplementConfig(config)
86+
err = service.ImplementConfig(ctx, config)
8787

8888
if err != nil {
8989
return fmt.Errorf("error setting up config: %w", err)

internal/execx/execx.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package execx
22

3+
import "context"
4+
35
type Commander interface {
46
LookPath(file string) (string, error)
57
Command(name string, arg ...string) Cmd
8+
CommandContext(ctx context.Context, name string, arg ...string) Cmd
69
}
710

811
type Cmd interface {

internal/execx/osexec_commander.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package execx
22

3-
import "os/exec"
3+
import (
4+
"context"
5+
"os/exec"
6+
)
47

58
type ExecCmd struct {
69
*exec.Cmd
@@ -10,7 +13,7 @@ func (c *ExecCmd) CombinedOutput() ([]byte, error) {
1013
return c.Cmd.CombinedOutput()
1114
}
1215

13-
func (c *ExecCmd) String() string{
16+
func (c *ExecCmd) String() string {
1417
return c.Cmd.String()
1518
}
1619

@@ -23,3 +26,7 @@ func (ex *ExecCommander) LookPath(file string) (string, error) {
2326
func (ex *ExecCommander) Command(name string, arg ...string) Cmd {
2427
return &ExecCmd{exec.Command(name, arg...)}
2528
}
29+
30+
func (ex *ExecCommander) CommandContext(context context.Context, name string, arg ...string) Cmd {
31+
return &ExecCmd{exec.CommandContext(context, name, arg...)}
32+
}

internal/service/catalog_service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/aslamcodes/appstreamfile/internal/config"
@@ -11,7 +12,7 @@ type UpdateStackCatalogSvc struct {
1112
Exec execx.Commander
1213
}
1314

14-
func (svc *UpdateStackCatalogSvc) UpdateStackCatalog(c config.CatalogConfig) error {
15+
func (svc *UpdateStackCatalogSvc) UpdateStackCatalog(ctx context.Context, c config.CatalogConfig) error {
1516
fmt.Println("\nConfiguring stack catalog")
1617
fmt.Println(c)
1718

@@ -23,7 +24,7 @@ func (svc *UpdateStackCatalogSvc) UpdateStackCatalog(c config.CatalogConfig) err
2324

2425
args := append([]string{"add-application"}, c.Args()...)
2526

26-
cmd := svc.Exec.Command(IMAGE_ASSISTANT, args...)
27+
cmd := svc.Exec.CommandContext(ctx, IMAGE_ASSISTANT, args...)
2728

2829
output, err := cmd.CombinedOutput()
2930

internal/service/catalog_service_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service_test
22

33
import (
4+
"context"
45
"errors"
56
"slices"
67
"testing"
@@ -10,6 +11,8 @@ import (
1011
)
1112

1213
func TestCatalogConfigSuccess(t *testing.T) {
14+
ctx := context.TODO()
15+
1316
fc := &FakeCommander{
1417
LookPathErr: nil,
1518
}
@@ -27,7 +30,7 @@ func TestCatalogConfigSuccess(t *testing.T) {
2730
Exec: fc,
2831
}
2932

30-
updateCatalogSvc.UpdateStackCatalog(catalog)
33+
updateCatalogSvc.UpdateStackCatalog(ctx, catalog)
3134

3235
if !slices.Equal(catalog.Args(), fc.LastArgs[1:]) {
3336
t.Errorf("expected %v\n, got %v", catalog.Args(), fc.LastArgs[1:])
@@ -36,6 +39,8 @@ func TestCatalogConfigSuccess(t *testing.T) {
3639
}
3740

3841
func TestCatalogConfigFail(t *testing.T) {
42+
ctx := context.TODO()
43+
3944
fc := &FakeCommander{
4045
LookPathErr: errors.New("file not found"),
4146
}
@@ -53,7 +58,7 @@ func TestCatalogConfigFail(t *testing.T) {
5358
Exec: fc,
5459
}
5560

56-
err := updateCatalogSvc.UpdateStackCatalog(catalog)
61+
err := updateCatalogSvc.UpdateStackCatalog(ctx, catalog)
5762

5863
if !errors.Is(fc.LookPathErr, err) {
5964
t.Errorf("expected %v\n, got %v", fc.LookPathErr, err)

internal/service/config_service.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/aslamcodes/appstreamfile/internal/config"
@@ -9,24 +10,20 @@ import (
910

1011
const IMAGE_ASSISTANT = "image-assistant"
1112

12-
func ImplementConfig(c *config.Config) error {
13+
func ImplementConfig(ctx context.Context, c *config.Config) error {
14+
execCmd := &execx.ExecCommander{}
15+
1316
services := &services{
1417
FileDeploySvc: &FileDeploySvc{},
1518
SessionScriptService: &SessionScriptSvc{},
16-
CatalogSvc: &UpdateStackCatalogSvc{
17-
Exec: &execx.ExecCommander{},
18-
},
19-
ImageBuildService: &ImageBuildSvc{
20-
Exec: &execx.ExecCommander{},
21-
},
22-
InstallerService: &InstallerSvc{
23-
Exec: &execx.ExecCommander{},
24-
},
19+
CatalogSvc: &UpdateStackCatalogSvc{Exec: execCmd},
20+
ImageBuildService: &ImageBuildSvc{Exec: execCmd},
21+
InstallerService: &InstallerSvc{Exec: execCmd},
2522
}
2623

2724
for _, i := range c.Installers {
2825
fmt.Println("Executing installer with", i.Executable)
29-
err := services.InstallerService.InstallScript(&i)
26+
err := services.InstallerService.InstallScript(ctx, &i)
3027

3128
if err != nil {
3229
return fmt.Errorf("error executing %s script\n%s: %w", i.Executable, i.InstallScript, err)
@@ -42,17 +39,17 @@ func ImplementConfig(c *config.Config) error {
4239
}
4340
}
4441

45-
if err := services.SessionScriptService.UpdateSessionScriptConfig(SessionScriptLocation(), c.SessionScripts); err != nil {
42+
if err := services.SessionScriptService.UpdateSessionScriptConfig(ctx, SessionScriptLocation(), c.SessionScripts); err != nil {
4643
return fmt.Errorf("error configuring session scripts: %w", err)
4744
}
4845

4946
for _, catalog := range c.Catalogs {
50-
if err := services.CatalogSvc.UpdateStackCatalog(catalog); err != nil {
47+
if err := services.CatalogSvc.UpdateStackCatalog(ctx, catalog); err != nil {
5148
return fmt.Errorf("error updating stack catalog: %w", err)
5249
}
5350
}
5451

55-
if err := services.ImageBuildService.BuildImage(c.Image); err != nil {
52+
if err := services.ImageBuildService.BuildImage(ctx, c.Image); err != nil {
5653
return fmt.Errorf("error building out image: %w", err)
5754
}
5855

internal/service/image_build_service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/aslamcodes/appstreamfile/internal/config"
@@ -11,7 +12,7 @@ type ImageBuildSvc struct {
1112
Exec execx.Commander
1213
}
1314

14-
func (i *ImageBuildSvc) BuildImage(image config.Image) error {
15+
func (i *ImageBuildSvc) BuildImage(ctx context.Context, image config.Image) error {
1516
fmt.Println("\nBuilding out image")
1617

1718
_, err := i.Exec.LookPath(IMAGE_ASSISTANT)
@@ -22,7 +23,7 @@ func (i *ImageBuildSvc) BuildImage(image config.Image) error {
2223

2324
args := append([]string{"create-image"}, image.Args()...)
2425

25-
cmd := i.Exec.Command(IMAGE_ASSISTANT, args...)
26+
cmd := i.Exec.CommandContext(ctx, IMAGE_ASSISTANT, args...)
2627

2728
fmt.Println(cmd.String())
2829

internal/service/image_build_service_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service_test
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
67
"testing"
@@ -10,6 +11,8 @@ import (
1011
)
1112

1213
func TestImageBuild(t *testing.T) {
14+
ctx := context.TODO()
15+
1316
fc := FakeCommander{
1417
LastCommand: "",
1518
LastArgs: []string{},
@@ -30,7 +33,7 @@ func TestImageBuild(t *testing.T) {
3033
DryRun: true,
3134
}
3235

33-
i.BuildImage(image)
36+
i.BuildImage(ctx, image)
3437

3538
expectedCommand := strings.TrimSpace(`image-assistant create-image --name test --display-name test2 --description test3 --use-latest-agent-version --enable-dynamic-app-catalog --dry-run --tags k1 v1 k2 built with appstreamfile`)
3639
actual := strings.TrimSpace(fmt.Sprintf("%s %s", fc.LastCommand, strings.Join(fc.LastArgs, " ")))

internal/service/installer_service.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67

@@ -13,7 +14,7 @@ type InstallerSvc struct {
1314
KeepTmpForTest bool
1415
}
1516

16-
func (s *InstallerSvc) InstallScript(inst *config.Installer) error {
17+
func (s *InstallerSvc) InstallScript(ctx context.Context, inst *config.Installer) error {
1718
var (
1819
exe string
1920
ext string
@@ -52,11 +53,11 @@ func (s *InstallerSvc) InstallScript(inst *config.Installer) error {
5253
return fmt.Errorf("unable to close the file: %w", err)
5354
}
5455

55-
return s.RunScript(exe, args, f.Name())
56+
return s.RunScript(ctx, exe, args, f.Name())
5657
}
5758

58-
func (s *InstallerSvc) RunScript(exe string, args []string, filePath string) error {
59-
cmd := s.Exec.Command(exe, append(args, filePath)...)
59+
func (s *InstallerSvc) RunScript(ctx context.Context, exe string, args []string, filePath string) error {
60+
cmd := s.Exec.CommandContext(ctx, exe, append(args, filePath)...)
6061

6162
out, err := cmd.CombinedOutput()
6263

internal/service/installer_service_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service_test
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

@@ -9,6 +10,7 @@ import (
910
)
1011

1112
func TestInstallCreatesScriptFile(t *testing.T) {
13+
ctx := context.TODO()
1214
fake := &FakeCommander{}
1315
svc := &service.InstallerSvc{Exec: fake, KeepTmpForTest: true}
1416

@@ -17,7 +19,7 @@ func TestInstallCreatesScriptFile(t *testing.T) {
1719
InstallScript: "Write-Host hi",
1820
}
1921

20-
_ = svc.InstallScript(inst)
22+
_ = svc.InstallScript(ctx, inst)
2123

2224
scriptPath := fake.LastArgs[len(fake.LastArgs)-1]
2325

@@ -30,10 +32,12 @@ func TestInstallCreatesScriptFile(t *testing.T) {
3032
}
3133

3234
func TestRunScript(t *testing.T) {
35+
ctx := context.TODO()
36+
3337
fake := &FakeCommander{}
3438
svc := &service.InstallerSvc{Exec: fake}
3539

36-
err := svc.RunScript("powershell.exe",
40+
err := svc.RunScript(ctx, "powershell.exe",
3741
[]string{"-NoProfile", "-File"},
3842
"/tmp/test.ps1",
3943
)

0 commit comments

Comments
 (0)