Files
Fourier fe37d1f99b feat(auth): add multi-profile support with switch command
- Add profiles support to config structure
- Add auth switch command for interactive/direct profile switching
- Update auth login to support --profile flag
- Update auth status to show current profile
- Update getToken() to use profile system
- Backward compatible: auto-migrates legacy single-token config
- Add comprehensive tests for profile functionality

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-24 15:29:00 +08:00

70 lines
1.7 KiB
Go

package cmd
import (
"fmt"
"os"
"github.com/4ier/notion-cli/internal/config"
"github.com/spf13/cobra"
)
var (
outputFormat string
debugMode bool
// Version is set by goreleaser ldflags
Version = "dev"
)
var rootCmd = &cobra.Command{
Use: "notion",
Short: "Work seamlessly with Notion from the command line",
Long: `Work seamlessly with Notion from the command line.
Notion CLI lets you manage pages, databases, blocks, and more
without leaving your terminal. Built for developers and AI agents.`,
Version: Version,
SilenceUsage: true,
SilenceErrors: true,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().StringVarP(&outputFormat, "format", "f", "", "Output format: json, md, table, text (default: auto)")
rootCmd.PersistentFlags().BoolVar(&debugMode, "debug", false, "Show HTTP request/response details")
rootCmd.AddCommand(authCmd)
rootCmd.AddCommand(searchCmd)
rootCmd.AddCommand(pageCmd)
rootCmd.AddCommand(dbCmd)
rootCmd.AddCommand(blockCmd)
rootCmd.AddCommand(userCmd)
rootCmd.AddCommand(apiCmd)
rootCmd.AddCommand(commentCmd)
rootCmd.AddCommand(fileCmd)
}
// getToken returns the Notion API token from flag, env, or config file.
func getToken() (string, error) {
// 1. Environment variable
if token := os.Getenv("NOTION_TOKEN"); token != "" {
return token, nil
}
// 2. Config file (with profile support)
cfg, err := config.Load()
if err == nil {
profile := cfg.GetCurrentProfile()
if profile != nil && profile.Token != "" {
return profile.Token, nil
}
}
return "", fmt.Errorf("not authenticated. Run 'notion auth login --with-token' or set NOTION_TOKEN")
}