mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
* Allow for argument completion This shows for a single command on how to complete service names in the cluster: `uc inspect <TAB>` will complete using ListServices See: #307 Signed-off-by: Miek Gieben <miek@miek.nl> * All cluster interactions can be completed Signed-off-by: Miek Gieben <miek@miek.nl> * Machines -m flag completion Signed-off-by: Miek Gieben <miek@miek.nl> * Add completion for "ctx use" Signed-off-by: Miek Gieben <miek@miek.nl> * Complete from compose file too Signed-off-by: Miek Gieben <miek@miek.nl> * Allow for argument completion This shows for a single command on how to complete service names in the cluster: `uc inspect <TAB>` will complete using ListServices See: #307 Signed-off-by: Miek Gieben <miek@miek.nl> * All cluster interactions can be completed Signed-off-by: Miek Gieben <miek@miek.nl> * Machines -m flag completion Signed-off-by: Miek Gieben <miek@miek.nl> * Add completion for "ctx use" Signed-off-by: Miek Gieben <miek@miek.nl> * Complete from compose file too Signed-off-by: Miek Gieben <miek@miek.nl> * Move to better place Signed-off-by: Miek Gieben <miek@miek.nl> * Adjust imports Signed-off-by: Miek Gieben <miek@miek.nl> * prefix match for context too Signed-off-by: Miek Gieben <miek@miek.nl> --------- Signed-off-by: Miek Gieben <miek@miek.nl>
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package caddy
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/alecthomas/chroma/v2/quick"
|
|
"github.com/psviderski/uncloud/internal/cli"
|
|
"github.com/psviderski/uncloud/internal/cli/completion"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type configOptions struct {
|
|
machine string
|
|
noColor bool
|
|
}
|
|
|
|
func NewConfigCommand() *cobra.Command {
|
|
opts := configOptions{}
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "config",
|
|
Short: "Show the current Caddy configuration (Caddyfile).",
|
|
Long: "Display the current Caddy configuration (Caddyfile) from the connected machine or a specified one.",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
|
return runConfig(cmd.Context(), uncli, opts)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&opts.machine, "machine", "m", "",
|
|
"Name or ID of the machine to get the configuration from. (default is connected machine)")
|
|
cmd.Flags().BoolVar(&opts.noColor, "no-color", false,
|
|
"Disable syntax highlighting for the output.")
|
|
|
|
completion.MachinesFlag(cmd)
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runConfig(ctx context.Context, uncli *cli.CLI, opts configOptions) error {
|
|
clusterClient, err := uncli.ConnectCluster(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("connect to cluster: %w", err)
|
|
}
|
|
defer clusterClient.Close()
|
|
|
|
if opts.machine != "" {
|
|
// If a specific machine is requested, use it to get the Caddy configuration.
|
|
ctx, _, err = clusterClient.ProxyMachinesContext(ctx, []string{opts.machine})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
config, err := clusterClient.Caddy.GetConfig(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("get Caddy config: %w", err)
|
|
}
|
|
|
|
// Print the Caddyfile with syntax highlighting.
|
|
if opts.noColor {
|
|
fmt.Print(config.Caddyfile)
|
|
} else {
|
|
if err = quick.Highlight(os.Stdout, config.Caddyfile, "caddy", "terminal256", "monokai"); err != nil {
|
|
// If highlighting fails, fall back to plain output.
|
|
fmt.Print(config.Caddyfile)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|