From 9c7f1149bae1c08dcea98d5f675d8505ee2a9187 Mon Sep 17 00:00:00 2001 From: kokopi-dev Date: Mon, 6 Apr 2026 02:38:37 +0900 Subject: [PATCH] add:partial functionality --- internal/pages/file_action.go | 13 ++ internal/pages/server_actions.go | 13 ++ internal/services/commands.go | 53 ++++++++ internal/services/services_store.go | 10 ++ internal/services/storage.go | 52 ++++++++ internal/styles/styles.go | 37 ++++++ internal/tui/tui.go | 39 ++++-- internal/tui/update.go | 194 +++++++++++++++++++++++----- internal/tui/view.go | 71 +++++++++- main.go | 9 ++ 10 files changed, 444 insertions(+), 47 deletions(-) create mode 100644 internal/pages/file_action.go create mode 100644 internal/pages/server_actions.go create mode 100644 internal/services/commands.go create mode 100644 internal/services/storage.go diff --git a/internal/pages/file_action.go b/internal/pages/file_action.go new file mode 100644 index 0000000..a53f6e0 --- /dev/null +++ b/internal/pages/file_action.go @@ -0,0 +1,13 @@ +package pages + +type FileActionPageMsg struct { + ServerName string + Filename string +} + +func FileActionItems() []MenuItem { + return []MenuItem{ + {Label: "Get", Key: "get"}, + {Label: "Delete", Key: "delete"}, + } +} diff --git a/internal/pages/server_actions.go b/internal/pages/server_actions.go new file mode 100644 index 0000000..051fd07 --- /dev/null +++ b/internal/pages/server_actions.go @@ -0,0 +1,13 @@ +package pages + +type ServerActionsPageMsg struct { + ServerName string +} + +func ServerActionItems() []MenuItem { + return []MenuItem{ + {Label: "Send", Key: "send"}, + {Label: "Get", Key: "get"}, + {Label: "Clean", Key: "clean"}, + } +} diff --git a/internal/services/commands.go b/internal/services/commands.go new file mode 100644 index 0000000..a4851a1 --- /dev/null +++ b/internal/services/commands.go @@ -0,0 +1,53 @@ +package services + +import ( + "os/exec" +) + +const defaultPort = "22" +const defaultStoragePath = "~/.filepass_storage" + +func serverPort(s Server) string { + if s.Port == "" { + return defaultPort + } + return s.Port +} + +// SSHCmd returns an exec.Cmd for running a single command on the server. +func SSHCmd(s Server, remoteCmd string) *exec.Cmd { + return exec.Command( + "ssh", + "-i", s.PrivateKey, + "-p", serverPort(s), + "-o", "StrictHostKeyChecking=no", + "-o", "BatchMode=yes", + s.User+"@"+s.Host, + remoteCmd, + ) +} + +// RsyncCmd returns an exec.Cmd for an rsync transfer. +// src and dst follow standard rsync syntax (local path or user@host:path). +func RsyncCmd(s Server, src, dst string) *exec.Cmd { + sshFlag := "ssh -i " + s.PrivateKey + " -p " + serverPort(s) + + " -o StrictHostKeyChecking=no -o BatchMode=yes" + return exec.Command( + "rsync", + "-avz", + "--partial", + "-e", sshFlag, + src, + dst, + ) +} + +// RemotePath returns the full remote path for a filename inside storage. +func RemotePath(s Server, filename string) string { + return s.User + "@" + s.Host + ":" + defaultStoragePath + "/" + filename +} + +// RemoteStorageRoot returns the remote storage root for rsync operations. +func RemoteStorageRoot(s Server) string { + return s.User + "@" + s.Host + ":" + defaultStoragePath + "/" +} diff --git a/internal/services/services_store.go b/internal/services/services_store.go index 01990a4..a9c2eaf 100644 --- a/internal/services/services_store.go +++ b/internal/services/services_store.go @@ -1,5 +1,7 @@ package services +import "fmt" + type ServicesStore struct { Config *ConfigService } @@ -11,3 +13,11 @@ func NewServicesStore() (*ServicesStore, error) { } return &ServicesStore{Config: cfg}, nil } + +func (s *ServicesStore) NewStorageService(serverName string) (*StorageService, error) { + srv, ok := s.Config.servers[serverName] + if !ok { + return nil, fmt.Errorf("server %q not found", serverName) + } + return NewStorageService(srv), nil +} diff --git a/internal/services/storage.go b/internal/services/storage.go new file mode 100644 index 0000000..989f596 --- /dev/null +++ b/internal/services/storage.go @@ -0,0 +1,52 @@ +package services + +import ( + "fmt" + "strings" +) + +// StorageService executes file operations against a single server's storage. +type StorageService struct { + server Server +} + +func NewStorageService(s Server) *StorageService { + return &StorageService{server: s} +} + +// Check returns the list of files currently in the remote storage directory. +func (s *StorageService) Check() ([]string, error) { + cmd := SSHCmd(s.server, + "find "+defaultStoragePath+" -type f -printf '%f\n' 2>/dev/null", + ) + out, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("check failed: %w", err) + } + raw := strings.TrimSpace(string(out)) + if raw == "" { + return []string{}, nil + } + return strings.Split(raw, "\n"), nil +} + +// Send transfers one or more local files to the remote storage. +// Multiple files are archived into a temp tarball first. +func (s *StorageService) Send(localPaths []string) error { + // TODO: implement + return fmt.Errorf("send: not yet implemented") +} + +// Get downloads one or more files from remote storage to destDir. +// Multiple files are archived server-side, transferred, then extracted. +func (s *StorageService) Get(remoteFiles []string, destDir string) error { + // TODO: implement + return fmt.Errorf("get: not yet implemented") +} + +// Clean removes specific files from remote storage. +// Pass a nil or empty slice to remove all files. +func (s *StorageService) Clean(remoteFiles []string) error { + // TODO: implement + return fmt.Errorf("clean: not yet implemented") +} diff --git a/internal/styles/styles.go b/internal/styles/styles.go index f28b963..0e78e90 100644 --- a/internal/styles/styles.go +++ b/internal/styles/styles.go @@ -117,6 +117,35 @@ var ( serverRowNameActiveStyle = lipgloss.NewStyle(). Bold(true). Foreground(lipgloss.Color("75")) + + // Storage file list + StorageFileSectionStyle = lipgloss.NewStyle(). + BorderTop(true). + BorderStyle(lipgloss.NormalBorder()). + BorderForeground(lipgloss.Color("237")). + MarginTop(1). + PaddingTop(1). + Width(44) + + StorageEmptyStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("243")). + Italic(true) + + fileItemInactive = lipgloss.NewStyle(). + PaddingLeft(4). + Foreground(lipgloss.Color("252")). + Width(44) + + fileItemActive = lipgloss.NewStyle(). + PaddingLeft(2). + Foreground(lipgloss.Color("75")). + Bold(true). + Width(44). + SetString("▸ ") + + FilenameLabelStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("243")). + MarginBottom(1) ) func MenuItemStyle(active, disabled bool) lipgloss.Style { @@ -150,6 +179,14 @@ func ButtonStyle(focused, enabled bool) lipgloss.Style { } } +// FileItemStyle returns the style for a file list row. +func FileItemStyle(active bool) lipgloss.Style { + if active { + return fileItemActive + } + return fileItemInactive +} + // ServerRowStyle renders a single-line server list entry showing only the server name. func ServerRowStyle(active bool, name string) string { if active { diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 811d7dd..4fbb5d9 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -12,23 +12,34 @@ const ( pageConfig pageAddServer pageSelectServer + pageServerActions + pageFileAction ) type TUIInterface struct { - Services *services.ServicesStore - Page page - MenuItems []pages.MenuItem - Selected int - Servers map[string]services.Server - ServerNames []string // sorted, stable order for list rendering - NoServers bool - InitErr error - FlashMsg string - Form addServerForm - FormErr string // inline field error (e.g. duplicate name) - Quitting bool - WindowWidth int - WindowHeight int + Services *services.ServicesStore + Page page + MenuItems []pages.MenuItem + Selected int + Servers map[string]services.Server + ServerNames []string // sorted, stable order for list rendering + NoServers bool + InitErr error + FlashMsg string + Form addServerForm + FormErr string // inline field error (e.g. duplicate name) + Quitting bool + WindowWidth int + WindowHeight int + // server actions page + ActiveServer string + StorageFiles []string + StorageLoading bool + StorageErr error + FileSelected int // cursor within StorageFiles + FileFocused bool // true = ↑↓ drives file list, false = action menu + // file action page + ActiveFile string } func NewTUIInterface(store *services.ServicesStore) TUIInterface { diff --git a/internal/tui/update.go b/internal/tui/update.go index f699fec..b399933 100644 --- a/internal/tui/update.go +++ b/internal/tui/update.go @@ -23,6 +23,22 @@ type serverAddedMsg struct { type clearFlashMsg struct{} +type storageFilesMsg struct { + files []string + err error +} + +func checkStorageCmd(store *services.ServicesStore, serverName string) tea.Cmd { + return func() tea.Msg { + storage, err := store.NewStorageService(serverName) + if err != nil { + return storageFilesMsg{err: err} + } + files, err := storage.Check() + return storageFilesMsg{files: files, err: err} + } +} + func clearFlashAfter(d time.Duration) tea.Cmd { return tea.Tick(d, func(time.Time) tea.Msg { return clearFlashMsg{} @@ -82,6 +98,32 @@ func (m TUIInterface) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.Selected = 0 return m, nil + case pages.ServerActionsPageMsg: + m.Page = pageServerActions + m.ActiveServer = msg.ServerName + m.MenuItems = pages.ServerActionItems() + m.Selected = 0 + m.FileSelected = 0 + m.FileFocused = false + m.StorageFiles = nil + m.StorageErr = nil + m.StorageLoading = true + return m, checkStorageCmd(m.Services, msg.ServerName) + + case pages.FileActionPageMsg: + m.Page = pageFileAction + m.ActiveFile = msg.Filename + m.MenuItems = pages.FileActionItems() + m.Selected = 0 + return m, nil + + case storageFilesMsg: + m.StorageLoading = false + m.StorageFiles = msg.files + m.StorageErr = msg.err + m.FileSelected = 0 + return m, nil + case configLoadedMsg: if msg.err != nil { m.InitErr = msg.err @@ -112,14 +154,18 @@ func (m TUIInterface) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil case tea.KeyPressMsg: - // add server form has its own key handling if m.Page == pageAddServer { return m.updateAddServer(msg) } - // server list has its own key handling if m.Page == pageSelectServer { return m.updateSelectServer(msg) } + if m.Page == pageServerActions { + return m.updateServerActions(msg) + } + if m.Page == pageFileAction { + return m.updateFileAction(msg) + } switch msg.String() { case "up", "k": @@ -169,6 +215,122 @@ func (m TUIInterface) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } +func (m TUIInterface) updateServerActions(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { + switch msg.String() { + case "tab": + // toggle focus between action menu and file list + if len(m.StorageFiles) > 0 { + m.FileFocused = !m.FileFocused + } + + case "up", "k": + if m.FileFocused { + if m.FileSelected > 0 { + m.FileSelected-- + } + } else { + if m.Selected > 0 { + m.Selected-- + } + } + + case "down", "j": + if m.FileFocused { + if m.FileSelected < len(m.StorageFiles)-1 { + m.FileSelected++ + } + } else { + if m.Selected < len(m.MenuItems)-1 { + m.Selected++ + } + } + + case "enter": + if m.FileFocused && len(m.StorageFiles) > 0 { + file := m.StorageFiles[m.FileSelected] + server := m.ActiveServer + return m, func() tea.Msg { + return pages.FileActionPageMsg{ServerName: server, Filename: file} + } + } + switch m.MenuItems[m.Selected].Key { + case "send": + // TODO: navigate to send page + case "get": + // TODO: navigate to get page (bulk) + case "clean": + // TODO: navigate to clean page (bulk) + } + + case "ctrl+c": + m.Quitting = true + return m, tea.Quit + + case "esc": + return m, func() tea.Msg { return pages.SelectServerPageMsg{} } + } + + return m, nil +} + +func (m TUIInterface) updateFileAction(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { + switch msg.String() { + case "up", "k": + if m.Selected > 0 { + m.Selected-- + } + case "down", "j": + if m.Selected < len(m.MenuItems)-1 { + m.Selected++ + } + case "enter": + server := m.ActiveServer + file := m.ActiveFile + switch m.MenuItems[m.Selected].Key { + case "get": + _ = server + _ = file + // TODO: implement get + case "delete": + _ = server + _ = file + // TODO: implement delete + } + case "ctrl+c": + m.Quitting = true + return m, tea.Quit + case "esc": + server := m.ActiveServer + return m, func() tea.Msg { return pages.ServerActionsPageMsg{ServerName: server} } + } + return m, nil +} + +func (m TUIInterface) updateSelectServer(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { + last := len(m.ServerNames) - 1 + switch msg.String() { + case "up", "k": + if m.Selected > 0 { + m.Selected-- + } + case "down", "j": + if m.Selected < last { + m.Selected++ + } + case "enter": + if m.Selected >= 0 && m.Selected < len(m.ServerNames) { + name := m.ServerNames[m.Selected] + return m, func() tea.Msg { return pages.ServerActionsPageMsg{ServerName: name} } + } + case "ctrl+c": + m.Quitting = true + return m, tea.Quit + case "esc": + return m, func() tea.Msg { return pages.HomePageMsg{} } + } + return m, nil +} + func (m TUIInterface) updateAddServer(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { f := m.Form @@ -182,16 +344,13 @@ func (m TUIInterface) updateAddServer(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) return m, nil case "enter": - // on an input field, advance to next if f.focused < fieldSave { m.Form = f.focusNext() return m, nil } - // on save button if f.focused == fieldSave { return m.submitAddServer() } - // on back button if f.focused == fieldBack { return m, func() tea.Msg { return pages.ConfigPageMsg{} } } @@ -201,7 +360,6 @@ func (m TUIInterface) updateAddServer(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) return m, tea.Quit case "ctrl+v": - // OSC52 clipboard read; result arrives as tea.ClipboardMsg if f.focused < len(f.inputs) { return m, tea.ReadClipboard } @@ -211,11 +369,9 @@ func (m TUIInterface) updateAddServer(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) return m, func() tea.Msg { return pages.ConfigPageMsg{} } } - // route keystrokes to the focused input if f.focused < len(f.inputs) { var cmd tea.Cmd f.inputs[f.focused], cmd = f.inputs[f.focused].Update(msg) - // clear duplicate-name error when user edits the name field if f.focused == fieldName { m.FormErr = "" } @@ -240,28 +396,6 @@ func (m TUIInterface) updateAddServerPaste(text string) (tea.Model, tea.Cmd) { return m, cmd } -func (m TUIInterface) updateSelectServer(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { - last := len(m.ServerNames) - 1 - switch msg.String() { - case "up", "k": - if m.Selected > 0 { - m.Selected-- - } - case "down", "j": - if m.Selected < last { - m.Selected++ - } - case "enter": - // TODO: connect to selected server - case "ctrl+c": - m.Quitting = true - return m, tea.Quit - case "esc": - return m, func() tea.Msg { return pages.HomePageMsg{} } - } - return m, nil -} - func (m TUIInterface) submitAddServer() (tea.Model, tea.Cmd) { f := m.Form name := strings.TrimSpace(f.inputs[fieldName].Value()) diff --git a/internal/tui/view.go b/internal/tui/view.go index f8c483a..55fd784 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -25,6 +25,13 @@ func (m TUIInterface) subtitle() string { return "Add Server" case pageSelectServer: return "Select Server" + case pageServerActions: + if m.ActiveServer != "" { + return m.ActiveServer + } + return "Server" + case pageFileAction: + return m.ActiveFile default: return "Secure file transfer" } @@ -50,6 +57,10 @@ func (m TUIInterface) View() tea.View { body = m.viewAddServer() case pageSelectServer: body = m.viewSelectServer() + case pageServerActions: + body = m.viewServerActions() + case pageFileAction: + body = m.viewFileAction() default: body = m.viewMenu() } @@ -79,6 +90,20 @@ func (m TUIInterface) View() tea.View { footerHint("enter", "connect") + footerSep() + footerHint("esc", "back") + case pageServerActions: + footerStr = footerHint("tab", "switch pane") + + footerSep() + + footerHint("↑↓", "navigate") + + footerSep() + + footerHint("enter", "select") + + footerSep() + + footerHint("esc", "back") + case pageFileAction: + footerStr = footerHint("↑↓", "navigate") + + footerSep() + + footerHint("enter", "confirm") + + footerSep() + + footerHint("esc", "back") default: footerStr = footerHint("↑↓", "navigate") + footerSep() + @@ -130,6 +155,49 @@ func (m TUIInterface) viewMenu() string { return menu } +func (m TUIInterface) viewServerActions() string { + // action menu — single column, unfocused when file pane is active + var actionRows []string + for i, item := range m.MenuItems { + active := !m.FileFocused && i == m.Selected + actionRows = append(actionRows, styles.MenuItemStyle(active, false).Render(item.Label)) + } + actions := lipgloss.JoinVertical(lipgloss.Left, actionRows...) + + // file list section below, separated by a top border + var fileRows []string + switch { + case m.StorageLoading: + fileRows = append(fileRows, styles.StatusWarnStyle.Render(" loading…")) + case m.StorageErr != nil: + fileRows = append(fileRows, styles.StatusErrStyle.Render("✗ "+m.StorageErr.Error())) + case len(m.StorageFiles) == 0: + fileRows = append(fileRows, styles.StorageEmptyStyle.Render(" no files in storage")) + default: + for i, f := range m.StorageFiles { + active := m.FileFocused && i == m.FileSelected + fileRows = append(fileRows, styles.FileItemStyle(active).Render(f)) + } + } + fileList := lipgloss.JoinVertical(lipgloss.Left, fileRows...) + fileSection := styles.StorageFileSectionStyle.Render(fileList) + + return lipgloss.JoinVertical(lipgloss.Left, actions, fileSection) +} + +func (m TUIInterface) viewFileAction() string { + // filename shown as a dim label above the menu + filenameLabel := styles.FilenameLabelStyle.Render(m.ActiveFile) + + var menuRows []string + for i, item := range m.MenuItems { + menuRows = append(menuRows, styles.MenuItemStyle(i == m.Selected, false).Render(item.Label)) + } + menu := lipgloss.JoinVertical(lipgloss.Left, menuRows...) + + return lipgloss.JoinVertical(lipgloss.Left, filenameLabel, menu) +} + func (m TUIInterface) viewSelectServer() string { if len(m.ServerNames) == 0 { return styles.StatusWarnStyle.Render("⚠ No servers configured.") @@ -155,16 +223,13 @@ func (m TUIInterface) viewAddServer() string { } form := lipgloss.JoinVertical(lipgloss.Left, rows...) - // required legend legend := styles.FieldLegendStyle.Render("* required") - // form error (duplicate name, etc.) var errLine string if m.FormErr != "" { errLine = styles.StatusErrStyle.Render(m.FormErr) } - // save / back buttons saveBtn := styles.ButtonStyle(f.focused == fieldSave, f.canSave()).Render("Save") backBtn := styles.ButtonStyle(f.focused == fieldBack, true).Render("Back") buttons := lipgloss.JoinHorizontal(lipgloss.Top, saveBtn, " ", backBtn) diff --git a/main.go b/main.go index b5cddad..2721344 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "os/exec" "filepass/internal/services" "filepass/internal/tui" @@ -11,6 +12,14 @@ import ( ) func main() { + if _, err := exec.LookPath("rsync"); err != nil { + fmt.Fprintln(os.Stderr, "error: rsync is required but was not found in PATH") + fmt.Fprintln(os.Stderr, "install it with your package manager, e.g.:") + fmt.Fprintln(os.Stderr, " brew install rsync") + fmt.Fprintln(os.Stderr, " apt install rsync") + os.Exit(1) + } + store, err := services.NewServicesStore() if err != nil { fmt.Fprintln(os.Stderr, "failed to initialise config:", err)