Files
mcq/cmd/mcq/login.go
Kyle Isom 3d5f52729f Add CLI client subcommands and MCP server
Adds push, list, get, delete, and login subcommands backed by an HTTP
API client, plus an MCP server for tool-based access to the document
queue.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 00:08:55 -07:00

72 lines
1.7 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/spf13/cobra"
"golang.org/x/term"
"git.wntrmute.dev/mc/mcq/internal/client"
)
func loginCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Authenticate with MCIAS and cache a token",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
server := clientFlags.server
if server == "" {
server = os.Getenv("MCQ_SERVER")
}
if server == "" {
return fmt.Errorf("server URL required: use --server or MCQ_SERVER")
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Username: ")
username, _ := reader.ReadString('\n')
username = strings.TrimSpace(username)
fmt.Print("Password: ")
passBytes, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return fmt.Errorf("read password: %w", err)
}
fmt.Println()
password := string(passBytes)
fmt.Print("TOTP code (blank if none): ")
totpCode, _ := reader.ReadString('\n')
totpCode = strings.TrimSpace(totpCode)
c := client.New(server, "")
token, err := c.Login(context.Background(), username, password, totpCode)
if err != nil {
return fmt.Errorf("login: %w", err)
}
path := tokenPath()
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
if err := os.WriteFile(path, []byte(token+"\n"), 0600); err != nil {
return fmt.Errorf("cache token: %w", err)
}
fmt.Printf("token cached to %s\n", path)
return nil
},
}
cmd.Flags().StringVar(&clientFlags.server, "server", "", "MCQ server URL (env: MCQ_SERVER)")
return cmd
}