NewCmd initializes the pgadmin command
()
| 73 | |
| 74 | // NewCmd initializes the pgadmin command |
| 75 | func NewCmd() *cobra.Command { |
| 76 | var dryRun bool |
| 77 | var mode string |
| 78 | var pgadminImage string |
| 79 | |
| 80 | pgadminCmd := &cobra.Command{ |
| 81 | Use: "pgadmin4 [name]", |
| 82 | Short: "Creates a pgAdmin deployment", |
| 83 | Args: cobra.MinimumNArgs(1), |
| 84 | Long: `Creates a pgAdmin deployment configured to work with a CNPG Cluster.`, |
| 85 | GroupID: plugin.GroupIDMiscellaneous, |
| 86 | Example: pgadminExample, |
| 87 | RunE: func(_ *cobra.Command, args []string) error { |
| 88 | ctx := context.Background() |
| 89 | clusterName := args[0] |
| 90 | |
| 91 | if !slices.Contains([]string{string(ModeDesktop), string(ModeServer)}, mode) { |
| 92 | return fmt.Errorf("unknown mode: %s", mode) |
| 93 | } |
| 94 | |
| 95 | // Get the Cluster object |
| 96 | var cluster apiv1.Cluster |
| 97 | if err := plugin.Client.Get( |
| 98 | ctx, |
| 99 | client.ObjectKey{Namespace: plugin.Namespace, Name: clusterName}, |
| 100 | &cluster); err != nil { |
| 101 | return fmt.Errorf("could not get cluster: %v", err) |
| 102 | } |
| 103 | |
| 104 | pgAdminCmd, err := newCommand(&cluster, Mode(mode), dryRun, pgadminImage) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | if err := pgAdminCmd.execute(ctx); err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |
| 113 | if !pgAdminCmd.dryRun { |
| 114 | _ = usageExampleTemplate.Execute(os.Stdout, pgAdminCmd) |
| 115 | } |
| 116 | |
| 117 | return nil |
| 118 | }, |
| 119 | } |
| 120 | pgadminCmd.Flags().BoolVar( |
| 121 | &dryRun, |
| 122 | "dry-run", |
| 123 | false, |
| 124 | "When true prints the deployment manifest instead of creating it", |
| 125 | ) |
| 126 | |
| 127 | pgadminCmd.Flags().StringVar( |
| 128 | &mode, |
| 129 | "mode", |
| 130 | "server", |
| 131 | "Chooses between 'server' and 'desktop' (insecure) mode.", |
| 132 | ) |