|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/pepabo/xpoint-cli/internal/xpoint" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + systemFormListOutput string |
| 16 | + systemFormListJQ string |
| 17 | + systemFormShowOutput string |
| 18 | + systemFormShowJQ string |
| 19 | +) |
| 20 | + |
| 21 | +var systemCmd = &cobra.Command{ |
| 22 | + Use: "system", |
| 23 | + Short: "X-point system (admin) APIs", |
| 24 | +} |
| 25 | + |
| 26 | +var systemFormCmd = &cobra.Command{ |
| 27 | + Use: "form", |
| 28 | + Short: "Manage X-point forms via admin APIs", |
| 29 | +} |
| 30 | + |
| 31 | +var systemFormListCmd = &cobra.Command{ |
| 32 | + Use: "list", |
| 33 | + Short: "List registration forms (admin)", |
| 34 | + Long: "List all registered forms via GET /api/v1/system/forms. Requires an administrator account.", |
| 35 | + RunE: runSystemFormList, |
| 36 | +} |
| 37 | + |
| 38 | +var systemFormShowCmd = &cobra.Command{ |
| 39 | + Use: "show <form_code|form_id>", |
| 40 | + Short: "Show a form definition (admin)", |
| 41 | + Long: `Fetch field definitions via GET /api/v1/system/forms/{fid}. |
| 42 | +
|
| 43 | +The argument may be a form_code (e.g. "TORIHIKISAKI_a") or a numeric |
| 44 | +form_id. When a form_code is given, the CLI first calls |
| 45 | +/api/v1/system/forms to resolve the id. Requires an administrator |
| 46 | +account.`, |
| 47 | + Args: cobra.ExactArgs(1), |
| 48 | + RunE: runSystemFormShow, |
| 49 | +} |
| 50 | + |
| 51 | +func init() { |
| 52 | + rootCmd.AddCommand(systemCmd) |
| 53 | + systemCmd.AddCommand(systemFormCmd) |
| 54 | + systemFormCmd.AddCommand(systemFormListCmd) |
| 55 | + systemFormCmd.AddCommand(systemFormShowCmd) |
| 56 | + |
| 57 | + lf := systemFormListCmd.Flags() |
| 58 | + lf.StringVarP(&systemFormListOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)") |
| 59 | + lf.StringVar(&systemFormListJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)") |
| 60 | + |
| 61 | + sf := systemFormShowCmd.Flags() |
| 62 | + sf.StringVarP(&systemFormShowOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)") |
| 63 | + sf.StringVar(&systemFormShowJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)") |
| 64 | +} |
| 65 | + |
| 66 | +func runSystemFormList(cmd *cobra.Command, args []string) error { |
| 67 | + client, err := newClientFromFlags(cmd.Context()) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + res, err := client.ListSystemForms(cmd.Context()) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + return render(res, resolveOutputFormat(systemFormListOutput), systemFormListJQ, func() error { |
| 77 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 78 | + defer w.Flush() |
| 79 | + fmt.Fprintln(w, "GROUP_ID\tGROUP_NAME\tFORMS\tFORM_ID\tFORM_CODE\tFORM_NAME\tPAGES\tTABLE") |
| 80 | + for _, g := range res.FormGroup { |
| 81 | + if len(g.Form) == 0 { |
| 82 | + fmt.Fprintf(w, "%s\t%s\t%d\t-\t-\t-\t-\t-\n", g.ID, g.Name, g.FormCount) |
| 83 | + continue |
| 84 | + } |
| 85 | + for _, f := range g.Form { |
| 86 | + fmt.Fprintf(w, "%s\t%s\t%d\t%d\t%s\t%s\t%d\t%s\n", |
| 87 | + g.ID, g.Name, g.FormCount, f.ID, f.Code, f.Name, f.PageCount, f.TableName, |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + return nil |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func runSystemFormShow(cmd *cobra.Command, args []string) error { |
| 96 | + client, err := newClientFromFlags(cmd.Context()) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + formID, err := resolveSystemFormID(cmd.Context(), client, args[0]) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + res, err := client.GetSystemFormDetail(cmd.Context(), formID) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + return render(res, resolveOutputFormat(systemFormShowOutput), systemFormShowJQ, func() error { |
| 112 | + form := res.Form |
| 113 | + fmt.Fprintf(os.Stdout, "FORM: %s %s MAX_STEP: %d\n", form.Code, form.Name, form.MaxStep) |
| 114 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 115 | + defer w.Flush() |
| 116 | + fmt.Fprintln(w, "PAGE\tFIELD_ID\tTYPE\tREQUIRED\tUNIQUE\tARRAYSIZE\tLABEL") |
| 117 | + for _, p := range form.Pages { |
| 118 | + for _, f := range p.Fields { |
| 119 | + fmt.Fprintf(w, "%d\t%s\t%d\t%t\t%t\t%d\t%s\n", |
| 120 | + p.PageNo, f.FieldID, f.FieldType, f.Required, f.Unique, f.ArraySize, f.Label, |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + return nil |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +type systemFormLister interface { |
| 129 | + ListSystemForms(ctx context.Context) (*xpoint.SystemFormsListResponse, error) |
| 130 | +} |
| 131 | + |
| 132 | +// resolveSystemFormID mirrors resolveFormID but consults the admin forms list. |
| 133 | +func resolveSystemFormID(ctx context.Context, lister systemFormLister, arg string) (int, error) { |
| 134 | + if id, err := strconv.Atoi(arg); err == nil { |
| 135 | + return id, nil |
| 136 | + } |
| 137 | + forms, err := lister.ListSystemForms(ctx) |
| 138 | + if err != nil { |
| 139 | + return 0, fmt.Errorf("resolve form code: %w", err) |
| 140 | + } |
| 141 | + for _, g := range forms.FormGroup { |
| 142 | + for _, f := range g.Form { |
| 143 | + if f.Code == arg { |
| 144 | + return f.ID, nil |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return 0, fmt.Errorf("form code %q not found", arg) |
| 149 | +} |
0 commit comments