|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "os/signal" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "syscall" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/nxtcoder17/fwatcher/pkg/executor" |
| 17 | + fn "github.com/nxtcoder17/fwatcher/pkg/functions" |
| 18 | + "github.com/nxtcoder17/fwatcher/pkg/logging" |
| 19 | + "github.com/nxtcoder17/fwatcher/pkg/watcher" |
| 20 | + "github.com/urfave/cli/v3" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + ProgramName string |
| 25 | + Version string |
| 26 | +) |
| 27 | + |
| 28 | +// DefaultIgnoreList is list of directories that are mostly ignored |
| 29 | +var DefaultIgnoreList = []string{ |
| 30 | + ".git", ".svn", ".hg", // version control |
| 31 | + ".idea", ".vscode", // IDEs |
| 32 | + ".direnv", // direnv nix |
| 33 | + "node_modules", // node |
| 34 | + ".DS_Store", // macOS |
| 35 | + ".log", // logs |
| 36 | +} |
| 37 | + |
| 38 | +func main() { |
| 39 | + cmd := &cli.Command{ |
| 40 | + Name: ProgramName, |
| 41 | + UseShortOptionHandling: true, |
| 42 | + Usage: "simple tool to run commands on filesystem change events", |
| 43 | + ArgsUsage: "<Command To Run>", |
| 44 | + Version: Version, |
| 45 | + Flags: []cli.Flag{ |
| 46 | + &cli.BoolFlag{ |
| 47 | + Name: "debug", |
| 48 | + }, |
| 49 | + |
| 50 | + &cli.StringFlag{ |
| 51 | + Name: "command", |
| 52 | + Usage: "[command to run]", |
| 53 | + Value: "echo hi", |
| 54 | + Aliases: []string{"c"}, |
| 55 | + }, |
| 56 | + |
| 57 | + &cli.StringSliceFlag{ |
| 58 | + Name: "watch", |
| 59 | + Usage: "[dir] (to watch) | -[dir] (to ignore)", |
| 60 | + Value: []string{"."}, |
| 61 | + Aliases: []string{"w"}, |
| 62 | + }, |
| 63 | + |
| 64 | + &cli.StringSliceFlag{ |
| 65 | + Name: "ext", |
| 66 | + Usage: "[ext] (to watch) | -[ext] (to ignore)", |
| 67 | + Required: false, |
| 68 | + Aliases: []string{"e"}, |
| 69 | + }, |
| 70 | + |
| 71 | + // &cli.StringSliceFlag{ |
| 72 | + // Name: "exclude", |
| 73 | + // Usage: "exclude this directory", |
| 74 | + // Aliases: []string{"x"}, |
| 75 | + // }, |
| 76 | + |
| 77 | + &cli.StringSliceFlag{ |
| 78 | + Name: "ignore-list", |
| 79 | + Usage: "disables ignoring from default ignore list", |
| 80 | + Value: DefaultIgnoreList, |
| 81 | + Aliases: []string{"I"}, |
| 82 | + }, |
| 83 | + |
| 84 | + &cli.StringFlag{ |
| 85 | + Name: "cooldown", |
| 86 | + Usage: "cooldown duration", |
| 87 | + Value: "100ms", |
| 88 | + }, |
| 89 | + |
| 90 | + &cli.BoolFlag{ |
| 91 | + Name: "interactive", |
| 92 | + Usage: "interactive mode, with stdin", |
| 93 | + }, |
| 94 | + |
| 95 | + &cli.BoolFlag{ |
| 96 | + Name: "sse", |
| 97 | + Usage: "run watcher in sse mode", |
| 98 | + }, |
| 99 | + |
| 100 | + &cli.StringFlag{ |
| 101 | + Name: "sse-addr", |
| 102 | + HideDefault: false, |
| 103 | + Usage: "run watcher in sse mode", |
| 104 | + Sources: cli.ValueSourceChain{}, |
| 105 | + Value: ":12345", |
| 106 | + }, |
| 107 | + }, |
| 108 | + Action: func(ctx context.Context, c *cli.Command) error { |
| 109 | + logger := logging.NewSlogLogger(logging.SlogOptions{ |
| 110 | + ShowTimestamp: false, |
| 111 | + ShowCaller: false, |
| 112 | + ShowDebugLogs: c.Bool("debug"), |
| 113 | + SetAsDefaultLogger: true, |
| 114 | + }) |
| 115 | + |
| 116 | + if c.NArg() == 0 { |
| 117 | + return c.Command("help").Action(ctx, c) |
| 118 | + } |
| 119 | + |
| 120 | + var watchDirs, excludeDirs []string |
| 121 | + |
| 122 | + for _, d := range c.StringSlice("watch") { |
| 123 | + if strings.HasPrefix(d, "-") { |
| 124 | + // INFO: needs to be excluded |
| 125 | + excludeDirs = append(excludeDirs, d[1:]) |
| 126 | + continue |
| 127 | + } |
| 128 | + watchDirs = append(watchDirs, d) |
| 129 | + } |
| 130 | + |
| 131 | + var watchExtensions, ignoreExtensions []string |
| 132 | + for _, ext := range c.StringSlice("ext") { |
| 133 | + if strings.HasPrefix(ext, "-") { |
| 134 | + // INFO: needs to be excluded |
| 135 | + ignoreExtensions = append(ignoreExtensions, ext[1:]) |
| 136 | + continue |
| 137 | + } |
| 138 | + watchExtensions = append(watchExtensions, ext) |
| 139 | + } |
| 140 | + |
| 141 | + cooldown, err := time.ParseDuration(c.String("cooldown")) |
| 142 | + if err != nil { |
| 143 | + panic(err) |
| 144 | + } |
| 145 | + |
| 146 | + args := watcher.WatcherArgs{ |
| 147 | + Logger: logger, |
| 148 | + |
| 149 | + WatchDirs: watchDirs, |
| 150 | + IgnoreDirs: excludeDirs, |
| 151 | + |
| 152 | + WatchExtensions: watchExtensions, |
| 153 | + IgnoreExtensions: ignoreExtensions, |
| 154 | + CooldownDuration: &cooldown, |
| 155 | + |
| 156 | + IgnoreList: c.StringSlice("ignore-list"), |
| 157 | + } |
| 158 | + |
| 159 | + w, err := watcher.NewWatcher(ctx, args) |
| 160 | + if err != nil { |
| 161 | + panic(err) |
| 162 | + } |
| 163 | + |
| 164 | + var ex executor.Executor |
| 165 | + |
| 166 | + switch { |
| 167 | + case c.Bool("sse"): |
| 168 | + { |
| 169 | + sseAddr := c.String("sse-addr") |
| 170 | + ex = executor.NewSSEExecutor(executor.SSEExecutorArgs{Addr: sseAddr}) |
| 171 | + logger.Info("HELLo world") |
| 172 | + } |
| 173 | + default: |
| 174 | + { |
| 175 | + execCmd := c.Args().First() |
| 176 | + execArgs := c.Args().Tail() |
| 177 | + ex = executor.NewCmdExecutor(ctx, executor.CmdExecutorArgs{ |
| 178 | + Logger: logger, |
| 179 | + Interactive: c.Bool("interactive"), |
| 180 | + Command: func(context.Context) *exec.Cmd { |
| 181 | + cmd := exec.CommandContext(ctx, execCmd, execArgs...) |
| 182 | + cmd.Stdout = os.Stdout |
| 183 | + cmd.Stderr = os.Stderr |
| 184 | + cmd.Stdin = os.Stdin |
| 185 | + return cmd |
| 186 | + }, |
| 187 | + // IsInteractive: true, |
| 188 | + }) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + var wg sync.WaitGroup |
| 193 | + wg.Add(1) |
| 194 | + go func() { |
| 195 | + defer wg.Done() |
| 196 | + if err := ex.Start(); err != nil { |
| 197 | + slog.Error("got", "err", err) |
| 198 | + } |
| 199 | + logger.Debug("1. start-job finished") |
| 200 | + }() |
| 201 | + |
| 202 | + counter := 0 |
| 203 | + pwd := fn.Must(os.Getwd()) |
| 204 | + |
| 205 | + wg.Add(1) |
| 206 | + go func() { |
| 207 | + defer wg.Done() |
| 208 | + w.Watch(ctx) |
| 209 | + logger.Debug("2. watch context closed") |
| 210 | + }() |
| 211 | + |
| 212 | + wg.Add(1) |
| 213 | + go func() { |
| 214 | + defer wg.Done() |
| 215 | + <-ctx.Done() |
| 216 | + ex.Stop() |
| 217 | + logger.Debug("3. killed signal processed") |
| 218 | + }() |
| 219 | + |
| 220 | + for event := range w.GetEvents() { |
| 221 | + logger.Debug("received", "event", event) |
| 222 | + relPath, err := filepath.Rel(pwd, event.Name) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + counter += 1 |
| 227 | + logger.Info(fmt.Sprintf("[RELOADING (%d)] due changes in %s", counter, relPath)) |
| 228 | + |
| 229 | + ex.OnWatchEvent(executor.Event{Source: event.Name}) |
| 230 | + } |
| 231 | + |
| 232 | + // logger.Debug("stopping executor") |
| 233 | + // if err := ex.Stop(); err != nil { |
| 234 | + // return err |
| 235 | + // } |
| 236 | + // logger.Info("stopped executor") |
| 237 | + |
| 238 | + wg.Wait() |
| 239 | + return nil |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + ctx, stop := signal.NotifyContext(context.TODO(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT) |
| 244 | + defer stop() |
| 245 | + |
| 246 | + if err := cmd.Run(ctx, os.Args); err != nil { |
| 247 | + panic(err) |
| 248 | + } |
| 249 | + os.Exit(0) |
| 250 | +} |
0 commit comments