- Change module from forge.lthn.ai/core/go to forge.lthn.ai/core/cli - Remove pkg/ directory (now served from core/go) - Add require + replace for forge.lthn.ai/core/go => ../go - Update go.work to include ../go workspace module - Fix all internal/cmd/* imports: pkg/ refs → forge.lthn.ai/core/go/pkg/ - Rename internal/cmd/sdk package to sdkcmd (avoids conflict with pkg/sdk) - Remove SDK library files from internal/cmd/sdk/ (now in core/go/pkg/sdk/) - Remove duplicate RAG helper functions from internal/cmd/rag/ - Remove stale cmd/core-ide/ (now in core/ide repo) - Update IDE variant to remove core-ide import - Fix test assertion for new module name - Run go mod tidy to sync dependencies core/cli is now a pure CLI application importing core/go for packages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package forge
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"forge.lthn.ai/core/go/pkg/cli"
|
|
fg "forge.lthn.ai/core/go/pkg/forge"
|
|
)
|
|
|
|
// Auth command flags.
|
|
var (
|
|
authURL string
|
|
authToken string
|
|
)
|
|
|
|
// addAuthCommand adds the 'auth' subcommand for authentication status and login.
|
|
func addAuthCommand(parent *cli.Command) {
|
|
cmd := &cli.Command{
|
|
Use: "auth",
|
|
Short: "Show authentication status",
|
|
Long: "Show the current Forgejo authentication status, or log in with a new token.",
|
|
RunE: func(cmd *cli.Command, args []string) error {
|
|
return runAuth()
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVar(&authURL, "url", "", "Forgejo instance URL")
|
|
cmd.Flags().StringVar(&authToken, "token", "", "API token (create at <url>/user/settings/applications)")
|
|
|
|
parent.AddCommand(cmd)
|
|
}
|
|
|
|
func runAuth() error {
|
|
// If credentials provided, save them first
|
|
if authURL != "" || authToken != "" {
|
|
if err := fg.SaveConfig(authURL, authToken); err != nil {
|
|
return err
|
|
}
|
|
if authURL != "" {
|
|
cli.Success(fmt.Sprintf("URL set to %s", authURL))
|
|
}
|
|
if authToken != "" {
|
|
cli.Success("Token saved")
|
|
}
|
|
}
|
|
|
|
// Always show current auth status
|
|
url, token, err := fg.ResolveConfig(authURL, authToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if token == "" {
|
|
cli.Blank()
|
|
cli.Print(" %s %s\n", dimStyle.Render("URL:"), valueStyle.Render(url))
|
|
cli.Print(" %s %s\n", dimStyle.Render("Auth:"), warningStyle.Render("not authenticated"))
|
|
cli.Print(" %s %s\n", dimStyle.Render("Hint:"), dimStyle.Render(fmt.Sprintf("core forge auth --token TOKEN (create at %s/user/settings/applications)", url)))
|
|
cli.Blank()
|
|
return nil
|
|
}
|
|
|
|
client, err := fg.NewFromConfig(authURL, authToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user, _, err := client.API().GetMyUserInfo()
|
|
if err != nil {
|
|
cli.Blank()
|
|
cli.Print(" %s %s\n", dimStyle.Render("URL:"), valueStyle.Render(url))
|
|
cli.Print(" %s %s\n", dimStyle.Render("Auth:"), errorStyle.Render("token invalid or expired"))
|
|
cli.Blank()
|
|
return nil
|
|
}
|
|
|
|
cli.Blank()
|
|
cli.Success(fmt.Sprintf("Authenticated to %s", client.URL()))
|
|
cli.Print(" %s %s\n", dimStyle.Render("User:"), valueStyle.Render(user.UserName))
|
|
cli.Print(" %s %s\n", dimStyle.Render("Email:"), valueStyle.Render(user.Email))
|
|
if user.IsAdmin {
|
|
cli.Print(" %s %s\n", dimStyle.Render("Role:"), infoStyle.Render("admin"))
|
|
}
|
|
cli.Blank()
|
|
|
|
return nil
|
|
}
|