main: simplify upload error handling
This commit is contained in:
parent
8d03b2215c
commit
6dd8515801
4 changed files with 20 additions and 27 deletions
|
|
@ -232,14 +232,13 @@ 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) {
|
||||
return err
|
||||
if errors.Is(err, ErrUploadTypeUnsupported) || errors.Is(err, ErrUploadConfigNotProvided) {
|
||||
err = nil
|
||||
}
|
||||
if missingUploadConfig != nil && !missingUploadConfig.allMissing {
|
||||
return fmt.Errorf("partial upload config provided: %w", err)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if uploader != nil {
|
||||
|
|
|
|||
|
|
@ -343,7 +343,7 @@ func TestBuildIntegrationHappy(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
|
||||
// ensure osbuild was run exactly one
|
||||
assert.Equal(t, 1, len(fakeOsbuildCmd.Calls()))
|
||||
require.Equal(t, 1, len(fakeOsbuildCmd.Calls()))
|
||||
osbuildCall := fakeOsbuildCmd.Calls()[0]
|
||||
// --cache is passed correctly to osbuild
|
||||
storePos := slices.Index(osbuildCall, "--store")
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
|
@ -14,22 +15,14 @@ import (
|
|||
"github.com/osbuild/images/pkg/cloud/awscloud"
|
||||
)
|
||||
|
||||
type MissingUploadConfigError struct {
|
||||
missing []string
|
||||
allMissing bool
|
||||
}
|
||||
// ErrMissingUploadConfig is returned when the upload configuration is missing
|
||||
var ErrMissingUploadConfig = errors.New("missing upload configuration")
|
||||
|
||||
func (e *MissingUploadConfigError) Error() string {
|
||||
return fmt.Sprintf("missing upload configuration: %q", e.missing)
|
||||
}
|
||||
// ErrUploadConfigNotProvided is returned when all the upload configuration is missing
|
||||
var ErrUploadConfigNotProvided = errors.New("missing all 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 type")
|
||||
|
||||
var awscloudNewUploader = awscloud.NewUploader
|
||||
|
||||
|
|
@ -73,7 +66,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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -104,10 +97,11 @@ func uploaderForCmdAWS(cmd *cobra.Command) (cloud.Uploader, error) {
|
|||
}
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return nil, &MissingUploadConfigError{
|
||||
missing: missing,
|
||||
allMissing: len(missing) == len(requiredArgs),
|
||||
if len(missing) == len(requiredArgs) {
|
||||
return nil, fmt.Errorf("%w: %q", ErrUploadConfigNotProvided, missing)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: %q", ErrMissingUploadConfig, missing)
|
||||
}
|
||||
|
||||
return awscloudNewUploader(region, bucketName, amiName, nil)
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ func TestUploadCmdlineErrors(t *testing.T) {
|
|||
`missing --to parameter, try --to=aws`,
|
||||
}, {
|
||||
[]string{"--to=aws"},
|
||||
`missing upload configuration: ["--aws-ami-name" "--aws-bucket" "--aws-region"]`,
|
||||
`missing all upload configuration: ["--aws-ami-name" "--aws-bucket" "--aws-region"]`,
|
||||
},
|
||||
{
|
||||
[]string{"--to=aws", "--aws-ami-name=1"},
|
||||
|
|
@ -242,5 +242,5 @@ func TestBuildAndUploadWithAWSPartialCmdlineErrors(t *testing.T) {
|
|||
defer restore()
|
||||
|
||||
err := main.Run()
|
||||
assert.EqualError(t, err, `partial upload config provided: missing upload configuration: ["--aws-ami-name" "--aws-bucket"]`)
|
||||
assert.EqualError(t, err, `missing upload configuration: ["--aws-ami-name" "--aws-bucket"]`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue