Skip to content

Commit

Permalink
Merge pull request #6 from pablodz/feat/tagger-and-decrease-complexity
Browse files Browse the repository at this point in the history
tagger and decrease complexity
  • Loading branch information
pablodz authored Jul 12, 2024
2 parents 13ac996 + c4015e0 commit 4955d67
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 41 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/tagger.yml
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
git config --local user.name "GitHub Actions"
git tag ${{ env.NEW_TAG }}
git push origin ${{ env.NEW_TAG }}
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.5
v0.0.8
9 changes: 7 additions & 2 deletions inotifywaitgo/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ 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()
if line != "" {
return true, nil
}
}

if err := scanner.Err(); err != nil {
return false, err
}

return false, nil
}
8 changes: 4 additions & 4 deletions inotifywaitgo/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 47 additions & 31 deletions inotifywaitgo/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -45,6 +43,7 @@ func WatchPath(s *Settings) {
s.ErrorChan <- err
return
}

if err := cmd.Start(); err != nil {
s.ErrorChan <- err
return
Expand All @@ -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
}
3 changes: 0 additions & 3 deletions inotifywaitgo/watcher_test.go

This file was deleted.

0 comments on commit 4955d67

Please sign in to comment.