bf9395e022
CI / results (push) Blocked by required conditions
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / e2e-live (push) Waiting to run
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / deadcode (push) Waiting to run
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package profile
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
"github.com/larksuite/cli/internal/cmdutil"
|
|
"github.com/larksuite/cli/internal/core"
|
|
"github.com/larksuite/cli/internal/output"
|
|
)
|
|
|
|
// NewCmdProfileUse creates the profile use subcommand.
|
|
func NewCmdProfileUse(f *cmdutil.Factory) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "use <name>",
|
|
Short: "Switch to a profile (use '-' to toggle back)",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return profileUseRun(f, args[0])
|
|
},
|
|
}
|
|
cmdutil.SetTips(cmd, []string{
|
|
"AI agents: Do NOT switch profiles unless the user explicitly asks.",
|
|
})
|
|
cmdutil.SetRisk(cmd, "write")
|
|
return cmd
|
|
}
|
|
|
|
func profileUseRun(f *cmdutil.Factory, name string) error {
|
|
multi, err := core.LoadOrNotConfigured()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Handle "-" for toggle-back
|
|
if name == "-" {
|
|
if multi.PreviousApp == "" {
|
|
return errs.NewValidationError(errs.SubtypeFailedPrecondition, "no previous profile to switch back to").
|
|
WithHint("switch to a profile by name first: lark-cli profile use <name>")
|
|
}
|
|
name = multi.PreviousApp
|
|
}
|
|
|
|
app := multi.FindApp(name)
|
|
if app == nil {
|
|
return errs.NewValidationError(errs.SubtypeInvalidArgument, "profile %q not found, available profiles: %s", name, strings.Join(multi.ProfileNames(), ", "))
|
|
}
|
|
|
|
targetName := app.ProfileName()
|
|
|
|
// Short-circuit if already on the target profile
|
|
currentApp := multi.CurrentAppConfig("")
|
|
if currentApp != nil && currentApp.ProfileName() == targetName {
|
|
fmt.Fprintf(f.IOStreams.ErrOut, "Already on profile %q\n", targetName)
|
|
return nil
|
|
}
|
|
|
|
// Update previous and current
|
|
if currentApp != nil {
|
|
multi.PreviousApp = currentApp.ProfileName()
|
|
}
|
|
multi.CurrentApp = targetName
|
|
|
|
if err := core.SaveMultiAppConfig(multi); err != nil {
|
|
return errs.NewInternalError(errs.SubtypeStorage, "failed to save config: %v", err).WithCause(err)
|
|
}
|
|
|
|
output.PrintSuccess(f.IOStreams.ErrOut, fmt.Sprintf("Switched to profile %q (%s, %s)", targetName, app.AppId, app.Brand))
|
|
return nil
|
|
}
|