1package cli
2
3import (
4 "context"
5 "fmt"
6 "io"
7
8 "github.com/spf13/cobra"
9)
10
11func newAutorouteCmd() *cobra.Command {
12 cmd := &cobra.Command{
13 Use: "autoroute",
14 Short: "Manage auto-route settings",
15 }
16 cmd.AddCommand(newAutorouteAllowCmd())
17 return cmd
18}
19
20func newAutorouteAllowCmd() *cobra.Command {
21 cmd := &cobra.Command{
22 Use: "allow",
23 Short: "Manage auto-route allowed domains",
24 }
25 cmd.AddCommand(newAutorouteAllowAddCmd())
26 cmd.AddCommand(newAutorouteAllowListCmd())
27 cmd.AddCommand(newAutorouteAllowRemoveCmd())
28 return cmd
29}
30
31func newAutorouteAllowAddCmd() *cobra.Command {
32 return &cobra.Command{
33 Use: "add <pattern>",
34 Short: "Allow an auto-route domain pattern",
35 Args: cobra.ExactArgs(1),
36 RunE: func(cmd *cobra.Command, args []string) error {
37 return runAutorouteAllowAdd(cmd.Context(), args, cmd.OutOrStdout())
38 },
39 }
40}
41
42func newAutorouteAllowListCmd() *cobra.Command {
43 var jsonOut bool
44 cmd := &cobra.Command{
45 Use: "list",
46 Short: "List auto-route allowed domains",
47 RunE: func(cmd *cobra.Command, args []string) error {
48 return runAutorouteAllowList(cmd.Context(), cmd.OutOrStdout(), jsonOut)
49 },
50 }
51 cmd.Flags().BoolVar(&jsonOut, "json", false, "Output as JSON")
52 return cmd
53}
54
55func newAutorouteAllowRemoveCmd() *cobra.Command {
56 return &cobra.Command{
57 Use: "remove <pattern>",
58 Short: "Remove an auto-route domain pattern",
59 Args: cobra.ExactArgs(1),
60 RunE: func(cmd *cobra.Command, args []string) error {
61 return runAutorouteAllowRemove(cmd.Context(), args, cmd.OutOrStdout())
62 },
63 }
64}
65
66func runAutorouteAllowAdd(ctx context.Context, args []string, out io.Writer) error {
67 handle, err := resolveControlPlane(configPath)
68 if err != nil {
69 return err
70 }
71 defer handle.close()
72 if err := handle.plane.AddAutoRouteAllowedDomain(ctx, args[0]); err != nil {
73 return fmt.Errorf("failed to add auto-route allowed domain: %w", err)
74 }
75 return cliWriteLine(out, cliRenderSuccess("Allowed domain added"))
76}
77
78func runAutorouteAllowList(ctx context.Context, out io.Writer, jsonOut bool) error {
79 handle, err := resolveControlPlane(configPath)
80 if err != nil {
81 return err
82 }
83 defer handle.close()
84 domains, err := handle.plane.GetAutoRouteAllowedDomains(ctx)
85 if err != nil {
86 return fmt.Errorf("failed to list auto-route allowed domains: %w", err)
87 }
88 if jsonOut {
89 return writeJSON(out, map[string]any{"domains": domains})
90 }
91 if len(domains) == 0 {
92 return cliWriteLine(out, cliRenderMuted("No allowed domains configured"))
93 }
94 for _, domain := range domains {
95 if err := cliWriteLine(out, domain); err != nil {
96 return err
97 }
98 }
99 return nil
100}
101
102func runAutorouteAllowRemove(ctx context.Context, args []string, out io.Writer) error {
103 handle, err := resolveControlPlane(configPath)
104 if err != nil {
105 return err
106 }
107 defer handle.close()
108 if err := handle.plane.RemoveAutoRouteAllowedDomain(ctx, args[0]); err != nil {
109 return fmt.Errorf("failed to remove auto-route allowed domain: %w", err)
110 }
111 return cliWriteLine(out, cliRenderSuccess("Allowed domain removed"))
112}