Skip to content

Commit 6040dd4

Browse files
committed
fix: add context to installer service, s3 client and file deploy service
1 parent 10f4820 commit 6040dd4

File tree

8 files changed

+21
-9
lines changed

8 files changed

+21
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
bin
22
# Added by goreleaser init:
33
dist/
4+
.DS_Store

cmd/appstreamfile/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func run(ctx context.Context, opts *RunOptions) error {
6363
if opts.bucket == "" || opts.key == "" {
6464
return fmt.Errorf("missing required S3 options: bucket and key")
6565
}
66-
backendSource, err = backend.NewS3Backend(opts.bucket, opts.key, opts.versionId, "appstream_machine_role")
66+
backendSource, err = backend.NewS3Backend(ctx, opts.bucket, opts.key, opts.versionId, "appstream_machine_role")
6767

6868
default:
6969
return fmt.Errorf("invalid source provided")

internal/backend/s3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func (s3Backend *S3Backend) GetConfig(ctx context.Context) (*config.Config, erro
5050
return &configData, nil
5151
}
5252

53-
func NewS3Backend(bucket, key, versionId, profile string) (BackendSource, error) {
54-
client, err := NewS3Client(profile)
53+
func NewS3Backend(ctx context.Context, bucket, key, versionId, profile string) (BackendSource, error) {
54+
client, err := NewS3Client(ctx, profile)
5555

5656
if err != nil {
5757
return nil, fmt.Errorf("not able to create s3 client: %w", err)

internal/backend/s3_client.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ func (c *S3BackendClient) GetObject(ctx context.Context, bucket string, key stri
2929
return c.s3Client.GetObject(ctx, objectInput)
3030
}
3131

32-
func NewS3Client(profile string) (S3Client, error) {
32+
func NewS3Client(ctx context.Context, profile string) (S3Client, error) {
3333
opts := []func(*s3Config.LoadOptions) error{}
3434

3535
if profile != "" {
3636
opts = append(opts, s3Config.WithSharedConfigProfile(profile))
3737
}
3838

39-
ctx := context.Background()
40-
4139
cfg, err := s3Config.LoadDefaultConfig(ctx, opts...)
4240
if err != nil {
4341
return nil, fmt.Errorf("failed to load SDK configuration: %w", err)

internal/service/config_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ImplementConfig(ctx context.Context, c *config.Config) error {
3232

3333
for _, f := range c.Files {
3434
fmt.Println("Deploying file", f.Path)
35-
err := services.FileDeploySvc.DeployFile(&f)
35+
err := services.FileDeploySvc.DeployFile(ctx, &f)
3636

3737
if err != nil {
3838
return fmt.Errorf("error deploying file %s: %w", f.Path, err)

internal/service/file_deploy_service.go

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

33
import (
44
"bufio"
5+
"context"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -12,7 +13,12 @@ import (
1213
type FileDeploySvc struct {
1314
}
1415

15-
func (s *FileDeploySvc) DeployFile(f *config.File) error {
16+
func (s *FileDeploySvc) DeployFile(ctx context.Context, f *config.File) error {
17+
18+
if err := ctx.Err(); err != nil {
19+
return err
20+
}
21+
1622
if err := os.MkdirAll(filepath.Dir(f.Path), 0770); err != nil {
1723
return fmt.Errorf("error creating required directories for %s: %w", f.Path, err)
1824
}

internal/service/file_deploy_service_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package service
22

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

78
"github.com/aslamcodes/appstreamfile/internal/config"
89
)
910

1011
func TestFileDeploy(t *testing.T) {
12+
ctx := context.TODO()
13+
1114
file, err := os.CreateTemp("../../testdata", "test.txt")
1215
defer func() {
1316
os.Remove(file.Name())
@@ -24,7 +27,7 @@ func TestFileDeploy(t *testing.T) {
2427

2528
s := FileDeploySvc{}
2629

27-
s.DeployFile(f)
30+
s.DeployFile(ctx, f)
2831

2932
content, err := os.ReadFile(f.Path)
3033

internal/service/installer_service.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ type InstallerSvc struct {
1515
}
1616

1717
func (s *InstallerSvc) InstallScript(ctx context.Context, inst *config.Installer) error {
18+
if err := ctx.Err(); err != nil {
19+
return err
20+
}
21+
1822
var (
1923
exe string
2024
ext string

0 commit comments

Comments
 (0)