Skip to content

Commit

Permalink
main: simplify upload error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Feb 12, 2025
1 parent ea6b053 commit 009f2dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 27 deletions.
8 changes: 1 addition & 7 deletions cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -213,15 +212,10 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
return err
}

var uploadUnsupported *UploadTypeUnsupportedError
var missingUploadConfig *MissingUploadConfigError
uploader, err := uploaderFor(cmd, res.ImgType.Name())
if err != nil && !errors.As(err, &missingUploadConfig) && !errors.As(err, &uploadUnsupported) {
if err != nil {
return err
}
if missingUploadConfig != nil && !missingUploadConfig.allMissing {
return fmt.Errorf("partial upload config provided: %w", err)
}

if uploader != nil {
pbar.SetPulseMsgf("Checking cloud access")
Expand Down
27 changes: 7 additions & 20 deletions cmd/image-builder/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand All @@ -14,22 +15,11 @@ import (
"github.com/osbuild/images/pkg/cloud/awscloud"
)

type MissingUploadConfigError struct {
missing []string
allMissing bool
}

func (e *MissingUploadConfigError) Error() string {
return fmt.Sprintf("missing upload configuration: %q", e.missing)
}
// ErrMissingUploadConfig is returned when the upload configuration is missing
var ErrMissingUploadConfig = errors.New("missing upload configuration")

type UploadTypeUnsupportedError struct {
typ string
}

func (e *UploadTypeUnsupportedError) Error() string {
return fmt.Sprintf("unsupported upload type %q", e.typ)
}
// ErrUploadTypeUnsupported is returned when the upload type is not supported
var ErrUploadTypeUnsupported = errors.New("unsupported upload type")

var awscloudNewUploader = awscloud.NewUploader

Expand Down Expand Up @@ -73,7 +63,7 @@ func uploaderFor(cmd *cobra.Command, typeOrCloud string) (cloud.Uploader, error)
case "ami", "aws":
return uploaderForCmdAWS(cmd)
default:
return nil, &UploadTypeUnsupportedError{typeOrCloud}
return nil, fmt.Errorf("%w: %q", ErrUploadTypeUnsupported, typeOrCloud)
}

}
Expand Down Expand Up @@ -104,10 +94,7 @@ func uploaderForCmdAWS(cmd *cobra.Command) (cloud.Uploader, error) {
}
}
if len(missing) > 0 {
return nil, &MissingUploadConfigError{
missing: missing,
allMissing: len(missing) == len(requiredArgs),
}
return nil, fmt.Errorf("%w: %q", ErrMissingUploadConfig, missing)
}

return awscloudNewUploader(region, bucketName, amiName, nil)
Expand Down

0 comments on commit 009f2dd

Please sign in to comment.