From cac985da493cd709da4161780b0f4e57237d5137 Mon Sep 17 00:00:00 2001 From: Taylor Monacelli Date: Tue, 30 Jul 2024 17:50:52 -0700 Subject: [PATCH] document patterns in --help stuff --- cmd/watchDir.go | 42 +++++++++++++++++++++++------------------- core/file.go | 14 ++++++++++++++ watcher/watcher.go | 16 +--------------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/cmd/watchDir.go b/cmd/watchDir.go index f0f440c..a7ca48e 100644 --- a/cmd/watchDir.go +++ b/cmd/watchDir.go @@ -1,8 +1,6 @@ package cmd import ( - "os" - "github.com/gkwa/littlewill/watcher" "github.com/spf13/cobra" ) @@ -15,23 +13,18 @@ var ( var watchDirCmd = &cobra.Command{ Use: "watch-dir [directory]", Aliases: []string{"wd"}, - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: + Args: cobra.ExactArgs(1), + Short: "Watch a directory for file changes", + Long: `Watch a directory for file changes and process modified files. -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - err := cmd.Usage() - if err != nil { - cmd.PrintErrf("Error: %v\n", err) - } - cmd.PrintErrln("Error: directory path is required") - os.Exit(1) - } +You can specify patterns to filter which files to watch. If no patterns are specified, +all files will be watched. Patterns use standard glob syntax. +Examples: + littlewill watch-dir /path/to/directory + littlewill watch-dir /path/to/directory --patterns "*.md,*.txt" + littlewill watch-dir /path/to/directory --patterns "doc_*.md" --patterns "report_*.txt"`, + Run: func(cmd *cobra.Command, args []string) { dir := args[0] watcher.RunWatcher( cmd.Context(), @@ -45,6 +38,17 @@ to quickly create a Cobra application.`, func init() { rootCmd.AddCommand(watchDirCmd) - watchDirCmd.Flags().StringSliceVar(&patterns, "patterns", []string{}, "File patterns to watch") - watchDirCmd.Flags().StringVar(&filterType, "filter-type", "write", "Filter type (create, write, remove, rename, chmod)") + + watchDirCmd.Flags().StringSliceVarP(&patterns, "patterns", "p", []string{}, `File patterns to watch (comma-separated or multiple flags). +Examples: + --patterns "*.md,*.txt" + --patterns "*.go" --patterns "*.yaml" +Default: watch all files`) + + watchDirCmd.Flags().StringVarP(&filterType, "filter-type", "f", "write", `Event type to filter on. Options: + create: Only watch for new files + write: Watch for file modifications (default) + remove: Watch for file deletions + rename: Watch for file renames + chmod: Watch for permission changes`) } diff --git a/core/file.go b/core/file.go index 3129876..7e4dc91 100644 --- a/core/file.go +++ b/core/file.go @@ -8,10 +8,24 @@ import ( "io" "os" + "github.com/gkwa/littlewill/file" "github.com/go-logr/logr" ) func ProcessFile(logger logr.Logger, path string, transforms ...func(io.Reader, io.Writer) error) error { + f := file.File{Path: path} + + isSymlink, err := f.IsSymlink() + if err != nil { + logger.Error(err, "Failed to check if path is symlink", "path", path) + } + logger.V(1).Info("file type check", "path", path, "type", f.FileType()) + + if isSymlink { + logger.V(1).Info("skipping symlink", "path", path) + return nil + } + originalContent, err := os.ReadFile(path) if err != nil { return fmt.Errorf("failed to read original file: %w", err) diff --git a/watcher/watcher.go b/watcher/watcher.go index e20fdc0..bc169ac 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -10,7 +10,6 @@ import ( "github.com/fsnotify/fsnotify" "github.com/gkwa/littlewill/core" - "github.com/gkwa/littlewill/file" "github.com/go-logr/logr" ) @@ -35,20 +34,7 @@ func RunWatcher( time.Sleep(100 * time.Millisecond) fmt.Printf("Event: %s, File: %s\n", event.Op, path) - f := file.File{Path: path} - - isSymlink, err := f.IsSymlink() - if err != nil { - logger.Error(err, "Failed to check if path is symlink", "path", path) - } - logger.V(1).Info("file type check", "path", path, "type", f.FileType()) - - if isSymlink { - logger.V(1).Info("skipping symlink", "path", path) - return - } - - err = core.ProcessFile(logger, path, linkTransforms...) + err := core.ProcessFile(logger, path, linkTransforms...) if err != nil { logger.Error(err, "Failed to process file", "path", path) }