Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions cmd/appstreamfile/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"os/signal"

"github.com/aslamcodes/appstreamfile/internal/backend"
"github.com/aslamcodes/appstreamfile/internal/logger"
Expand All @@ -12,13 +14,17 @@ import (
)

type RunOptions struct {
location string
bucket string
key string
versionId string
location string
SourceType string
bucket string
key string
versionId string
}

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

source := flag.String("source", "", "Configuration source: s3 or local")
location := flag.String("location", "", "Local filesystem path to the config file")
bucket := flag.String("bucket", "", "S3 bucket containing the config file")
Expand All @@ -30,24 +36,25 @@ func main() {
logger.Init()

runOptions := &RunOptions{
location: *location,
bucket: *bucket,
key: *key,
versionId: *versionId,
SourceType: *source,
location: *location,
bucket: *bucket,
key: *key,
versionId: *versionId,
}

if err := run(*source, runOptions); err != nil {
if err := run(ctx, runOptions); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func run(sourceType string, opts *RunOptions) error {
func run(ctx context.Context, opts *RunOptions) error {
var backendSource backend.BackendSource
var err error

switch sourceType {
case "local":
switch opts.SourceType {
case "local":
if opts.location == "" {
return fmt.Errorf("location of config file must be provided")
}
Expand All @@ -58,7 +65,6 @@ func run(sourceType string, opts *RunOptions) error {
}
backendSource, err = backend.NewS3Backend(opts.bucket, opts.key, opts.versionId, "appstream_machine_role")


default:
return fmt.Errorf("invalid source provided")
}
Expand All @@ -67,13 +73,13 @@ func run(sourceType string, opts *RunOptions) error {
return fmt.Errorf("unable to create backend source: %w", err)
}

config, err := backendSource.GetConfig()
config, err := backendSource.GetConfig(ctx)

if err != nil {
return fmt.Errorf("failed to fetch config from backend: %w", err)
}

if err := validator.ValidateConfig(config); err != nil {
if err := validator.ValidateConfig(ctx, config); err != nil {
return fmt.Errorf("config file validation failed: %w", err)
}

Expand Down
8 changes: 6 additions & 2 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package backend

import c "github.com/aslamcodes/appstreamfile/internal/config"
import (
"context"

c "github.com/aslamcodes/appstreamfile/internal/config"
)

type BackendSource interface {
GetConfig() (*c.Config, error)
GetConfig(ctx context.Context) (*c.Config, error)
}
7 changes: 6 additions & 1 deletion internal/backend/local.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend

import (
"context"
"fmt"
"os"

Expand All @@ -12,7 +13,11 @@ type LocalBackend struct {
Location string
}

func (lb *LocalBackend) GetConfig() (*config.Config, error) {
func (lb *LocalBackend) GetConfig(ctx context.Context) (*config.Config, error) {
if err := ctx.Err(); err != nil {
return nil, err
}

fmt.Printf("Attempting to fetch config from local backend at %s\n", lb.Location)

data, err := os.ReadFile(lb.Location)
Expand Down
3 changes: 2 additions & 1 deletion internal/backend/local_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend_test

import (
"context"
"fmt"
"os"
"reflect"
Expand All @@ -15,7 +16,7 @@ func TestGetConfig(t *testing.T) {
Location: "../../testdata/config_win.yaml",
}

actual, err := localBackend.GetConfig()
actual, err := localBackend.GetConfig(context.TODO())

if err != nil {
t.Fatal(err)
Expand Down
6 changes: 4 additions & 2 deletions internal/backend/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ type S3Backend struct {
Client S3Client
}

func (s3Backend *S3Backend) GetConfig() (*config.Config, error) {
ctx := context.Background()
func (s3Backend *S3Backend) GetConfig(ctx context.Context) (*config.Config, error) {
if err := ctx.Err(); err != nil {
return nil, err
}

if s3Backend.Client == nil {
return nil, fmt.Errorf("client is nil")
Expand Down
4 changes: 2 additions & 2 deletions internal/backend/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ installers:
Client: client,
}

actual, err := backend.GetConfig()
actual, err := backend.GetConfig(context.TODO())

if err != nil {
t.Errorf("error fetching the config: %v", err)
Expand All @@ -76,7 +76,7 @@ func TestGetConfigFail(t *testing.T) {
},
}

_, err := backend.GetConfig()
_, err := backend.GetConfig(context.TODO())

if err == nil {
t.Errorf("expected %v, got nil", expectedErr)
Expand Down
3 changes: 2 additions & 1 deletion internal/validator/catalog_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"context"
"errors"

"github.com/aslamcodes/appstreamfile/internal/config"
Expand All @@ -12,7 +13,7 @@ var (
ErrEmptyCatalogNamePath = errors.New("catalog application path and name cannot be empty")
)

func ValidateCatalogApplications(configData *config.Config) error {
func ValidateCatalogApplications(ctx context.Context, configData *config.Config) error {
for _, c := range configData.Catalogs {
if c.Name == "" && c.Path == "" {
return ErrEmptyCatalogNamePath
Expand Down
5 changes: 3 additions & 2 deletions internal/validator/catalog_validator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator_test

import (
"context"
"testing"

"github.com/aslamcodes/appstreamfile/internal/backend"
Expand Down Expand Up @@ -40,13 +41,13 @@ func TestValidateCatalogConfig(t *testing.T) {
Location: tC.filename,
}

config, err := lb.GetConfig()
config, err := lb.GetConfig(context.TODO())

if err != nil {
t.Errorf("unable to load config: %s", err.Error())
}

actual := validator.ValidateCatalogApplications(config)
actual := validator.ValidateCatalogApplications(context.TODO(), config)

if actual != tC.expected {
t.Errorf("expected %v, actual %v", tC.expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions internal/validator/config_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"context"
"errors"
"slices"

Expand All @@ -12,8 +13,7 @@ var (
ErrInvalidPlatform = errors.New("Platform not supported")
)


func ValidatePlatforms(c *config.Config) error {
func ValidatePlatforms(ctx context.Context, c *config.Config) error {
if c.Platform == "" {
return ErrPlatformMissing
}
Expand Down
7 changes: 5 additions & 2 deletions internal/validator/config_validator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator_test

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -46,6 +47,8 @@ installers:
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
ctx := context.TODO()

file, err := os.CreateTemp("../../testdata", fmt.Sprintf("test_%s_", tC.desc))

if err != nil {
Expand All @@ -58,13 +61,13 @@ installers:
Location: file.Name(),
}

configData, err := lb.GetConfig()
configData, err := lb.GetConfig(ctx)

if err != nil {
t.Errorf("unable to fetch config data: %v", err)
}

err = validator.ValidatePlatforms(configData)
err = validator.ValidatePlatforms(ctx, configData)

if !errors.Is(err, tC.expected) {
t.Errorf("expected %v, got %v", tC.expected, err)
Expand Down
3 changes: 2 additions & 1 deletion internal/validator/file_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"context"
"errors"

"github.com/aslamcodes/appstreamfile/internal/config"
Expand All @@ -10,7 +11,7 @@ var (
ErrFileDeployPathMissing = errors.New("file deploy path should not be null")
)

func ValidateFileDeploys(c *config.Config) error {
func ValidateFileDeploys(ctx context.Context, c *config.Config) error {
for _, file := range c.Files {
if file.Path == "" {
return ErrFileDeployPathMissing
Expand Down
7 changes: 5 additions & 2 deletions internal/validator/file_validator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator_test

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -52,13 +53,15 @@ files:
Location: file.Name(),
}

configData, err := lb.GetConfig()
ctx := context.TODO()

configData, err := lb.GetConfig(ctx)

if err != nil {
t.Errorf("unable to fetch config data: %v", err)
}

err = validator.ValidateFileDeploys(configData)
err = validator.ValidateFileDeploys(ctx, configData)

if !errors.Is(err, tC.expected) {
t.Errorf("expected %v, got %v", tC.expected, err)
Expand Down
3 changes: 2 additions & 1 deletion internal/validator/image_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"context"
"errors"
"strings"

Expand All @@ -12,7 +13,7 @@ var (
ErrInvalidTagsCreateImage = errors.New("format invalid for create-image tags (key1:value1)")
)

func ValidateImage(c *config.Config) error {
func ValidateImage(ctx context.Context, c *config.Config) error {
if c.Image.Name == "" {
return ErrInvalidParametersCreateImage
}
Expand Down
7 changes: 5 additions & 2 deletions internal/validator/image_validator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator_test

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -64,6 +65,8 @@ image:
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
ctx := context.TODO()

file, err := os.CreateTemp("../../testdata", fmt.Sprintf("test_%s_", tC.desc))

if err != nil {
Expand All @@ -76,13 +79,13 @@ image:
Location: file.Name(),
}

configData, err := lb.GetConfig()
configData, err := lb.GetConfig(ctx)

if err != nil {
t.Errorf("unable to fetch config data: %v", err)
}

err = validator.ValidateImage(configData)
err = validator.ValidateImage(ctx, configData)

if !errors.Is(err, tC.expected) {
t.Errorf("expected %v, got %v", tC.expected, err)
Expand Down
3 changes: 2 additions & 1 deletion internal/validator/installer_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"context"
"errors"
"fmt"
"slices"
Expand All @@ -12,7 +13,7 @@ var (
ErrInvalidExecutableForPlatform = errors.New("Invalid executable for given platform")
)

func InstallerValidator(configData *config.Config) error {
func InstallerValidator(ctx context.Context, configData *config.Config) error {
platform := configData.Platform

platformExecs, exists := ExecPlatformMap[platform]
Expand Down
Loading