feat: set server type explicitly (#36)

- **CLI**: New flag `--server-type` that overrides the `--architecture`
flag and allows users to specify the server type they want
- **Lib**: New field in `UploadOptions`: `ServerType *hcloud.ServerType`
that overrides the `Architecture` field and allows users to specify the
server type they want

Closes #30
This commit is contained in:
Julian Tölle 2024-06-23 16:21:14 +02:00 committed by GitHub
parent f244acdfaf
commit 42eeb00a07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 5 deletions

View file

@ -17,6 +17,7 @@ const (
uploadFlagImagePath = "image-path"
uploadFlagCompression = "compression"
uploadFlagArchitecture = "architecture"
uploadFlagServerType = "server-type"
uploadFlagDescription = "description"
uploadFlagLabels = "labels"
)
@ -43,12 +44,12 @@ This does cost a bit of money for the server.`,
imagePathString, _ := cmd.Flags().GetString(uploadFlagImagePath)
imageCompression, _ := cmd.Flags().GetString(uploadFlagCompression)
architecture, _ := cmd.Flags().GetString(uploadFlagArchitecture)
serverType, _ := cmd.Flags().GetString(uploadFlagServerType)
description, _ := cmd.Flags().GetString(uploadFlagDescription)
labels, _ := cmd.Flags().GetStringToString(uploadFlagLabels)
options := hcloudimages.UploadOptions{
ImageCompression: hcloudimages.Compression(imageCompression),
Architecture: hcloud.Architecture(architecture),
Description: hcloud.Ptr(description),
Labels: labels,
}
@ -69,6 +70,12 @@ This does cost a bit of money for the server.`,
options.ImageReader = imageFile
}
if architecture != "" {
options.Architecture = hcloud.Architecture(architecture)
} else if serverType != "" {
options.ServerType = &hcloud.ServerType{Name: serverType}
}
image, err := client.Upload(ctx, options)
if err != nil {
return fmt.Errorf("failed to upload the image: %w", err)
@ -99,7 +106,12 @@ func init() {
uploadFlagArchitecture,
cobra.FixedCompletions([]string{string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)}, cobra.ShellCompDirectiveNoFileComp),
)
_ = uploadCmd.MarkFlagRequired(uploadFlagArchitecture)
uploadCmd.Flags().String(uploadFlagServerType, "", "Explicitly use this server type to generate the image. Mutually exclusive with --architecture.")
// Only one of them needs to be set
uploadCmd.MarkFlagsOneRequired(uploadFlagArchitecture, uploadFlagServerType)
uploadCmd.MarkFlagsMutuallyExclusive(uploadFlagArchitecture, uploadFlagServerType)
uploadCmd.Flags().String(uploadFlagDescription, "", "Description for the resulting image")