vcom/internal/tui/archive_create.go
vrubelroman b5bad66e36 feat(tui): archives as read-only mounts, create and extract via jobs (6.5)
Enter on an archive indexes it in a job (progress, cancel; Update never
waits: U-P1-9) and pushes an archivefs mount; ".." from its root returns
with the cursor on the archive. Nothing is unpacked to a temp directory
and every mutating action is refused inside (U-P0-2). a: format list →
name proposed by archive.Name → archive.Create in a job (refuses to write
over or into a source: U-P0-1). e/F11 extracts the archive under the
cursor into the other panel (local or SSH: N-P1-15) through the same
plan → execute flow as copy, so existing files are conflicts (U-P1-11);
inside an archive it copies the marked members out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 14:01:45 +03:00

87 lines
2.8 KiB
Go

package tui
import (
"context"
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/archive"
"vcom/internal/jobs"
"vcom/internal/transfer"
"vcom/internal/tui/modal"
"vcom/internal/tui/panel"
)
func (a *App) canCreateArchive() error {
p := a.Active()
switch {
case p.Source != panel.SourceDir:
return errors.New("nothing to archive here")
case p.FS().Caps().ReadOnly:
return errors.New("this panel is read-only (archive)")
}
if es, _ := p.Targets(); len(es) == 0 {
return errors.New("nothing selected")
}
return nil
}
// createArchive is a: format list → name (proposed by archive.Name) → a
// job writing the archive into this panel's directory through
// archive.Create (temp + rename; never over or into a source: U-P0-1).
func (a *App) createArchive() tea.Cmd {
p := a.Active()
paths, names, _ := a.opTargets()
here := p.Here()
items := []modal.Item{
{Label: "zip", Value: archive.Zip},
{Label: "tar.gz", Value: archive.TarGz},
{Label: "tar", Value: archive.Tar},
}
a.openModal(modal.NewList("Archive format", items, nil), func(o modal.Outcome) tea.Cmd {
if o.Cancelled {
return nil
}
f := o.Value.(archive.Format)
in := modal.NewInput("Create archive", modal.Field{Label: "Name", Value: archive.Name(names, f)})
in.Text = fmt.Sprintf("%s → %s", describe(names), displayPath(here.Location()))
in.Validate = func(v []string) error { return validName(v[0]) }
a.openModal(in, func(o modal.Outcome) tea.Cmd {
if o.Cancelled {
return nil
}
name := o.Value.([]string)[0]
fsys := here.FS()
target := fsys.Paths().Join(here.Path, name)
fn := func(ctx context.Context, h *jobs.Handle) (any, error) {
rep := archive.Create(ctx, fsys, paths, fsys, target, f, transferHooks(h))
if rep.Cancelled {
return rep, context.Canceled
}
return rep, rep.Err
}
return a.startJob("archive", "Archive "+name, fn, nil, holdMounts(here)...)
})
return nil
})
return nil
}
// unpackJob indexes the archive, then runs the same plan → execute flow as
// copy, with the archive as the source (one pass over it).
func unpackJob(src panel.Target, archivePath string, to panel.Target, opts transfer.Options) jobs.Func {
return func(ctx context.Context, h *jobs.Handle) (any, error) {
h.SetProgress(jobs.Progress{Phase: "reading the archive index", Current: archivePath})
idx, err := archive.IndexFile(ctx, src.FS(), archivePath, func(n int) {
h.SetProgress(jobs.Progress{Phase: "reading the archive index", Current: archivePath, FilesDone: n})
})
if err != nil {
return transfer.Report{Err: err}, err
}
req := transfer.Request{Op: transfer.Copy, From: archive.NewSource(src.FS(), archivePath, idx, nil),
To: to.FS(), ToDir: to.Path, Opts: opts}
return transferJob(req, "Extract", 0)(ctx, h)
}
}