Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

refactor(rexctl): remove dead code and repeated helpers

Hampus0691742d037f
diff --git a/fluxer_rexctl/cmd/app.go b/fluxer_rexctl/cmd/app.go
index d15b98423..bddd09c80 100644
--- a/fluxer_rexctl/cmd/app.go
+++ b/fluxer_rexctl/cmd/app.go
@@ -113,7 +113,7 @@ func (a *App) warnUnknown(inst *instance.Instance) {
return
}
a.warned = true
- a.UI.Warn("This instance has settings this rexctl doesn't know: "+joinAnd(inst.State.Unknown)+".", "rexctl keeps them. Run rexctl upgrade if a newer rexctl wrote them.")
+ a.UI.Warn("This instance has settings this rexctl doesn't know: "+ui.JoinAnd(inst.State.Unknown)+".", "rexctl keeps them. Run rexctl upgrade if a newer rexctl wrote them.")
}
func rootlessGuard(inst *instance.Instance) error {
diff --git a/fluxer_rexctl/cmd/config.go b/fluxer_rexctl/cmd/config.go
index 12690962a..3ff2cedc0 100644
--- a/fluxer_rexctl/cmd/config.go
+++ b/fluxer_rexctl/cmd/config.go
@@ -49,7 +49,7 @@ func describeType(k adminapi.ConfigKey) string {
return "true or false"
}
if len(k.Options) > 0 {
- return adminapi.JoinOr(k.Options)
+ return ui.JoinOr(k.Options)
}
switch k.Type {
case "number":
@@ -74,7 +74,7 @@ var deploymentTypes = map[string]string{
func describeDeployType(k settings.Key) string {
if len(k.Options) > 0 {
- return adminapi.JoinOr(k.Options)
+ return ui.JoinOr(k.Options)
}
if k.Secret {
return deploymentTypes["secret"]
@@ -204,7 +204,7 @@ func extraRestartText(p deploy.Preview) string {
case len(extra) == 0:
return "This also restarts other Fluxer services."
}
- return "This also restarts " + joinAnd(extra) + "."
+ return "This also restarts " + ui.JoinAnd(extra) + "."
}
func restartText(services []string) string {
@@ -215,7 +215,7 @@ func restartText(services []string) string {
case len(parts) > 2:
return "Restarts Fluxer"
}
- return "Restarts " + joinAnd(parts)
+ return "Restarts " + ui.JoinAnd(parts)
}
type configRow struct {
@@ -610,7 +610,7 @@ func parseAdminChange(schema []adminapi.ConfigKey, name string, raw *string) (ad
mode := strings.ToLower(strings.TrimSpace(*raw))
patch, err := adminapi.RegistrationPatch(mode)
if err != nil {
- return adminChange{}, ui.Usage(fmt.Sprintf("%s needs one of %s.", k.Key, adminapi.JoinOr(adminapi.RegistrationModes)))
+ return adminChange{}, ui.Usage(fmt.Sprintf("%s needs one of %s.", k.Key, ui.JoinOr(adminapi.RegistrationModes)))
}
return adminChange{key: k, value: mode, patch: patch, mode: mode}, nil
}
diff --git a/fluxer_rexctl/cmd/install.go b/fluxer_rexctl/cmd/install.go
index 34066ab7f..b2a3c2622 100644
--- a/fluxer_rexctl/cmd/install.go
+++ b/fluxer_rexctl/cmd/install.go
@@ -113,7 +113,7 @@ func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlags
return err
}
if names := f.Setup.passedNames(c); f.SetupWhere == "browser" && len(names) > 0 {
- return &ui.ExitError{Code: ui.ExitUsage, Msg: fmt.Sprintf("--setup browser doesn't take %s.", joinAnd(names)), Fix: "Pass --setup terminal instead, or leave out the setup options and enter them in the browser."}
+ return &ui.ExitError{Code: ui.ExitUsage, Msg: fmt.Sprintf("--setup browser doesn't take %s.", ui.JoinAnd(names)), Fix: "Pass --setup terminal instead, or leave out the setup options and enter them in the browser."}
}
dir, err := app.Dir()
if err != nil {
@@ -238,7 +238,7 @@ func (in *installer) missingFlags() error {
if len(missing) == 0 {
return nil
}
- return ui.Usage("Pass " + joinAnd(missing) + ".")
+ return ui.Usage("Pass " + ui.JoinAnd(missing) + ".")
}
func notReady() error {
@@ -800,7 +800,7 @@ func (in *installer) takeOver(ctx context.Context, dir string) error {
return ui.Refused("Stopped without changing anything.", "")
}
}
- inst, _, err := runTakeover(ctx, in.app, legacy, takeoverOptions{DryRun: in.f.DryRun})
+ inst, _, err := takeoverDir(ctx, in.app, legacy.Dir, takeoverOptions{DryRun: in.f.DryRun})
if err != nil || in.f.DryRun || inst == nil {
return err
}
diff --git a/fluxer_rexctl/cmd/lifecycle.go b/fluxer_rexctl/cmd/lifecycle.go
index 3dd915de6..c2a1c4065 100644
--- a/fluxer_rexctl/cmd/lifecycle.go
+++ b/fluxer_rexctl/cmd/lifecycle.go
@@ -77,7 +77,7 @@ func servicesForName(name string) ([]string, error) {
return nil, &ui.ExitError{
Code: ui.ExitUsage,
Msg: fmt.Sprintf("There's no service called %s.", name),
- Fix: "Pick " + joinOr(logServiceNames()) + ".",
+ Fix: "Pick " + ui.JoinOr(logServiceNames()) + ".",
}
}
@@ -145,7 +145,7 @@ func engineFailureFor(kind engine.Kind, err error) error {
}
msg := registry + " is limiting downloads from this machine."
if e != nil && len(e.Images) > 0 {
- msg = fmt.Sprintf("%s is limiting downloads from this machine and %s didn't download. Images already on this machine weren't downloaded again.", registry, joinAnd(imageNames(e.Images)))
+ msg = fmt.Sprintf("%s is limiting downloads from this machine and %s didn't download. Images already on this machine weren't downloaded again.", registry, ui.JoinAnd(imageNames(e.Images)))
}
if registry == "Docker Hub" {
return failed(msg, "Wait for Docker Hub's limit to reset, which takes up to 6 hours, then try again.")
@@ -279,7 +279,7 @@ func waitingFor(names []string) string {
if len(names) > 3 {
return fmt.Sprintf("%s and %d more", strings.Join(names[:3], ", "), len(names)-3)
}
- return joinAnd(names)
+ return ui.JoinAnd(names)
}
type progress struct {
@@ -360,7 +360,7 @@ func newLogsCmd(app *App) *cobra.Command {
c := &cobra.Command{
Use: "logs [service]",
Short: "Show logs from your instance",
- Long: "Show logs from your instance.\n\nThe services are " + joinAnd(logServiceNames()) +
+ Long: "Show logs from your instance.\n\nThe services are " + ui.JoinAnd(logServiceNames()) +
".\nWithout a service, rexctl shows every service.",
Args: maxArgs(1),
RunE: func(c *cobra.Command, args []string) error {
@@ -399,16 +399,20 @@ func newLogsCmd(app *App) *cobra.Command {
}
func newStartCmd(app *App) *cobra.Command {
+ return newBootableCmd(app, "start", "Start your instance", runStart, runBootStart)
+}
+
+func newBootableCmd(app *App, use, short string, run, bootRun func(context.Context, *App) error) *cobra.Command {
var boot bool
c := &cobra.Command{
- Use: "start",
- Short: "Start your instance",
+ Use: use,
+ Short: short,
Args: noArgs,
RunE: func(c *cobra.Command, _ []string) error {
if boot {
- return runBootStart(c.Context(), app)
+ return bootRun(c.Context(), app)
}
- return runStart(c.Context(), app)
+ return run(c.Context(), app)
},
}
c.Flags().BoolVar(&boot, "boot", false, "")
@@ -584,21 +588,7 @@ func defaultRoute() bool {
}
func newStopCmd(app *App) *cobra.Command {
- var boot bool
- c := &cobra.Command{
- Use: "stop",
- Short: "Stop your instance",
- Args: noArgs,
- RunE: func(c *cobra.Command, _ []string) error {
- if boot {
- return runBootStop(c.Context(), app)
- }
- return runStop(c.Context(), app)
- },
- }
- c.Flags().BoolVar(&boot, "boot", false, "")
- _ = c.Flags().MarkHidden("boot")
- return c
+ return newBootableCmd(app, "stop", "Stop your instance", runStop, runBootStop)
}
func runStop(ctx context.Context, app *App) error {
diff --git a/fluxer_rexctl/cmd/registration.go b/fluxer_rexctl/cmd/registration.go
index 7b68129c6..9b2427688 100644
--- a/fluxer_rexctl/cmd/registration.go
+++ b/fluxer_rexctl/cmd/registration.go
@@ -31,7 +31,7 @@ func newRegistrationCmd(app *App) *cobra.Command {
set.RunE = func(c *cobra.Command, args []string) error {
patch, err := adminapi.RegistrationPatch(args[0])
if err != nil {
- return ui.Usage(fmt.Sprintf("Pass %s.", adminapi.JoinOr(adminapi.RegistrationModes)))
+ return ui.Usage(fmt.Sprintf("Pass %s.", ui.JoinOr(adminapi.RegistrationModes)))
}
return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
if err := api.PatchConfig(ctx, patch); err != nil {
diff --git a/fluxer_rexctl/cmd/rollback.go b/fluxer_rexctl/cmd/rollback.go
index c295bbe50..208785454 100644
--- a/fluxer_rexctl/cmd/rollback.go
+++ b/fluxer_rexctl/cmd/rollback.go
@@ -164,7 +164,7 @@ func runRollback(ctx context.Context, app *App, f *rollbackFlags) error {
return ui.Wrap(err, "Couldn't save your instance's state.", "Run rexctl doctor to see what's wrong.")
}
if len(put) > 0 {
- app.UI.Info("rexctl put back the settings Fluxer %s had: %s.", prevShort, joinAnd(put))
+ app.UI.Info("rexctl put back the settings Fluxer %s had: %s.", prevShort, ui.JoinAnd(put))
}
return nil
})
diff --git a/fluxer_rexctl/cmd/takeover.go b/fluxer_rexctl/cmd/takeover.go
index e5e4cbc44..f46444d75 100644
--- a/fluxer_rexctl/cmd/takeover.go
+++ b/fluxer_rexctl/cmd/takeover.go
@@ -164,13 +164,13 @@ func describeTakeover(u *ui.UI, r *instance.ImportReport) {
u.Info("Settings that appear twice in .env keep their last value.")
}
if len(r.Placeholders) > 0 {
- u.Info("These settings still have the example value from the old installer: %s. rexctl keeps them. Run rexctl config list to check them.", joinAnd(r.Placeholders))
+ u.Info("These settings still have the example value from the old installer: %s. rexctl keeps them. Run rexctl config list to check them.", ui.JoinAnd(r.Placeholders))
}
if n := len(r.AdminKeys); n > 0 {
u.Info("%s move into Fluxer's own settings, where rexctl config set changes them.", countWord(n, "One email, branding or billing setting", "email, branding and billing settings"))
}
if len(r.Changed) > 0 {
- u.Info("rexctl keeps the changes you made: %s.", joinAnd(r.Changed))
+ u.Info("rexctl keeps the changes you made: %s.", ui.JoinAnd(r.Changed))
}
if r.PodmanCompose {
u.Info("The first time rexctl starts or changes Fluxer, it replaces the containers podman-compose made. Fluxer restarts once and your data stays.")
@@ -267,7 +267,7 @@ func confirmKeptCopies(app *App, inst *instance.Instance) (bool, error) {
warnKeptCopy(app.UI, k.Name, k.Copy, true)
names = append(names, k.Copy)
}
- msg := fmt.Sprintf("Your changes in %s stop working after the upgrade.", joinAnd(names))
+ msg := fmt.Sprintf("Your changes in %s stop working after the upgrade.", ui.JoinAnd(names))
if !app.UI.Interactive() || app.UI.Yes() {
return false, ui.Refused(msg, "Move them, then delete the copies and run rexctl upgrade again.")
}
diff --git a/fluxer_rexctl/cmd/upgrade.go b/fluxer_rexctl/cmd/upgrade.go
index 55a3049f0..3273f7e3d 100644
--- a/fluxer_rexctl/cmd/upgrade.go
+++ b/fluxer_rexctl/cmd/upgrade.go
@@ -306,7 +306,7 @@ func upgradeInstance(ctx context.Context, app *App, dryRun bool) (*instance.Inst
if err != nil {
return nil, false, err
}
- inst, kept, err := runTakeover(ctx, app, legacy, takeoverOptions{DryRun: dryRun, Then: "Then rexctl upgrades it to the latest Fluxer release."})
+ inst, kept, err := takeoverDir(ctx, app, legacy.Dir, takeoverOptions{DryRun: dryRun, Then: "Then rexctl upgrades it to the latest Fluxer release."})
if err != nil || inst == nil {
return nil, false, err
}
diff --git a/fluxer_rexctl/cmd/users.go b/fluxer_rexctl/cmd/users.go
index c6ffdb4cf..3e67fe0ec 100644
--- a/fluxer_rexctl/cmd/users.go
+++ b/fluxer_rexctl/cmd/users.go
@@ -243,7 +243,7 @@ func newUsersCmd(app *App) *cobra.Command {
var reason string
var deleteMessages bool
ban := &cobra.Command{Use: "ban <user>", Short: "Ban an account", Args: exactArgs(userArg)}
- ban.Flags().StringVar(&reason, "reason", "", "why you're banning them, one of "+joinOr(adminapi.BanReasonNames()))
+ ban.Flags().StringVar(&reason, "reason", "", "why you're banning them, one of "+ui.JoinOr(adminapi.BanReasonNames()))
ban.Flags().BoolVar(&deleteMessages, "delete-messages", false, "also delete their messages")
ban.RunE = func(c *cobra.Command, args []string) error {
if r, ok := adminapi.FindBanReason(reason); ok {
@@ -260,7 +260,7 @@ func newUsersCmd(app *App) *cobra.Command {
Options: options,
Validate: func(s string) error {
if _, ok := adminapi.FindBanReason(s); !ok {
- return ui.Usage(fmt.Sprintf("Pass --reason with %s.", joinOr(adminapi.BanReasonNames())))
+ return ui.Usage(fmt.Sprintf("Pass --reason with %s.", ui.JoinOr(adminapi.BanReasonNames())))
}
return nil
},
@@ -270,7 +270,7 @@ func newUsersCmd(app *App) *cobra.Command {
}
why, ok := adminapi.FindBanReason(reason)
if !ok {
- return ui.Usage(fmt.Sprintf("Pass --reason with %s.", joinOr(adminapi.BanReasonNames())))
+ return ui.Usage(fmt.Sprintf("Pass --reason with %s.", ui.JoinOr(adminapi.BanReasonNames())))
}
return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {
u, err := api.ResolveUser(ctx, args[0])
diff --git a/fluxer_rexctl/internal/adminapi/config.go b/fluxer_rexctl/internal/adminapi/config.go
index 8cb5a1638..2bb2769de 100644
--- a/fluxer_rexctl/internal/adminapi/config.go
+++ b/fluxer_rexctl/internal/adminapi/config.go
@@ -10,6 +10,8 @@ import (
"slices"
"strconv"
"strings"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
)
//go:embed configschema.json
@@ -174,7 +176,7 @@ func ParseConfigValue(k ConfigKey, value string) (any, error) {
return v, nil
}
if len(k.Options) > 0 && !slices.Contains(k.Options, value) {
- return nil, &InvalidValueError{Key: k, Want: "one of " + JoinOr(k.Options)}
+ return nil, &InvalidValueError{Key: k, Want: "one of " + ui.JoinOr(k.Options)}
}
return value, nil
}
diff --git a/fluxer_rexctl/internal/adminapi/explain.go b/fluxer_rexctl/internal/adminapi/explain.go
index 474c8f614..8b45bade5 100644
--- a/fluxer_rexctl/internal/adminapi/explain.go
+++ b/fluxer_rexctl/internal/adminapi/explain.go
@@ -43,7 +43,7 @@ func Explain(err error) error {
for _, m := range ambiguous.Matches {
ids = append(ids, m.ID)
}
- return ui.Failed(fmt.Sprintf("%d accounts are called %s.", len(ambiguous.Matches), ambiguous.Ref), fmt.Sprintf("Pass a user ID. Theirs are %s.", joinAnd(ids)))
+ return ui.Failed(fmt.Sprintf("%d accounts are called %s.", len(ambiguous.Matches), ambiguous.Ref), fmt.Sprintf("Pass a user ID. Theirs are %s.", ui.JoinAnd(ids)))
case errors.As(err, &banBlocks):
return ui.Refused(fmt.Sprintf("%s is banned for a reason that doesn't allow closing the account.", banBlocks.Username), fmt.Sprintf("Run rexctl users unban %s, then delete the account.", banBlocks.Username))
case errors.As(err, &invite):
@@ -53,7 +53,7 @@ func Explain(err error) error {
case errors.As(err, &unknown):
fix := "Run rexctl config list to see every setting."
if len(unknown.Similar) > 0 && len(unknown.Similar) <= 3 {
- fix = "Did you mean " + JoinOr(unknown.Similar) + "?"
+ fix = "Did you mean " + ui.JoinOr(unknown.Similar) + "?"
}
return &ui.ExitError{Code: ui.ExitUsage, Msg: fmt.Sprintf("There's no setting called %s.", unknown.Key), Fix: fix}
case errors.As(err, &invalid):
diff --git a/fluxer_rexctl/internal/adminapi/schemagen/main.go b/fluxer_rexctl/internal/adminapi/schemagen/main.go
index b4963fa48..299a9afc8 100644
--- a/fluxer_rexctl/internal/adminapi/schemagen/main.go
+++ b/fluxer_rexctl/internal/adminapi/schemagen/main.go
@@ -5,8 +5,9 @@ package main
import (
"encoding/json"
"fmt"
+ "maps"
"os"
- "sort"
+ "slices"
"strings"
)
@@ -137,12 +138,7 @@ func (g *gen) walk(s *schema, path []string) {
return
}
if r.Type == "object" && len(r.Properties) > 0 && len(r.Required) == 0 && len(path) > 0 || r.Type == "object" && len(path) == 0 {
- names := make([]string, 0, len(r.Properties))
- for n := range r.Properties {
- names = append(names, n)
- }
- sort.Strings(names)
- for _, n := range names {
+ for _, n := range slices.Sorted(maps.Keys(r.Properties)) {
g.walk(r.Properties[n], append(append([]string{}, path...), n))
}
return
diff --git a/fluxer_rexctl/internal/backup/backup.go b/fluxer_rexctl/internal/backup/backup.go
index a3f67f3f2..8052f7fe5 100644
--- a/fluxer_rexctl/internal/backup/backup.go
+++ b/fluxer_rexctl/internal/backup/backup.go
@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "slices"
"sort"
"strings"
"time"
@@ -65,12 +66,7 @@ type Manifest struct {
}
func (m *Manifest) HasSkipped(what string) bool {
- for _, s := range m.Skipped {
- if s == what {
- return true
- }
- }
- return false
+ return slices.Contains(m.Skipped, what)
}
type Info struct {
diff --git a/fluxer_rexctl/internal/backup/create.go b/fluxer_rexctl/internal/backup/create.go
index 1046810e7..540094e6b 100644
--- a/fluxer_rexctl/internal/backup/create.go
+++ b/fluxer_rexctl/internal/backup/create.go
@@ -10,9 +10,11 @@ import (
"errors"
"fmt"
"io"
+ "maps"
"net/url"
"os"
"path/filepath"
+ "slices"
"strconv"
"strings"
"time"
@@ -140,7 +142,7 @@ func Create(ctx context.Context, inst *instance.Instance, p *engine.Project, opt
if bundledDB {
progress(StageDatabase)
- if !contains(running, postgresService) {
+ if !slices.Contains(running, postgresService) {
if len(volumes) == 0 {
defer p.Stop(context.WithoutCancel(ctx), postgresService)
}
@@ -456,16 +458,7 @@ func runningServices(ctx context.Context, p *engine.Project) ([]string, error) {
seen[c.Service] = true
}
}
- return sortedKeys(seen), nil
-}
-
-func contains(list []string, s string) bool {
- for _, v := range list {
- if v == s {
- return true
- }
- }
- return false
+ return slices.Sorted(maps.Keys(seen)), nil
}
func addDump(ctx context.Context, w *archiveWriter, p *engine.Project, spool string) error {
diff --git a/fluxer_rexctl/internal/backup/restore.go b/fluxer_rexctl/internal/backup/restore.go
index c1d267670..f644872ac 100644
--- a/fluxer_rexctl/internal/backup/restore.go
+++ b/fluxer_rexctl/internal/backup/restore.go
@@ -452,7 +452,7 @@ func restoreData(ctx context.Context, path string, p *engine.Project, image stri
if !ok {
return closeSink(nil)
}
- if !contains(m.Volumes, short) {
+ if !slices.Contains(m.Volumes, short) {
return fmt.Errorf("%w: %s isn't in the manifest", ErrDamaged, short)
}
if !volumesStarted {
diff --git a/fluxer_rexctl/internal/checks/engine.go b/fluxer_rexctl/internal/checks/engine.go
index 0cf84dcb9..b6029b213 100644
--- a/fluxer_rexctl/internal/checks/engine.go
+++ b/fluxer_rexctl/internal/checks/engine.go
@@ -53,11 +53,8 @@ func runEngine(ctx context.Context, e *Env) Result {
}
return fail(name+" isn't running.", "Start "+name+", then run rexctl doctor.")
}
- minimum := engine.MinDocker
- if e.Engine.Kind == engine.Podman {
- minimum = engine.MinPodman
- }
- if e.Engine.TooOld || versionLess(e.Engine.Version, minimum) {
+ minimum := e.Engine.MinVersion()
+ if e.Engine.TooOld || !engine.AtLeast(e.Engine.Version, minimum) {
if e.Engine.Kind == engine.Podman && e.installed() {
return fail(fmt.Sprintf("Podman %s is too old for Fluxer.", e.Engine.Version),
fmt.Sprintf("Update Podman to %s or newer, or move to Docker with rexctl backup and then rexctl restore <file> --engine docker.", minimum))
diff --git a/fluxer_rexctl/internal/checks/env_file.go b/fluxer_rexctl/internal/checks/env_file.go
index 3411a869c..60ec8859b 100644
--- a/fluxer_rexctl/internal/checks/env_file.go
+++ b/fluxer_rexctl/internal/checks/env_file.go
@@ -63,10 +63,10 @@ func fixEnvFile(ctx context.Context, e *Env) error {
for k, v := range r.Env.Settings() {
e.stage(k, v)
}
- for k, v := range r.Env.Secrets() {
+ for k, v := range r.Env.Values(instance.ChangeSecret) {
e.stageSecret(k, v)
}
- for k, v := range r.Env.Admin() {
+ for k, v := range r.Env.Values(instance.ChangeAdmin) {
e.stageAdmin(k, v)
}
e.importEnv = true
diff --git a/fluxer_rexctl/internal/checks/ports.go b/fluxer_rexctl/internal/checks/ports.go
index d0419d58b..d19ced908 100644
--- a/fluxer_rexctl/internal/checks/ports.go
+++ b/fluxer_rexctl/internal/checks/ports.go
@@ -12,6 +12,7 @@ import (
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
)
func init() {
@@ -59,18 +60,18 @@ func runPorts(ctx context.Context, e *Env) Result {
}
switch {
case first == nil:
- return pass(fmt.Sprintf("Ports %s are free.", joinAnd(numbers)))
+ return pass(fmt.Sprintf("Ports %s are free.", ui.JoinAnd(numbers)))
case len(busy) == 1:
return e.portBusy(*first, firstOwner)
}
- msg := fmt.Sprintf("Ports %s are already in use.", joinAnd(busy))
+ msg := fmt.Sprintf("Ports %s are already in use.", ui.JoinAnd(busy))
fix := "Stop the programs that use them, then run rexctl doctor."
switch {
case sameOwner && containerOwners[firstOwner]:
- msg = fmt.Sprintf("Ports %s are already in use by containers.", joinAnd(busy))
+ msg = fmt.Sprintf("Ports %s are already in use by containers.", ui.JoinAnd(busy))
fix = "Stop the containers that use them, then run rexctl doctor."
case sameOwner && firstOwner != "":
- msg = fmt.Sprintf("Ports %s are already in use by %s.", joinAnd(busy), firstOwner)
+ msg = fmt.Sprintf("Ports %s are already in use by %s.", ui.JoinAnd(busy), firstOwner)
fix = fmt.Sprintf("Stop %s, then run rexctl doctor.", firstOwner)
}
if e.mode() == modeBundled && !slices.ContainsFunc(busy, func(n string) bool { return n != "80" && n != "443" }) {
diff --git a/fluxer_rexctl/internal/checks/unfinished.go b/fluxer_rexctl/internal/checks/unfinished.go
index 786e17e58..479c90a5d 100644
--- a/fluxer_rexctl/internal/checks/unfinished.go
+++ b/fluxer_rexctl/internal/checks/unfinished.go
@@ -8,6 +8,7 @@ import (
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/release"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
)
func init() {
@@ -22,7 +23,7 @@ func runPendingChange(ctx context.Context, e *Env) Result {
}
msg := "A settings change didn't finish."
if len(st.Pending.Keys) > 0 {
- msg = fmt.Sprintf("The change to %s didn't finish.", joinAnd(st.Pending.Keys))
+ msg = fmt.Sprintf("The change to %s didn't finish.", ui.JoinAnd(st.Pending.Keys))
}
r := fixable(fail(msg, "Run rexctl doctor --fix to put the old settings back."))
r.Details = st.Pending.Keys
diff --git a/fluxer_rexctl/internal/deploy/api.go b/fluxer_rexctl/internal/deploy/api.go
index a29d915d9..928be969c 100644
--- a/fluxer_rexctl/internal/deploy/api.go
+++ b/fluxer_rexctl/internal/deploy/api.go
@@ -2,12 +2,7 @@
package deploy
-import (
- "context"
-
- "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
- "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
-)
+import "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
type ApplyOptions struct {
Pull bool
diff --git a/fluxer_rexctl/internal/deploy/apply.go b/fluxer_rexctl/internal/deploy/apply.go
index 6aacea9fa..1cdd76a03 100644
--- a/fluxer_rexctl/internal/deploy/apply.go
+++ b/fluxer_rexctl/internal/deploy/apply.go
@@ -14,7 +14,7 @@ import (
const edgeService = "edge"
-func apply(ctx context.Context, inst *instance.Instance, e *engine.Engine, o ApplyOptions) (*Change, error) {
+func Apply(ctx context.Context, inst *instance.Instance, e *engine.Engine, o ApplyOptions) (*Change, error) {
if err := refuseEjected(inst); err != nil {
return nil, err
}
diff --git a/fluxer_rexctl/internal/deploy/resolve.go b/fluxer_rexctl/internal/deploy/resolve.go
index 1c55d0f2d..49fe3104f 100644
--- a/fluxer_rexctl/internal/deploy/resolve.go
+++ b/fluxer_rexctl/internal/deploy/resolve.go
@@ -27,7 +27,7 @@ func Interrupted(inst *instance.Instance) string {
strings.Join(p.Keys[:len(p.Keys)-1], ", "), p.Keys[len(p.Keys)-1])
}
-func resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) error {
+func Resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) error {
p := inst.State.Pending
if p == nil {
return nil
diff --git a/fluxer_rexctl/internal/deploy/set.go b/fluxer_rexctl/internal/deploy/set.go
index 00a2e4dfc..165e1d549 100644
--- a/fluxer_rexctl/internal/deploy/set.go
+++ b/fluxer_rexctl/internal/deploy/set.go
@@ -62,7 +62,7 @@ func keyError(err error) error {
return err
}
-func set(ctx context.Context, inst *instance.Instance, e *engine.Engine, changes map[string]*string, o ApplyOptions) (*Change, error) {
+func Set(ctx context.Context, inst *instance.Instance, e *engine.Engine, changes map[string]*string, o ApplyOptions) (*Change, error) {
keys := slices.Sorted(maps.Keys(changes))
if err := refuseEjected(inst); err != nil {
return nil, err
diff --git a/fluxer_rexctl/internal/deploy/spec.go b/fluxer_rexctl/internal/deploy/spec.go
index 79b9d0cd2..130babff8 100644
--- a/fluxer_rexctl/internal/deploy/spec.go
+++ b/fluxer_rexctl/internal/deploy/spec.go
@@ -79,7 +79,7 @@ func specFor(inst *instance.Instance, set *stack.Set, chosen map[string]string)
return engine.Spec{Name: inst.Project(), Dir: inst.Dir, Files: composeFiles(inst, chosen), Env: env}, nil
}
-func spec(inst *instance.Instance) (engine.Spec, error) {
+func Spec(inst *instance.Instance) (engine.Spec, error) {
set, err := ReleaseSet(inst)
if err != nil {
return engine.Spec{}, err
@@ -87,8 +87,8 @@ func spec(inst *instance.Instance) (engine.Spec, error) {
return specFor(inst, set, inst.State.Settings)
}
-func load(ctx context.Context, inst *instance.Instance, e *engine.Engine) (*engine.Project, error) {
- s, err := spec(inst)
+func Load(ctx context.Context, inst *instance.Instance, e *engine.Engine) (*engine.Project, error) {
+ s, err := Spec(inst)
if err != nil {
return nil, err
}
diff --git a/fluxer_rexctl/internal/host/bootunit.go b/fluxer_rexctl/internal/host/bootunit.go
index f91a6a730..7ab17d85a 100644
--- a/fluxer_rexctl/internal/host/bootunit.go
+++ b/fluxer_rexctl/internal/host/bootunit.go
@@ -167,22 +167,13 @@ func unitExists(ctx context.Context, rootless bool, name string) bool {
return err == nil && strings.Contains(string(out), name)
}
-func unitEnabled(ctx context.Context, rootless bool, name string) bool {
- args := []string{"is-enabled", name}
+func unitIs(ctx context.Context, rootless bool, name, state string) bool {
+ args := []string{"is-" + state, name}
if rootless {
args = append([]string{"--user"}, args...)
}
out, _ := exec.CommandContext(ctx, "systemctl", args...).Output()
- return strings.TrimSpace(string(out)) == "enabled"
-}
-
-func unitActive(ctx context.Context, rootless bool, name string) bool {
- args := []string{"is-active", name}
- if rootless {
- args = append([]string{"--user"}, args...)
- }
- out, _ := exec.CommandContext(ctx, "systemctl", args...).Output()
- return strings.TrimSpace(string(out)) == "active"
+ return strings.TrimSpace(string(out)) == state
}
func CheckBootUnit(ctx context.Context, u BootUnit) (BootUnitState, error) {
@@ -202,7 +193,7 @@ func CheckBootUnit(ctx context.Context, u BootUnit) (BootUnitState, error) {
return s, err
default:
s.Stale = string(data) != u.content(ctx)
- s.Disabled = !unitEnabled(ctx, u.Rootless, u.Name())
+ s.Disabled = !unitIs(ctx, u.Rootless, u.Name(), "enabled")
}
if !u.Rootless {
s.CopyStale = !sameFile(u.Binary, LibexecBinary)
@@ -228,7 +219,7 @@ func InstallBootUnit(ctx context.Context, u BootUnit) error {
return err
}
}
- if !unitActive(ctx, u.Rootless, u.Name()) {
+ if !unitIs(ctx, u.Rootless, u.Name(), "active") {
return systemctlRun(ctx, u.Rootless, "start", "--no-block", u.Name())
}
return nil
@@ -259,7 +250,7 @@ func RemoveBootUnit(ctx context.Context, u BootUnit) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}
- if unitEnabled(ctx, u.Rootless, u.Name()) {
+ if unitIs(ctx, u.Rootless, u.Name(), "enabled") {
if err := systemctlRun(ctx, u.Rootless, "disable", u.Name()); err != nil {
return err
}
diff --git a/fluxer_rexctl/internal/instance/adopt.go b/fluxer_rexctl/internal/instance/adopt.go
index 94b92e7b5..be7e2302a 100644
--- a/fluxer_rexctl/internal/instance/adopt.go
+++ b/fluxer_rexctl/internal/instance/adopt.go
@@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io/fs"
+ "maps"
"os"
"path/filepath"
"regexp"
@@ -269,7 +270,7 @@ func adoptFiles(ctx context.Context, p *AdoptPlan) error {
if err := p.snapshot(); err != nil {
return err
}
- for _, src := range sortedKeys(p.Mine) {
+ for _, src := range slices.Sorted(maps.Keys(p.Mine)) {
if err := ctx.Err(); err != nil {
return err
}
@@ -320,7 +321,7 @@ func (p *AdoptPlan) keepRunning() error {
if l.existed(stack.Caddyfile) {
want[stack.Caddyfile] = filepath.Join(p.Dir, stack.Caddyfile)
}
- for _, name := range sortedKeys(want) {
+ for _, name := range slices.Sorted(maps.Keys(want)) {
dst := filepath.Join(p.Dir, name)
if fi, err := os.Stat(dst); err == nil && fi.Mode().IsRegular() {
if perm := fi.Mode().Perm(); perm&0o022 != 0 {
@@ -392,7 +393,7 @@ func (p *AdoptPlan) snapshot() error {
names = append(names, f)
}
names = append(names, p.DeleteFiles...)
- names = append(names, sortedKeys(p.Mine)...)
+ names = append(names, slices.Sorted(maps.Keys(p.Mine))...)
slices.Sort(names)
names = slices.Compact(names)
for _, name := range names {
diff --git a/fluxer_rexctl/internal/instance/drift.go b/fluxer_rexctl/internal/instance/drift.go
index 6c810a918..e4c2c660d 100644
--- a/fluxer_rexctl/internal/instance/drift.go
+++ b/fluxer_rexctl/internal/instance/drift.go
@@ -14,6 +14,7 @@ import (
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
)
type DriftReport struct {
@@ -97,7 +98,7 @@ func (r *DriftReport) Err() error {
name := r.Changed[0]
msg := fmt.Sprintf("%s was changed by hand. rexctl writes this file and would undo your change.", name)
if len(r.Changed) > 1 {
- msg = fmt.Sprintf("%s were changed by hand. rexctl writes these files and would undo your changes.", joinAnd(r.Changed))
+ msg = fmt.Sprintf("%s were changed by hand. rexctl writes these files and would undo your changes.", ui.JoinAnd(r.Changed))
}
return &DriftError{Msg: msg, Fix: "Run rexctl doctor --fix to put rexctl's version back, or rexctl eject to manage the files yourself."}
}
@@ -119,7 +120,7 @@ func (r *EnvDrift) Fix() string {
if len(r.Unused) == 0 {
return "Run rexctl doctor --fix to keep these values and remove the file, or change them with these commands. rexctl removes the file once you've set each of them:"
}
- unused := fmt.Sprintf("Fluxer doesn't use %s. Run rexctl config list to find the setting you meant and change it with rexctl config set.", joinAnd(r.Unused))
+ unused := fmt.Sprintf("Fluxer doesn't use %s. Run rexctl config list to find the setting you meant and change it with rexctl config set.", ui.JoinAnd(r.Unused))
if len(r.Changes) == 0 {
return unused + " Then delete " + r.Path + "."
}
@@ -137,20 +138,10 @@ func (r *EnvDrift) Settings() map[string]*string {
return out
}
-func (r *EnvDrift) Admin() map[string]string {
+func (r *EnvDrift) Values(kind ChangeKind) map[string]string {
out := map[string]string{}
for _, c := range r.Changes {
- if c.Kind == ChangeAdmin {
- out[c.Key] = c.Value
- }
- }
- return out
-}
-
-func (r *EnvDrift) Secrets() map[string]string {
- out := map[string]string{}
- for _, c := range r.Changes {
- if c.Kind == ChangeSecret {
+ if c.Kind == kind {
out[c.Key] = c.Value
}
}
diff --git a/fluxer_rexctl/internal/ui/ui.go b/fluxer_rexctl/internal/ui/ui.go
index 3c310c933..93b51d5ab 100644
--- a/fluxer_rexctl/internal/ui/ui.go
+++ b/fluxer_rexctl/internal/ui/ui.go
@@ -361,3 +361,14 @@ func QuietTerminal() func() {
_ = r.Close()
}
}
+
+func JoinAnd(items []string) string { return joinLast(items, " and ") }
+
+func JoinOr(items []string) string { return joinLast(items, " or ") }
+
+func joinLast(items []string, last string) string {
+ if len(items) < 2 {
+ return strings.Join(items, "")
+ }
+ return strings.Join(items[:len(items)-1], ", ") + last + items[len(items)-1]
+}