Skip to main content

fsmon/common/
help.rs

1use crate::{green, yellow};
2
3/// Help topic for fsmon commands.
4pub enum HelpTopic {
5    Root,
6    Daemon,
7    Init,
8    Cd,
9    Add,
10    Remove,
11    Monitored,
12    Query,
13    Clean,
14    Changes,
15    Health,
16}
17
18impl std::fmt::Debug for HelpTopic {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            HelpTopic::Root => write!(f, "Root"),
22            HelpTopic::Daemon => write!(f, "Daemon"),
23            HelpTopic::Init => write!(f, "Init"),
24            HelpTopic::Cd => write!(f, "Cd"),
25            HelpTopic::Add => write!(f, "Add"),
26            HelpTopic::Remove => write!(f, "Remove"),
27            HelpTopic::Monitored => write!(f, "Monitored"),
28            HelpTopic::Query => write!(f, "Query"),
29            HelpTopic::Clean => write!(f, "Clean"),
30            HelpTopic::Changes => write!(f, "Changes"),
31            HelpTopic::Health => write!(f, "Health"),
32        }
33    }
34}
35
36/// Get short description for a help topic.
37pub const fn about(topic: HelpTopic) -> &'static str {
38    match topic {
39        HelpTopic::Root => {
40            concat!(
41                "Lightweight high-performance file change tracking tool.\n\n",
42                yellow!("Note:"),
43                " If installed via 'cargo install', copy to system path for sudo compatibility:\n",
44                "  ",
45                green!("sudo cp ~/.cargo/bin/fsmon /usr/local/bin/"),
46                "\n\nConfig:  ~/.config/fsmon/fsmon.toml (created by 'fsmon init')",
47                "\nMonitor: ~/.local/share/fsmon/monitored.jsonl",
48                "\nLogs:    ~/.local/state/fsmon/",
49                "\nSocket:  /run/user/<UID>/fsmon/daemon.sock"
50            )
51        }
52        HelpTopic::Daemon => "Run the fsmon daemon (requires sudo for fanotify)",
53        HelpTopic::Init => "Create the config file (directories created on first use)",
54        HelpTopic::Cd => "Open a subshell in the monitored path or log directory",
55        HelpTopic::Add => "Add a path to the monitoring list",
56        HelpTopic::Remove => "Remove one or more paths from the monitoring list",
57        HelpTopic::Monitored => "List all monitored paths with their configuration",
58        HelpTopic::Query => "Query historical file change events from log files",
59        HelpTopic::Clean => "Clean historical log files, retain by time or size",
60        HelpTopic::Changes => "Show the most recent event per path (deduplicated changes)",
61        HelpTopic::Health => "Query daemon health status",
62    }
63}
64
65/// Get detailed description for a help topic.
66pub const fn long_about(topic: HelpTopic) -> &'static str {
67    match topic {
68        HelpTopic::Root => {
69            concat!(
70                "Lightweight high-performance file change tracking tool.\n\n",
71                yellow!("Note:"),
72                " If installed via 'cargo install', copy to system path for sudo compatibility:\n",
73                "  ",
74                green!("sudo cp ~/.cargo/bin/fsmon /usr/local/bin/"),
75                "\n\nConfig:  ~/.config/fsmon/fsmon.toml (created by 'fsmon init')",
76                "\nMonitor: ~/.local/share/fsmon/monitored.jsonl",
77                "\nLogs:    ~/.local/state/fsmon/",
78                "\nSocket:  /run/user/<UID>/fsmon/daemon.sock\n\n",
79                yellow!("Setup (no sudo needed):"),
80                "\n  ",
81                green!("fsmon init"),
82                "                        Create config file\n  ",
83                green!("sudo fsmon init --service"),
84                "         Also install systemd service\n  ",
85                green!("fsmon cd -l"),
86                "                       Open subshell in log directory\n  ",
87                green!("fsmon cd -m"),
88                "                       Open subshell in monitored store directory\n  ",
89                green!("fsmon cd -c"),
90                "                       Open subshell in config directory\n\n",
91                yellow!("Daemon (requires sudo):"),
92                "\n  ",
93                green!("sudo fsmon daemon &"),
94                "               Start daemon in background\n  ",
95                green!("sudo systemctl start fsmon"),
96                "        Start via systemd\n  ",
97                green!("journalctl -u fsmon -f"),
98                "           View daemon logs\n  ",
99                green!("kill %1"),
100                "                           Stop daemon (or Ctrl+C)\n\n",
101                yellow!("Management (no sudo needed):"),
102                "\n  ",
103                green!("fsmon add <cmd> --path /home -r"),
104                "   Track cmd on /home (recursive)\n  ",
105                green!("fsmon add _global --path /home"),
106                "   Monitor /home (all processes)\n  ",
107                green!("fsmon remove <cmd>"),
108                "                Remove cmd group\n  ",
109                green!("fsmon monitored"),
110                "                   List monitored paths\n\n",
111                yellow!("Query (stdout JSONL, pipe to jq):"),
112                "\n  ",
113                green!("fsmon query <cmd> -t '>1h'"),
114                "       Events from last hour\n  ",
115                green!("fsmon query <cmd> | jq '.'"),
116                "       Pretty print events\n\n",
117                yellow!("Clean (config defaults: keep_days=30, size>=1GB):"),
118                "\n  ",
119                green!("fsmon clean <cmd>"),
120                "                 Clean cmd log\n  ",
121                green!("fsmon clean <cmd> -t '>7d'"),
122                "      Keep last 7 days"
123            )
124        }
125        HelpTopic::Daemon => {
126            "Run the fsmon daemon (requires sudo for fanotify)\n\n\
127            Monitors all configured paths via fanotify and logs events.\n\
128            Use 'fsmon add'/'fsmon remove' to manage paths dynamically without restarting.\n\n\
129            Examples:\n\
130              sudo fsmon daemon &                     Start daemon in background\n\
131              sudo fsmon daemon --debug               Enable debug output\n\n\
132            For systemd integration:\n\
133              sudo fsmon init --service             Install systemd service\n\
134              sudo systemctl start fsmon            Start via systemd\n\
135              journalctl -u fsmon -f               View daemon logs\n\n\
136            Config: ~/.config/fsmon/fsmon.toml\n\
137            Logs:   ~/.local/state/fsmon/"
138        }
139        HelpTopic::Init => {
140            "Create the config file (directories created on first use)\n\n\
141            Directories are created on first use:\n\
142              - Monitored dir: by 'fsmon add' on first run\n\
143              - Log dir: by 'fsmon daemon' or 'fsmon cd -l' on first run\n\n\
144            With --service, also installs a systemd service:\n\
145              sudo fsmon init --service"
146        }
147        HelpTopic::Cd => {
148            "Open a subshell in the monitored path or log directory\n\n\
149            Spawns a new shell (using $SHELL, fallback /bin/sh) in the target directory.\n\
150            Type 'exit' to return to the original directory.\n\n\
151            Options:\n\
152              -m, --monitored    cd to the monitored store directory\n\
153              -l, --logging      cd to the log directory\n\
154              -c, --config       cd to the config directory (~/.config/fsmon/)\n\n\
155            Examples:\n\
156              fsmon cd -l                       Open subshell in log directory\n\
157              fsmon cd -m                       Open subshell in monitored store directory\n\
158              fsmon cd -c                       Open subshell in config directory"
159        }
160        HelpTopic::Add => {
161            "Add a path to the monitoring list\n\n\
162            The entry is added immediately if the daemon is running, and persisted\n\
163            in the monitored paths database for automatic monitoring on daemon restart.\n\n\
164            No sudo needed — store is updated immediately.\n\n\
165            <CMD> enables process tree tracking: fork/exec children are auto-included.\n\
166            Use '_global' to monitor all events on a path (no process tracking)."
167        }
168        HelpTopic::Remove => {
169            "Remove one or more paths from the monitoring list\n\n\
170            Without --path, removes the entire cmd group.\n\
171            With --path, removes only the specified paths. Multiple paths are atomic:\n\
172            all must exist, or nothing is removed."
173        }
174        HelpTopic::Monitored => {
175            "List all monitored paths with their configuration\n\n\
176            Displays each path with its recursive flag, event type filters,\n\
177            size threshold, path/cmd exclusion patterns."
178        }
179        HelpTopic::Query => {
180            "Query historical file change events from log files\n\n\
181            Output is JSONL (one JSON object per line), pipe to jq for custom filtering.\n\n\
182            Native fsmon query uses binary search and is significantly faster on large logs."
183        }
184        HelpTopic::Clean => {
185            "Clean historical log files, retain by time or size\n\n\
186            Defaults: keep_days=30, size=>=1GB (from fsmon.toml or code fallback).\n\
187            CLI args override config. Daemon does not auto-clean; use cron/systemd timer."
188        }
189        HelpTopic::Changes => {
190            "Show the most recent event per path (deduplicated changes)\n\n\
191            Same format as 'query', but with duplicate paths collapsed:\n\
192            only the latest event for each unique path is shown, sorted by time descending."
193        }
194        HelpTopic::Health => {
195            "Query daemon health status\n\n\
196            Queries the running daemon's health status via the Unix socket.\n\n\
197            Returns daemon uptime, memory usage, and monitored path count.\n\
198            Requires the daemon to be running."
199        }
200    }
201}