|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + systemLumpapplyListOutput string |
| 15 | + systemLumpapplyListJQ string |
| 16 | + systemLumpapplyShowJQ string |
| 17 | +) |
| 18 | + |
| 19 | +var systemLumpapplyCmd = &cobra.Command{ |
| 20 | + Use: "lumpapply", |
| 21 | + Short: "Manage X-point auto-apply (lumpapply) definitions", |
| 22 | +} |
| 23 | + |
| 24 | +var systemLumpapplyListCmd = &cobra.Command{ |
| 25 | + Use: "list", |
| 26 | + Short: "List auto-apply (lumpapply) registrations", |
| 27 | + Long: "List auto-apply registrations via GET /api/v1/system/lumpapply. Requires an administrator account.", |
| 28 | + RunE: runSystemLumpapplyList, |
| 29 | +} |
| 30 | + |
| 31 | +var systemLumpapplyShowCmd = &cobra.Command{ |
| 32 | + Use: "show <lumpapplyid>", |
| 33 | + Short: "Show an auto-apply (lumpapply) definition", |
| 34 | + Long: `Fetch definition details via GET /api/v1/system/lumpapply/{lumpapplyid}. |
| 35 | +
|
| 36 | +The argument is the numeric lumpapplyid (shown by ` + "`" + `system lumpapply list` + "`" + `). |
| 37 | +The response is returned as JSON (shape: csv_format / apply / create / lastaprv / ...). |
| 38 | +Requires an administrator account.`, |
| 39 | + Args: cobra.ExactArgs(1), |
| 40 | + RunE: runSystemLumpapplyShow, |
| 41 | +} |
| 42 | + |
| 43 | +func init() { |
| 44 | + systemCmd.AddCommand(systemLumpapplyCmd) |
| 45 | + systemLumpapplyCmd.AddCommand(systemLumpapplyListCmd) |
| 46 | + systemLumpapplyCmd.AddCommand(systemLumpapplyShowCmd) |
| 47 | + |
| 48 | + lf := systemLumpapplyListCmd.Flags() |
| 49 | + lf.StringVarP(&systemLumpapplyListOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)") |
| 50 | + lf.StringVar(&systemLumpapplyListJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)") |
| 51 | + |
| 52 | + sf := systemLumpapplyShowCmd.Flags() |
| 53 | + sf.StringVar(&systemLumpapplyShowJQ, "jq", "", "apply a gojq filter to the JSON response") |
| 54 | +} |
| 55 | + |
| 56 | +func runSystemLumpapplyList(cmd *cobra.Command, _ []string) error { |
| 57 | + client, err := newClientFromFlags(cmd.Context()) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + res, err := client.ListLumpapply(cmd.Context()) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + return render(res, resolveOutputFormat(systemLumpapplyListOutput), systemLumpapplyListJQ, func() error { |
| 67 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 68 | + defer w.Flush() |
| 69 | + fmt.Fprintln(w, "ID\tNAME\tFORM_ID\tFORM_CODE\tFORM_NAME\tROUTE_ID\tROUTE_CODE\tROUTE_NAME") |
| 70 | + for _, l := range res.Lumpapply { |
| 71 | + fmt.Fprintf(w, "%d\t%s\t%d\t%s\t%s\t%d\t%s\t%s\n", |
| 72 | + l.ID, l.Name, l.FormID, l.FormCode, l.FormName, l.RouteID, l.RouteCode, l.RouteName, |
| 73 | + ) |
| 74 | + } |
| 75 | + return nil |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func runSystemLumpapplyShow(cmd *cobra.Command, args []string) error { |
| 80 | + idArg := strings.TrimSpace(args[0]) |
| 81 | + lumpapplyID, err := strconv.Atoi(idArg) |
| 82 | + if err != nil || lumpapplyID <= 0 { |
| 83 | + return fmt.Errorf("lumpapplyid must be a positive integer: %q", args[0]) |
| 84 | + } |
| 85 | + |
| 86 | + client, err := newClientFromFlags(cmd.Context()) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + raw, err := client.GetLumpapply(cmd.Context(), lumpapplyID) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + if systemLumpapplyShowJQ != "" { |
| 96 | + return runJQ(raw, systemLumpapplyShowJQ) |
| 97 | + } |
| 98 | + return writeJSON(os.Stdout, raw) |
| 99 | +} |
0 commit comments