mirror of
https://github.com/larksuite/cli.git
synced 2026-07-07 00:55:53 +08:00
77 lines
2.7 KiB
Go
77 lines
2.7 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package base
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/larksuite/cli/internal/output"
|
|
"github.com/larksuite/cli/shortcuts/common"
|
|
)
|
|
|
|
var BaseFormQuestionsUpdate = common.Shortcut{
|
|
Service: "base",
|
|
Command: "+form-questions-update",
|
|
Description: "Update questions of a form in a Base table",
|
|
Risk: "write",
|
|
Scopes: []string{"base:form:update"},
|
|
AuthTypes: []string{"user", "bot"},
|
|
HasFormat: true,
|
|
Flags: []common.Flag{
|
|
{Name: "base-token", Desc: "Base token (base_token)", Required: true},
|
|
{Name: "table-id", Desc: "table ID", Required: true},
|
|
{Name: "form-id", Desc: "form ID", Required: true},
|
|
{Name: "questions", Desc: `questions JSON array, max 10 items, each item must include "id". Supported fields: "id"(required),"title","description"(plain text or markdown link like [text](https://example.com)),"required","option_display_mode"(0=dropdown,1=vertical,2=horizontal,select only). E.g. '[{"id":"q_001","title":"Updated?","required":true}]'`, Required: true},
|
|
},
|
|
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
|
return common.NewDryRunAPI().
|
|
PATCH("/open-apis/base/v3/bases/:base_token/tables/:table_id/forms/:form_id/questions").
|
|
Set("base_token", runtime.Str("base-token")).
|
|
Set("table_id", runtime.Str("table-id")).
|
|
Set("form_id", runtime.Str("form-id"))
|
|
},
|
|
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
|
baseToken := runtime.Str("base-token")
|
|
tableId := runtime.Str("table-id")
|
|
formId := runtime.Str("form-id")
|
|
questionsJSON := runtime.Str("questions")
|
|
|
|
var questions []interface{}
|
|
if err := json.Unmarshal([]byte(questionsJSON), &questions); err != nil {
|
|
return output.Errorf(output.ExitValidation, "invalid_json", "--questions must be a valid JSON array: %s", err)
|
|
}
|
|
|
|
data, err := baseV3Call(runtime, "PATCH",
|
|
baseV3Path("bases", baseToken, "tables", tableId, "forms", formId, "questions"),
|
|
nil, map[string]interface{}{"questions": questions})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
items, _ := data["items"].([]interface{})
|
|
if len(items) == 0 {
|
|
items, _ = data["questions"].([]interface{})
|
|
}
|
|
outData := map[string]interface{}{"questions": items}
|
|
|
|
runtime.OutFormat(outData, nil, func(w io.Writer) {
|
|
var rows []map[string]interface{}
|
|
for _, item := range items {
|
|
m, _ := item.(map[string]interface{})
|
|
rows = append(rows, map[string]interface{}{
|
|
"id": m["id"],
|
|
"title": m["title"],
|
|
"required": m["required"],
|
|
})
|
|
}
|
|
output.PrintTable(w, rows)
|
|
fmt.Fprintf(w, "\n%d question(s) updated\n", len(items))
|
|
})
|
|
return nil
|
|
},
|
|
}
|