Gitfed
bastien-mrq/gitfed / cmd / gitfed-tui / main.go
// gitfed-tui is the admin terminal UI for managing users, repos, ACLs and
// the federation trust store, per DESIGN.md §2/§9 (Phase 1-2).
//
// It talks to a running gitfed-server over its admin socket when one is up
// (live mode), or opens the store directly when it isn't (offline mode,
// e.g. first-time setup before the server has ever run). See
// internal/opsconnect for the shared connection logic (also used by
// gitfed-web).
package main

import (
	"flag"
	"fmt"
	"os"

	tea "github.com/charmbracelet/bubbletea"

	"git.neuromancer.ovh/bastien-mrq/gitfed/internal/config"
	"git.neuromancer.ovh/bastien-mrq/gitfed/internal/opsconnect"
)

func main() {
	configPath := flag.String("config", "gitfed.json", "path to instance config file")
	setPublic := flag.String("set-public", "", "non-interactive: make <repo> public and exit, skipping the TUI")
	setPrivate := flag.String("set-private", "", "non-interactive: make <repo> private and exit, skipping the TUI")
	flag.Parse()

	cfg, err := config.Load(*configPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "gitfed-tui: load config: %v\n", err)
		fmt.Fprintln(os.Stderr, "hint: run gitfed-server -init <domain> first")
		os.Exit(1)
	}

	ops, mode, closeFn, err := opsconnect.Connect(cfg)
	if err != nil {
		fmt.Fprintf(os.Stderr, "gitfed-tui: %v\n", err)
		os.Exit(1)
	}
	defer closeFn()

	// -set-public/-set-private exist so this one operation is scriptable
	// (e.g. over `kubectl exec`) without driving the interactive TUI's
	// keystrokes against a live instance — every other admin action still
	// goes through the TUI on purpose.
	if *setPublic != "" || *setPrivate != "" {
		if *setPublic != "" && *setPrivate != "" {
			fmt.Fprintln(os.Stderr, "gitfed-tui: pass only one of -set-public / -set-private")
			os.Exit(1)
		}
		repo, public := *setPublic, true
		if *setPrivate != "" {
			repo, public = *setPrivate, false
		}
		if err := ops.SetRepoPublic(repo, public); err != nil {
			fmt.Fprintf(os.Stderr, "gitfed-tui: set-public %s: %v\n", repo, err)
			os.Exit(1)
		}
		fmt.Printf("%s is now %s\n", repo, map[bool]string{true: "public", false: "private"}[public])
		return
	}

	p := tea.NewProgram(newModel(ops, cfg.Domain, mode), tea.WithAltScreen())
	if _, err := p.Run(); err != nil {
		fmt.Fprintf(os.Stderr, "gitfed-tui: %v\n", err)
		os.Exit(1)
	}
}