先日、Pythonで書かれたpfのTUI版をGoで書き換えてもらいました。
Python版では機能を追加したら他の部分がデグレするということがよく起きていたので、Goで書き換える時には、実装すべき機能を説明したfeatures.mdと、実装にむけたタスクをチェックリスト化したtodo.mdを作ってもらっていました。
この二つを用意することで、デグレが起きにくくなった感覚があります。
これらも完成させるため、最後に「features.mdとtodo.mdを参考にスクラッチから再実装できる」内容がカバーされているかチェックしてもらいました。
そうして出来上がったのがこちら
# pf-tui Go Features
This document outlines the features and user interface of the `pf-tui` Go implementation.
## Project Philosophy
This is a small project and I'd like to keep the files and structure as simple and flat as possible.
## Global Hotkeys
- **`Esc`**: In most screens, this key cancels the current operation (e.g., editing a rule, browsing files) and returns to the previous screen or main menu. In a text input field, it cancels the edit. From the main menu, it will show a confirmation dialog to exit the application.
- **`q`**: From the main menu or informational screens, this key will show a confirmation dialog to quit the application.
## Main Screen
The initial screen provides a central menu for all major operations.
- **Status Display:** Shows the current status of the PF firewall (Enabled/Disabled) and whether it's enabled on startup. This is displayed at the top of the screen.
- **Navigation:** Use arrow keys to navigate the menu. Navigation is circular, meaning pressing up from the top item goes to the bottom, and pressing down from the bottom item goes to the top.
### Menu Structure
The menu is organized into logical groups separated by lines:
- **Rule Management**
- Edit Firewall Rule
- Add New Firewall Rule
- Edit Port Forwarding Rule
- Add Port Forwarding Rule
- **Configuration**
- Save & Apply Configuration
- Export Configuration
- Import Configuration
- **Live PF Information & Control**
- Show Current Rules
- Show Info
- Enable PF
- Disable PF
- Enable PF on Startup
- Disable PF on Startup
- **Application**
- Exit
## Firewall Rule Screens
<以降省略>
ただし、一旦このように文章化されてしまったtodo.mdは
# `pf-tui-go` Implementation Summary
This document summarizes the implementation of the `pf-tui-go` application, which was built based on the specifications in `features.md`. The project is feature-complete.
## Phase 1: Project Setup & Core Structures
The project was initialized with Go modules and dependencies (`bubbletea`, `bubbles`, `lipgloss`). Core data structures for firewall rules, port forwarding rules, and the main configuration were defined in `firewall.go`, along with a `FirewallManager` to handle loading and saving the configuration from JSON.
## Phase 2: Backend `pfctl` Integration
A wrapper in `pf.go` was created to handle all interactions with the `pfctl` command-line tool. This includes functions for applying rules, checking status, and enabling/disabling the firewall. A pre-flight check for `sudo` credentials was implemented to ensure smooth TUI operation.
<以降省略>
チェックリスト形式に戻してもらいました。
バイブコーディング中にどこまで実装されたのかを把握して、必要に応じて途中から作業を再会してもらう時に、チェックリスト形式の方が都合がいいんですよね。
# `pf-tui-go` Implementation Plan
This document outlines the tasks required to implement the features described in `features.md` using Go and the `bubbletea` framework.
**Project Philosophy Note:** As per `features.md`, this project will maintain a simple and flat file structure. TUI components will be consolidated into a minimal number of files (e.g., a single `tui.go`) rather than being split into many separate files.
## Phase 1: Project Setup & Core Structures
- [x] **Initialize Project:**
- [x] Set up `go.mod`.
- [x] Add dependencies: `bubbletea`, `bubbles`, `lipgloss`.
- [x] **Define Core Data Structures (`firewall.go`):**
- [x] Create `FirewallRule` struct to represent a single filter rule.
- [x] Create `PortForwardingRule` struct for RDR rules.
- [x] Create a `Config` struct to hold both `FirewallRule` and `PortForwardingRule` slices.
- [x] **Firewall Manager (`firewall.go`):**
- [x] Create `FirewallManager` struct.
- [x] Implement `LoadConfig(path string)` to read rules from JSON.
- [x] Implement `SaveConfig(path string)` to write rules to JSON.
- [x] Implement `GeneratePfConf()` to create the `pf.conf` string from the current rules.
## Phase 2: Backend `pfctl` Integration
- [x] **Implement `pfctl` Commands (`pf.go`):**
- [x] `ApplyRules(rules string)`: Apply rules from a string.
- [x] `GetCurrentRules()`: Get currently loaded rules.
- [x] `GetPfStatus()`: Check if PF is enabled or disabled.
- [x] `EnablePf()`: Enable the firewall.
- [x] `DisablePf()`: Disable the firewall.
- [x] `GetPfInfo()`: Get detailed statistics.
- [x] `EnablePfOnStartup()`: Configure `launchd` to enable PF on boot.
- [x] `DisablePfOnStartup()`: Remove the `launchd` configuration.
- [x] **Sudo Handling:**
- [x] Implement a pre-flight check to validate `sudo` credentials.
- [x] If a password is required, temporarily pause the TUI and prompt for the password in the standard terminal.
<以降省略>
Geminiはこれがあればスクラッチから再実装できるとも答えているし、中々よいアプローチなのかも。
しばらくこの方法を試してみようと思います。
Markdownやコードの最新版はこちら
https://github.com/kh813/pf-tui-go