mirror of
https://github.com/larksuite/cli.git
synced 2026-08-03 08:32:46 +08:00
Port changes from feat/strict-mode-identity-filter_3 branch: - Add strict mode for identity filtering and configuration - Add profile management commands (add/list/remove/rename/use) - Add credential extension framework (registry, env provider) - Add VFS abstraction layer - Refactor factory default and client options - Update shortcuts to use new credential and validation patterns Change-Id: I8c104c6b147e1901d94aefcefe35a174932c742b Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmdutil
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/larksuite/cli/internal/build"
|
|
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
|
|
)
|
|
|
|
const (
|
|
HeaderSource = "X-Cli-Source"
|
|
HeaderVersion = "X-Cli-Version"
|
|
HeaderShortcut = "X-Cli-Shortcut"
|
|
HeaderExecutionId = "X-Cli-Execution-Id"
|
|
|
|
SourceValue = "lark-cli"
|
|
|
|
HeaderUserAgent = "User-Agent"
|
|
)
|
|
|
|
// UserAgentValue returns the User-Agent value: "lark-cli/{version}".
|
|
func UserAgentValue() string {
|
|
return SourceValue + "/" + build.Version
|
|
}
|
|
|
|
// BaseSecurityHeaders returns headers that every request must carry.
|
|
func BaseSecurityHeaders() http.Header {
|
|
h := make(http.Header)
|
|
h.Set(HeaderSource, SourceValue)
|
|
h.Set(HeaderVersion, build.Version)
|
|
h.Set(HeaderUserAgent, UserAgentValue())
|
|
return h
|
|
}
|
|
|
|
// ── Context utilities ──
|
|
|
|
type ctxKey string
|
|
|
|
const (
|
|
ctxShortcutName ctxKey = "lark:shortcut-name"
|
|
ctxExecutionId ctxKey = "lark:execution-id"
|
|
)
|
|
|
|
// ContextWithShortcut injects shortcut name and execution ID into the context.
|
|
func ContextWithShortcut(ctx context.Context, name, executionId string) context.Context {
|
|
ctx = context.WithValue(ctx, ctxShortcutName, name)
|
|
ctx = context.WithValue(ctx, ctxExecutionId, executionId)
|
|
return ctx
|
|
}
|
|
|
|
// ShortcutNameFromContext extracts the shortcut name from the context.
|
|
func ShortcutNameFromContext(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(ctxShortcutName).(string)
|
|
return v, ok && v != ""
|
|
}
|
|
|
|
// ExecutionIdFromContext extracts the execution ID from the context.
|
|
func ExecutionIdFromContext(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(ctxExecutionId).(string)
|
|
return v, ok && v != ""
|
|
}
|
|
|
|
// ShortcutHeaderOpts extracts Shortcut info from the context and returns a
|
|
// RequestOptionFunc that injects the corresponding headers into SDK requests.
|
|
// Returns nil if the context has no Shortcut info.
|
|
func ShortcutHeaderOpts(ctx context.Context) larkcore.RequestOptionFunc {
|
|
h := ShortcutHeaders(ctx)
|
|
if h == nil {
|
|
return nil
|
|
}
|
|
return larkcore.WithHeaders(h)
|
|
}
|
|
|
|
// ShortcutHeaders extracts Shortcut info from the context and returns
|
|
// the corresponding HTTP headers. Returns nil if the context has no Shortcut info.
|
|
func ShortcutHeaders(ctx context.Context) http.Header {
|
|
name, ok := ShortcutNameFromContext(ctx)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
h := make(http.Header)
|
|
h.Set(HeaderShortcut, name)
|
|
if eid, ok := ExecutionIdFromContext(ctx); ok {
|
|
h.Set(HeaderExecutionId, eid)
|
|
}
|
|
return h
|
|
}
|