Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
package runtime
import (
"cmp"
"context"
"fmt"
"slices"
"strings"
"sync"
"time"
"github.com/getarcaneapp/arcane/types/v2/environment"
"github.com/getarcaneapp/arcane/cli/v2/internal/client"
"github.com/getarcaneapp/arcane/cli/v2/internal/config"
"github.com/getarcaneapp/arcane/cli/v2/internal/types"
)
type contextKey string
const (
// AppContextKey is the context key under which the CLI's AppContext is stored;
// set it with context.WithValue and read it back with From.
AppContextKey contextKey = "arcane_app_context"
OutputModeText OutputMode = "text"
OutputModeJSON OutputMode = "json"
)
type OutputMode string
type Options struct {
EnvOverride string
OutputMode OutputMode
AssumeYes bool
NoColor bool
RequestTimeout time.Duration
}
type AppContext struct {
Options
FleetConfirmed bool
cfg *types.Config
selectedEnv string
environmentsOnce sync.Once
environments []environment.Environment
environmentsErr error
clientOnce sync.Once
clientErr error
client *client.Client
unauthClientOnce sync.Once
unauthClientErr error
unauthClient *client.Client
}
func New(opts Options) (*AppContext, error) {
cfg, err := config.Load()
if err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
mode := cmp.Or(opts.OutputMode, OutputModeText)
switch mode {
case OutputModeText, OutputModeJSON:
default:
return nil, fmt.Errorf("invalid output mode %q (expected text or json)", mode)
}
opts.OutputMode = mode
opts.EnvOverride = strings.TrimSpace(opts.EnvOverride)
return &AppContext{Options: opts, cfg: cfg}, nil
}
func From(ctx context.Context) (*AppContext, bool) {
if ctx == nil {
return nil, false
}
app, ok := ctx.Value(AppContextKey).(*AppContext)
return app, ok && app != nil
}
func (a *AppContext) Config() *types.Config {
if a == nil || a.cfg == nil {
return nil
}
copyCfg := a.cfg.Clone()
if id := cmp.Or(a.selectedEnv, a.EnvOverride); id != "" {
copyCfg.DefaultEnvironment = id
}
return copyCfg
}
func (a *AppContext) EnvID() string {
if a == nil {
return ""
}
if id := cmp.Or(a.selectedEnv, a.EnvOverride); id != "" {
return id
}
if a.cfg != nil {
return a.cfg.DefaultEnvironment
}
return ""
}
func (a *AppContext) IsJSON() bool {
return a != nil && a.OutputMode == OutputModeJSON
}
func (a *AppContext) Client() (*client.Client, error) {
if a == nil {
return client.NewFromConfig()
}
a.clientOnce.Do(func() {
a.client, a.clientErr = client.New(a.Config())
if a.clientErr != nil {
return
}
if a.RequestTimeout > 0 {
a.client.SetTimeout(a.RequestTimeout)
}
})
return a.client, a.clientErr
}
func (a *AppContext) UnauthClient() (*client.Client, error) {
if a == nil {
return client.NewFromConfigUnauthenticated()
}
a.unauthClientOnce.Do(func() {
a.unauthClient, a.unauthClientErr = client.NewUnauthenticated(a.Config())
if a.unauthClientErr != nil {
return
}
if a.RequestTimeout > 0 {
a.unauthClient.SetTimeout(a.RequestTimeout)
}
})
return a.unauthClient, a.unauthClientErr
}
// PreviewEnabled reports whether this command still needs an environment scope.
func (a *AppContext) PreviewEnabled() bool {
return a != nil && a.cfg.Previews.AllEnvironments && a.EnvOverride == "" && a.selectedEnv == ""
}
// SelectEnvironment pins subsequent requests without changing persisted configuration.
func (a *AppContext) SelectEnvironment(id string) {
a.selectedEnv = id
if a.client != nil {
a.client.SetEnvironment(cmp.Or(id, a.EnvOverride, a.cfg.DefaultEnvironment, "0"))
}
}
// Environments loads the complete visible, enabled environment list once per invocation.
func (a *AppContext) Environments(ctx context.Context) ([]environment.Environment, error) {
a.environmentsOnce.Do(func() {
c, err := a.Client()
if err != nil {
a.environmentsErr = err
return
}
response, err := c.GetJSON[[]environment.Environment](ctx, types.Environments()+"?limit=-1")
if err != nil {
a.environmentsErr = fmt.Errorf("failed to discover environments: %w", err)
return
}
a.environments = slices.DeleteFunc(response.Data, func(env environment.Environment) bool { return !env.Enabled })
slices.SortFunc(a.environments, func(left, right environment.Environment) int {
return cmp.Or(cmp.Compare(strings.ToLower(cmp.Or(left.Name, left.ID)), strings.ToLower(cmp.Or(right.Name, right.ID))), cmp.Compare(left.ID, right.ID))
})
})
return a.environments, a.environmentsErr
}