// Package entrypoint wires command services and reports failures once. package entrypoint import ( "context" "errors" "fmt" "os" "os/exec" "os/signal" "syscall" "time" "albedo/cli/internal/app" "albedo/cli/internal/cli" "albedo/cli/internal/component" "albedo/cli/internal/config" "albedo/cli/internal/daemon" "albedo/cli/internal/localdaemon" "albedo/cli/internal/storage" "albedo/cli/internal/terminal" "github.com/spf13/cobra" ) type Command func(cli.Dependencies) *cobra.Command func Main(command Command, checkout string, acpMode bool) { if err := run(command, checkout, acpMode, os.Args[1:]); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(component.ExitCode(err)) } } func run(command Command, checkout string, acpMode bool, args []string) error { ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() streams := cli.Streams{In: os.Stdin, Out: os.Stdout, Err: os.Stderr} home := config.HomeDir() term := &terminal.Service{In: streams.In, Out: streams.Out, Err: streams.Err, Capable: terminal.IsTTY(streams.In, streams.Out), Home: home} if os.Getenv("ALBEDO_NO_BROWSER") == "" { term.OpenBrowser = config.OpenBrowser } options := localdaemon.LocalOptions{HomeDir: home, ProjectRoot: config.ProjectRoot(checkout), UseServer: true} connection := &connector{ options: options, term: term, acpMode: acpMode, } application := &app.Service{Connect: connection.Connect, Existing: connection.Existing} store := &storage.Service{ Command: func(ctx context.Context, args ...string) (*exec.Cmd, error) { return localdaemon.LocalCommand(ctx, options, args...) }, Home: home, Now: time.Now, Running: func() (bool, error) { found, err := localdaemon.Discover(ctx, home) if _, compatibilityFailure := errors.AsType[*daemon.CompatibilityError](err); compatibilityFailure && found.Kind == localdaemon.Running { return true, nil } return found.Kind == localdaemon.Running, err }, DeleteSessions: application.DeleteSessions, OnlineReport: application.StorageReport, } workspace, err := os.Getwd() if err != nil { workspace = "." } return cli.ExecuteComponent(ctx, command, args, cli.Dependencies{Lifecycle: connection, Application: application, Storage: store, Terminal: term, Workspace: workspace, ProjectRoot: options.ProjectRoot}, streams) }