Files
larksuite__cli/shortcuts/doc/docs_create.go
SunPeiYang996 a6f3e635d4 feat(docs): add local authoring and resource workflows (#1921)
* feat(docs): add local authoring and resource workflows

Add docs +script workflows for isolated draft initialization and tolerant XML/Markdown profiling.

Support local and remote document resources across create and update flows with safe, bounded-concurrency uploads, binding verification, and cleanup.

Synchronize shared credential-source selection during concurrent uploads, refresh lark-doc guidance, and expand unit, dry-run, and live E2E coverage.

* docs(lark-doc): clarify genre reference paths

* fix(docs): address PR validation feedback

* docs(lark-doc): clarify remote image handling

* feat: streamline docs draft workflow

* fix(docs): clarify script input and resource cleanup

* fix(docs): align script dry-run test with auth flow

* fix(docs): authenticate local script e2e test

* fix(docs): refine script diagnostics and image preflight

* fix(docs): align draft workspace cleanup with VFS

* fix(docs): route workspace cleanup through FileIO

* docs(lark-doc): simplify profile check guidance
2026-08-07 18:15:28 +08:00

90 lines
2.4 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package doc
import (
"context"
"strings"
"github.com/spf13/cobra"
"github.com/larksuite/cli/shortcuts/common"
)
const docsContentPathAnnotation = "lark-cli.docs.content-input-path"
// v1CreateFlags returns hidden parse-only compatibility flags for old v1 commands.
func v1CreateFlags() []common.Flag {
return docsLegacyFlagDefinitions(docsCreateLegacyFlags())
}
var docsCreateLocalResourceScopes = []string{
"docs:document.media:upload",
"docx:document:write_only",
"docx:document:readonly",
}
var DocsCreate = common.Shortcut{
Service: "docs",
Command: "+create",
Description: "Create a Lark document",
Risk: "write",
AuthTypes: []string{"user", "bot"},
Scopes: []string{"docx:document:create"},
ConditionalScopes: docsCreateLocalResourceScopes,
PostMount: installDocsContentPathCapture,
Flags: concatFlags(
[]common.Flag{
docsAPIVersionCompatFlag(),
},
v2CreateFlags(),
v1CreateFlags(),
),
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
return validateCreateV2(ctx, runtime)
},
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
return dryRunCreateV2(ctx, runtime)
},
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
return executeCreateV2(ctx, runtime)
},
}
func installDocsContentPathCapture(cmd *cobra.Command) {
previousPreRunE := cmd.PreRunE
cmd.PreRunE = func(command *cobra.Command, args []string) error {
if previousPreRunE != nil {
if err := previousPreRunE(command, args); err != nil {
return err
}
}
captureDocsContentPath(command)
return nil
}
}
func captureDocsContentPath(cmd *cobra.Command) {
if cmd.Annotations == nil {
cmd.Annotations = make(map[string]string)
}
delete(cmd.Annotations, docsContentPathAnnotation)
raw, err := cmd.Flags().GetString("content")
if err != nil || !strings.HasPrefix(raw, "@") || strings.HasPrefix(raw, "@@") {
return
}
if path := strings.TrimSpace(strings.TrimPrefix(raw, "@")); path != "" {
cmd.Annotations[docsContentPathAnnotation] = path
}
}
// concatFlags combines multiple flag slices into one.
func concatFlags(slices ...[]common.Flag) []common.Flag {
var out []common.Flag
for _, s := range slices {
out = append(out, s...)
}
return out
}