diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index e8425b4..fb37708 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -168,29 +168,27 @@ func (d *Deployer) Sources() []*Source { return out } -// Deploy builds every environment, or only those named by env or branch; a named deploy skips purge and GC. func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) error { ctx, end := d.phase(ctx, "run") + want := map[string]bool{} + for _, e := range only { + want[e] = true + } d.mu.Lock() clear(d.skips) clear(d.counts) d.mu.Unlock() - found := map[string]bool{} var errs []error for _, s := range d.Sources() { - if err := d.deploySource(ctx, s, only, found, modules); err != nil { + if err := d.deploySource(ctx, s, want, modules); err != nil { errs = append(errs, fmt.Errorf("source %s: %w", s.Name, err)) } } - for _, e := range only { - if !found[e] { - errs = append(errs, fmt.Errorf("environment %s not found", e)) - } + for e := range want { + errs = append(errs, fmt.Errorf("environment %s not found", e)) } - if len(only) == 0 { - if err := d.GC(ctx); err != nil { - errs = append(errs, err) - } + if err := d.GC(ctx); err != nil { + errs = append(errs, err) } d.mu.Lock() for _, e := range slices.Sorted(maps.Keys(d.skips)) { @@ -211,17 +209,7 @@ func (d *Deployer) count(outcome string) { d.mu.Unlock() } -// Matches reports whether arg names env, either directly or as a branch that sanitises to it. -func (s *Source) Matches(arg, env string) bool { - n, _, err := s.EnvName(arg) - return arg == env || err == nil && n == env -} - -// Environments maps env name -> sha for every branch, or only the branches only names. -func (d *Deployer) Environments(ctx context.Context, s *Source, only ...string) (map[string]string, []error) { - wanted := func(branch, name string) bool { - return len(only) == 0 || slices.ContainsFunc(only, func(a string) bool { return a == branch || name != "" && s.Matches(a, name) }) - } +func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]string, []error) { all, err := d.branches(ctx, d.srcMirror(s)) if err != nil { return nil, []error{err} @@ -238,9 +226,6 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, only ...string) continue } name, corrected, err := s.EnvName(b) - if !wanted(b, name) { - continue - } if err != nil { errs = append(errs, err) continue @@ -257,7 +242,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, only ...string) return envs, errs } -func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, found map[string]bool, modules bool) error { +func (d *Deployer) deploySource(ctx context.Context, s *Source, want map[string]bool, modules bool) error { fctx, end := d.phase(ctx, "source.fetch", "source", s.Name) err := d.syncMirror(fctx, d.srcMirror(s), s.Remote, envRefspecs...) end() @@ -265,24 +250,22 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, f return err } bctx, end := d.phase(ctx, "source.branches", "source", s.Name) - envs, errs := d.Environments(bctx, s, only...) + envs, errs := d.Environments(bctx, s) end("envs", len(envs)) if envs == nil { return errors.Join(errs...) } - for name := range envs { - for _, a := range only { - if s.Matches(a, name) { - found[a] = true - } - } - } + full := len(want) == 0 if err := os.MkdirAll(s.Basedir, 0o755); err != nil { return err } var mu sync.Mutex var wg sync.WaitGroup for name, sha := range envs { + if !full && !want[name] { + continue + } + delete(want, name) wg.Add(1) go func() { defer wg.Done() @@ -294,7 +277,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, f }() } wg.Wait() - if len(only) == 0 { + if full { errs = append(errs, d.purge(s, envs)) } return errors.Join(errs...) diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index 820495a..c7e03dc 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -772,42 +772,6 @@ func TestGCKeepsModulesOfRemovedSource(t *testing.T) { } } -func TestNamedDeployTouchesOnlyNamed(t *testing.T) { - f := newFixture(t) - commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile(), "manifests/site.pp": "node default {}\n"}) - gitT(t, f.control, "branch", "gone") - commit(t, f.control, map[string]string{"manifests/site.pp": "node default { }\n"}) - gitT(t, f.control, "branch", "benvin/foo-bar") - f.mustDeploy(false) - gone := f.env("gone") - gitT(t, f.control, "branch", "-D", "gone") - f.mustDeploy(false) - f.clock = f.clock.Add(11 * time.Minute) - state := readFileT(t, f.cache(gcStateFile)) - - sha := commit(t, f.control, map[string]string{"manifests/site.pp": "node x {}\n"}) - gitT(t, f.control, "branch", "-f", "benvin/foo-bar", "main") - main := f.env("main") - if err := f.deploy(false, "benvin/foo-bar"); err != nil { - t.Fatal(err) - } - if !strings.HasPrefix(filepath.Base(f.env("benvin_foo_bar")), sha+"-") { - t.Fatal("a raw branch name deploys its sanitised env") - } - if f.env("main") != main || !exists(gone) || readFileT(t, f.cache(gcStateFile)) != state { - t.Fatal("a named deploy must leave other envs, their worktrees and GC state untouched") - } - if err := f.deploy(false, "main"); err != nil || !strings.HasPrefix(filepath.Base(f.env("main")), sha+"-") { - t.Fatalf("env name deploys: %v", err) - } - if err := f.deploy(false, "main", "nope"); err == nil || !strings.Contains(err.Error(), "nope not found") { - t.Fatalf("unknown env must error: %v", err) - } - if !exists(gone) { - t.Fatal("named deploys never GC") - } -} - func TestTimingsPhases(t *testing.T) { f := newFixture(t) f.cfg.W10k.GenerateTypes = true diff --git a/main.go b/main.go index 6efec92..18f7850 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,10 @@ import ( "context" "fmt" "log/slog" - "maps" "os" "os/signal" "path/filepath" - "slices" + "sort" "strings" "syscall" "time" @@ -51,24 +50,6 @@ func rootCmd() *cobra.Command { } return d, nil } - live := func() ([][3]string, error) { - d, err := load() - if err != nil { - return nil, err - } - var rows [][3]string - for _, s := range d.Sources() { - live, err := d.Live(s) - if err != nil { - return nil, err - } - for _, e := range slices.Sorted(maps.Keys(live)) { - sha, _, _ := strings.Cut(filepath.Base(live[e]), "-") - rows = append(rows, [3]string{s.Name, e, sha}) - } - } - return rows, nil - } locked := func(fn func(*deploy.Deployer) error) error { d, err := load() if err != nil { @@ -96,20 +77,10 @@ func rootCmd() *cobra.Command { var modules bool deployCmd := &cobra.Command{ Use: "deploy [env...]", - Short: "Deploy all environments, or only the named ones (env or branch names)", + Short: "Deploy all environments, or only the named ones", RunE: func(cmd *cobra.Command, args []string) error { return locked(func(d *deploy.Deployer) error { return d.Deploy(cmd.Context(), args, modules) }) }, - ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { - rows, _ := live() - var envs []string - for _, r := range rows { - if !slices.Contains(args, r[1]) && !slices.Contains(envs, r[1]) { - envs = append(envs, r[1]) - } - } - return envs, cobra.ShellCompDirectiveNoFileComp - }, } deployCmd.Flags().BoolVarP(&modules, "modules", "m", false, "re-resolve Puppetfile modules for unchanged environments") @@ -118,12 +89,24 @@ func rootCmd() *cobra.Command { Short: "List deployed environments", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { - rows, err := live() + d, err := load() if err != nil { return err } - for _, r := range rows { - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\n", r[0], r[1], r[2]) + for _, s := range d.Sources() { + live, err := d.Live(s) + if err != nil { + return err + } + envs := make([]string, 0, len(live)) + for e := range live { + envs = append(envs, e) + } + sort.Strings(envs) + for _, e := range envs { + sha, _, _ := strings.Cut(filepath.Base(live[e]), "-") + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\n", s.Name, e, sha) + } } return nil },