feat: documentation and cleanup command

This commit is contained in:
Julian Tölle 2024-05-04 22:13:33 +02:00
parent 27d4e3240e
commit c9ab40b539
16 changed files with 394 additions and 148 deletions

View file

@ -1,40 +1,43 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/apricote/hcloud-upload-image/hcloudimages/contextlogger"
)
// cleanupCmd represents the cleanup command
var cleanupCmd = &cobra.Command{
Use: "cleanup",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Short: "Remove any temporary resources that were left over",
Long: `If the upload fails at any point, there might still exist a server or
ssh key in your Hetzner Cloud project. This command cleans up any resources
that match the label "apricote.de/created-by=hcloud-upload-image".
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("cleanup called")
If you want to see a preview of what would be removed, you can use the official hcloud CLI and run:
$ hcloud server list -l apricote.de/created-by=hcloud-upload-image
$ hcloud ssh-key list -l apricote.de/created-by=hcloud-upload-image
This command does not handle any parallel executions of hcloud-upload-image
and will remove in-use resources if called at the same time.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
logger := contextlogger.From(ctx)
err := client.CleanupTempResources(ctx)
if err != nil {
return fmt.Errorf("failed to clean up temporary resources: %w", err)
}
logger.InfoContext(ctx, "Successfully cleaned up all temporary resources!")
return nil
},
}
func init() {
rootCmd.AddCommand(cleanupCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cleanupCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// cleanupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -1,40 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("list called")
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -2,7 +2,6 @@ package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"time"
@ -13,10 +12,21 @@ import (
"github.com/apricote/hcloud-upload-image/hcloudimages"
"github.com/apricote/hcloud-upload-image/hcloudimages/backoff"
"github.com/apricote/hcloud-upload-image/hcloudimages/contextlogger"
"github.com/apricote/hcloud-upload-image/internal/ui"
)
const (
flagVerbose = "verbose"
)
var (
// 1 activates slog debug output
// 2 activates hcloud-go debug output
verbose int
)
// The pre-authenticated client. Set in the root command PersistentPreRun
var client hcloudimages.Client
var client *hcloudimages.Client
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
@ -27,6 +37,14 @@ var rootCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
logLevel := slog.LevelInfo
if verbose >= 1 {
logLevel = slog.LevelDebug
}
slog.SetDefault(slog.New(ui.NewHandler(os.Stdout, &ui.HandlerOptions{
Level: logLevel,
})))
// Add logger to command context
logger := slog.Default()
ctx = contextlogger.New(ctx, logger)
@ -36,7 +54,7 @@ var rootCmd = &cobra.Command{
},
}
func newClient(ctx context.Context) hcloudimages.Client {
func newClient(ctx context.Context) *hcloudimages.Client {
logger := contextlogger.From(ctx)
// Build hcloud-go client
if os.Getenv("HCLOUD_TOKEN") == "" {
@ -50,7 +68,7 @@ func newClient(ctx context.Context) hcloudimages.Client {
hcloud.WithPollBackoffFunc(backoff.ExponentialBackoffWithLimit(2, 1*time.Second, 30*time.Second)),
}
if os.Getenv("HCLOUD_DEBUG") != "" {
if os.Getenv("HCLOUD_DEBUG") != "" || verbose >= 2 {
opts = append(opts, hcloud.WithDebugWriter(os.Stderr))
}
@ -66,7 +84,6 @@ func Execute() {
func init() {
rootCmd.SetErrPrefix("\033[1;31mError:")
rootCmd.SetFlagErrorFunc(func(command *cobra.Command, err error) error {
return fmt.Errorf("fooo")
})
rootCmd.PersistentFlags().CountVarP(&verbose, flagVerbose, "v", "verbose debug output, can be specified up to 2 times")
}

View file

@ -7,7 +7,7 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/spf13/cobra"
hcloud_upload_image "github.com/apricote/hcloud-upload-image/hcloudimages"
"github.com/apricote/hcloud-upload-image/hcloudimages"
"github.com/apricote/hcloud-upload-image/hcloudimages/contextlogger"
)
@ -41,9 +41,9 @@ This does cost a bit of money for the server.`,
return fmt.Errorf("unable to parse url from --%s=%q: %w", uploadFlagImageURL, imageURLString, err)
}
image, err := client.Upload(ctx, hcloud_upload_image.UploadOptions{
image, err := client.Upload(ctx, hcloudimages.UploadOptions{
ImageURL: imageURL,
ImageCompression: hcloud_upload_image.Compression(imageCompression),
ImageCompression: hcloudimages.Compression(imageCompression),
Architecture: hcloud.Architecture(architecture),
Description: hcloud.Ptr(description),
Labels: labels,
@ -67,7 +67,7 @@ func init() {
uploadCmd.Flags().String(uploadFlagCompression, "", "Type of compression that was used on the disk image")
_ = uploadCmd.RegisterFlagCompletionFunc(
uploadFlagCompression,
cobra.FixedCompletions([]string{string(hcloud_upload_image.CompressionBZ2)}, cobra.ShellCompDirectiveNoFileComp),
cobra.FixedCompletions([]string{string(hcloudimages.CompressionBZ2)}, cobra.ShellCompDirectiveNoFileComp),
)
uploadCmd.Flags().String(uploadFlagArchitecture, "", "CPU Architecture of the disk image. Choices: x86|arm")