From c4015e07cd7aff0caa3c8f574df1a53c04fcea08 Mon Sep 17 00:00:00 2001 From: Pablo Diaz Date: Fri, 12 Jul 2024 13:19:34 -0500 Subject: [PATCH] tagger and decrease complexity --- .github/workflows/tagger.yml | 31 ++++++++++++++ .version | 2 +- inotifywaitgo/check.go | 9 +++- inotifywaitgo/command.go | 8 ++-- inotifywaitgo/watcher.go | 78 +++++++++++++++++++++-------------- inotifywaitgo/watcher_test.go | 3 -- 6 files changed, 90 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/tagger.yml delete mode 100644 inotifywaitgo/watcher_test.go diff --git a/.github/workflows/tagger.yml b/.github/workflows/tagger.yml new file mode 100644 index 0000000..53cc0b1 --- /dev/null +++ b/.github/workflows/tagger.yml @@ -0,0 +1,31 @@ +name: tagger +on: + push: + branches: + - main +permissions: + contents: write + +jobs: + tagger: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create tag + id: tag + run: | + VERSION_FILE=".version" + VERSION_VALUE=$(cat $VERSION_FILE) + MAX_BRANCH_LENGTH=40 + FIXED_BRANCH=$(echo ${GITHUB_REF:11:${MAX_BRANCH_LENGTH}} | sed 's/[^[:alnum:]]/-/g') + NEW_TAG=$(echo "$VERSION_VALUE-${FIXED_BRANCH}.$(date +%Y%m%d-%H%M%S)") + echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV + + - name: Push tag + run: | + git config --local user.email "actions@github.com" + git config --local user.name "GitHub Actions" + git tag ${{ env.NEW_TAG }} + git push origin ${{ env.NEW_TAG }} \ No newline at end of file diff --git a/.version b/.version index 837042c..e1d848b 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -v0.0.5 \ No newline at end of file +v0.0.8 \ No newline at end of file diff --git a/inotifywaitgo/check.go b/inotifywaitgo/check.go index 0580ef3..aef702d 100644 --- a/inotifywaitgo/check.go +++ b/inotifywaitgo/check.go @@ -5,18 +5,18 @@ import ( "os/exec" ) -// Function to checkDependencies if inotifywait is installed +// CheckDependencies verifies if inotifywait is installed. func checkDependencies() (bool, error) { cmd := exec.Command("bash", "-c", "which inotifywait") stdout, err := cmd.StdoutPipe() if err != nil { return false, err } + if err := cmd.Start(); err != nil { return false, err } - // Read the output of inotifywait and split it into lines scanner := bufio.NewScanner(stdout) for scanner.Scan() { line := scanner.Text() @@ -24,5 +24,10 @@ func checkDependencies() (bool, error) { return true, nil } } + + if err := scanner.Err(); err != nil { + return false, err + } + return false, nil } diff --git a/inotifywaitgo/command.go b/inotifywaitgo/command.go index 0729762..3bf0fbb 100644 --- a/inotifywaitgo/command.go +++ b/inotifywaitgo/command.go @@ -34,27 +34,27 @@ func GenerateBashCommands(s *Settings) ([]string, error) { if !Contains(VALID_EVENTS, int(event)) { return nil, errors.New(INVALID_EVENT) } - baseCmd = append(baseCmd, "-e ", EVENT_MAP[int(event)]) + baseCmd = append(baseCmd, "-e", EVENT_MAP[int(event)]) } } baseCmd = append(baseCmd, s.Dir) - // remove spaces on all elements + // Trim spaces on all elements var outCmd []string for _, v := range baseCmd { outCmd = append(outCmd, strings.TrimSpace(v)) } if s.Verbose { - fmt.Println("baseCmd:", outCmd) + fmt.Println("Generated command:", strings.Join(outCmd, " ")) } return outCmd, nil } // Contains checks if a slice contains an item -func Contains[T string | int](slice []T, item T) bool { +func Contains[T comparable](slice []T, item T) bool { for _, v := range slice { if v == item { return true diff --git a/inotifywaitgo/watcher.go b/inotifywaitgo/watcher.go index 6529331..5e4d643 100644 --- a/inotifywaitgo/watcher.go +++ b/inotifywaitgo/watcher.go @@ -10,18 +10,16 @@ import ( "strings" ) -// Function that starts watching a path for new files and returns the file name (abspath) when a new file is finished writing +// WatchPath starts watching a path for new files and returns the file name (abspath) when a new file is finished writing. func WatchPath(s *Settings) { // Check if inotifywait is installed - ok, err := checkDependencies() - if !ok || err != nil { + if ok, err := checkDependencies(); !ok || err != nil { s.ErrorChan <- fmt.Errorf(NOT_INSTALLED) return } - // check if dir exists - _, err = os.Stat(s.Dir) - if os.IsNotExist(err) { + // Check if the directory exists + if _, err := os.Stat(s.Dir); os.IsNotExist(err) { s.ErrorChan <- fmt.Errorf(DIR_NOT_EXISTS) return } @@ -45,6 +43,7 @@ func WatchPath(s *Settings) { s.ErrorChan <- err return } + if err := cmd.Start(); err != nil { s.ErrorChan <- err return @@ -53,52 +52,69 @@ func WatchPath(s *Settings) { // Read the output of inotifywait and split it into lines scanner := bufio.NewScanner(stdout) for scanner.Scan() { - log.Println(scanner.Text()) line := scanner.Text() + log.Println(line) - r := csv.NewReader(strings.NewReader(line)) - - parts, err := r.Read() + parts, err := parseLine(line) if err != nil || len(parts) < 2 { s.ErrorChan <- fmt.Errorf(INVALID_OUTPUT) continue } - // Extract the input file name from the inotifywait output - prefix := parts[0] - file := parts[2] + prefix, file := parts[0], parts[2] + eventStrs := strings.Split(parts[1], ",") - eventsStr := strings.Split(parts[1], ",") if s.Verbose { - for _, eventStr := range eventsStr { + for _, eventStr := range eventStrs { log.Printf("eventStr: <%s>, <%s>", eventStr, line) } } - var eventsEvents []EVENT - isDir := false - for _, eventStr := range eventsStr { - if eventStr == FlagIsdir { - isDir = true - continue - } - - eventStr = strings.ToLower(eventStr) - event, ok := EVENT_MAP_REVERSE[eventStr] - if !ok { - s.ErrorChan <- fmt.Errorf("invalid eventStr: <%s>, <%s>", eventStr, line) - continue - } - eventsEvents = append(eventsEvents, EVENT(event)) + events, isDir := parseEvents(eventStrs, line, s) + if events == nil { + continue } event := FileEvent{ Filename: prefix + file, - Events: eventsEvents, + Events: events, IsDir: isDir, } // Send the file name to the channel s.FileEvents <- event } + + if err := scanner.Err(); err != nil { + s.ErrorChan <- err + } +} + +// parseLine parses a line of inotifywait output. +func parseLine(line string) ([]string, error) { + r := csv.NewReader(strings.NewReader(line)) + return r.Read() +} + +// parseEvents parses event strings into EVENT types. +func parseEvents(eventStrs []string, line string, s *Settings) ([]EVENT, bool) { + var events []EVENT + isDir := false + + for _, eventStr := range eventStrs { + if eventStr == FlagIsdir { + isDir = true + continue + } + + eventStr = strings.ToLower(eventStr) + event, ok := EVENT_MAP_REVERSE[eventStr] + if !ok { + s.ErrorChan <- fmt.Errorf("invalid eventStr: <%s>, <%s>", eventStr, line) + return nil, false + } + events = append(events, EVENT(event)) + } + + return events, isDir } diff --git a/inotifywaitgo/watcher_test.go b/inotifywaitgo/watcher_test.go deleted file mode 100644 index ce4bb6e..0000000 --- a/inotifywaitgo/watcher_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package inotifywaitgo - -// TODO: Do test