mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-01-13 13:21:01 +00:00
feat: add --location flag to override default location (#142)
Allow users to explicitly specify a location when uploading images. This is useful when the default location is unavailable.
This commit is contained in:
parent
a9b16cf07c
commit
3bbcf0439a
2 changed files with 20 additions and 2 deletions
|
|
@ -21,6 +21,7 @@ const (
|
||||||
uploadFlagFormat = "format"
|
uploadFlagFormat = "format"
|
||||||
uploadFlagArchitecture = "architecture"
|
uploadFlagArchitecture = "architecture"
|
||||||
uploadFlagServerType = "server-type"
|
uploadFlagServerType = "server-type"
|
||||||
|
uploadFlagLocation = "location"
|
||||||
uploadFlagDescription = "description"
|
uploadFlagDescription = "description"
|
||||||
uploadFlagLabels = "labels"
|
uploadFlagLabels = "labels"
|
||||||
)
|
)
|
||||||
|
|
@ -52,6 +53,7 @@ var uploadCmd = &cobra.Command{
|
||||||
imageFormat, _ := cmd.Flags().GetString(uploadFlagFormat)
|
imageFormat, _ := cmd.Flags().GetString(uploadFlagFormat)
|
||||||
architecture, _ := cmd.Flags().GetString(uploadFlagArchitecture)
|
architecture, _ := cmd.Flags().GetString(uploadFlagArchitecture)
|
||||||
serverType, _ := cmd.Flags().GetString(uploadFlagServerType)
|
serverType, _ := cmd.Flags().GetString(uploadFlagServerType)
|
||||||
|
location, _ := cmd.Flags().GetString(uploadFlagLocation)
|
||||||
description, _ := cmd.Flags().GetString(uploadFlagDescription)
|
description, _ := cmd.Flags().GetString(uploadFlagDescription)
|
||||||
labels, _ := cmd.Flags().GetStringToString(uploadFlagLabels)
|
labels, _ := cmd.Flags().GetStringToString(uploadFlagLabels)
|
||||||
|
|
||||||
|
|
@ -102,6 +104,10 @@ var uploadCmd = &cobra.Command{
|
||||||
options.ServerType = &hcloud.ServerType{Name: serverType}
|
options.ServerType = &hcloud.ServerType{Name: serverType}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if location != "" {
|
||||||
|
options.Location = &hcloud.Location{Name: location}
|
||||||
|
}
|
||||||
|
|
||||||
image, err := client.Upload(ctx, options)
|
image, err := client.Upload(ctx, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to upload the image: %w", err)
|
return fmt.Errorf("failed to upload the image: %w", err)
|
||||||
|
|
@ -140,6 +146,7 @@ func init() {
|
||||||
)
|
)
|
||||||
|
|
||||||
uploadCmd.Flags().String(uploadFlagServerType, "", "Explicitly use this server type to generate the image. Mutually exclusive with --architecture.")
|
uploadCmd.Flags().String(uploadFlagServerType, "", "Explicitly use this server type to generate the image. Mutually exclusive with --architecture.")
|
||||||
|
uploadCmd.Flags().String(uploadFlagLocation, "", "Explicitly use this location to generate the image.")
|
||||||
|
|
||||||
// Only one of them needs to be set
|
// Only one of them needs to be set
|
||||||
uploadCmd.MarkFlagsOneRequired(uploadFlagArchitecture, uploadFlagServerType)
|
uploadCmd.MarkFlagsOneRequired(uploadFlagArchitecture, uploadFlagServerType)
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,10 @@ type UploadOptions struct {
|
||||||
// - The default server type is no longer available, or not temporarily out of stock.
|
// - The default server type is no longer available, or not temporarily out of stock.
|
||||||
ServerType *hcloud.ServerType
|
ServerType *hcloud.ServerType
|
||||||
|
|
||||||
|
// Location can be optionally set to override the default location.
|
||||||
|
// This is useful is the default location is unavailable.
|
||||||
|
Location *hcloud.Location
|
||||||
|
|
||||||
// Description is an optional description that the resulting image (snapshot) will have. There is no way to
|
// Description is an optional description that the resulting image (snapshot) will have. There is no way to
|
||||||
// select images by its description, you should use Labels if you need to identify your image later.
|
// select images by its description, you should use Labels if you need to identify your image later.
|
||||||
Description *string
|
Description *string
|
||||||
|
|
@ -214,9 +218,16 @@ func (s *Client) Upload(ctx context.Context, options UploadOptions) (*hcloud.Ima
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var location *hcloud.Location
|
||||||
|
if options.Location != nil {
|
||||||
|
location = options.Location
|
||||||
|
} else {
|
||||||
|
location = defaultLocation
|
||||||
|
}
|
||||||
|
|
||||||
logger.DebugContext(ctx, "creating server with config",
|
logger.DebugContext(ctx, "creating server with config",
|
||||||
"image", defaultImage.Name,
|
"image", defaultImage.Name,
|
||||||
"location", defaultLocation.Name,
|
"location", location.Name,
|
||||||
"serverType", serverType.Name,
|
"serverType", serverType.Name,
|
||||||
)
|
)
|
||||||
serverCreateResult, _, err := s.c.Server.Create(ctx, hcloud.ServerCreateOpts{
|
serverCreateResult, _, err := s.c.Server.Create(ctx, hcloud.ServerCreateOpts{
|
||||||
|
|
@ -230,7 +241,7 @@ func (s *Client) Upload(ctx context.Context, options UploadOptions) (*hcloud.Ima
|
||||||
StartAfterCreate: hcloud.Ptr(false),
|
StartAfterCreate: hcloud.Ptr(false),
|
||||||
// Image will never be booted, we only boot into rescue system
|
// Image will never be booted, we only boot into rescue system
|
||||||
Image: defaultImage,
|
Image: defaultImage,
|
||||||
Location: defaultLocation,
|
Location: location,
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue