Skip to content

Commit

Permalink
document patterns in --help
Browse files Browse the repository at this point in the history
stuff
  • Loading branch information
taylormonacelli committed Jul 31, 2024
1 parent 2cfea4b commit cac985d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
42 changes: 23 additions & 19 deletions cmd/watchDir.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"os"

"github.com/gkwa/littlewill/watcher"
"github.com/spf13/cobra"
)
Expand All @@ -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(),
Expand All @@ -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`)
}
14 changes: 14 additions & 0 deletions core/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 1 addition & 15 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/gkwa/littlewill/core"
"github.com/gkwa/littlewill/file"
"github.com/go-logr/logr"
)

Expand All @@ -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)
}
Expand Down

0 comments on commit cac985d

Please sign in to comment.