Skip to content

Commit

Permalink
Flag forever (#6)
Browse files Browse the repository at this point in the history
`--forever` flag
  • Loading branch information
bobiverse authored Nov 13, 2023
1 parent 4d89fe4 commit 39daddc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Run command with default params
Output:
```
Command: [date +%s]
Job count to perform: [6]
Job count to perform: [100]
Threads: [5]
Delay: 10 ms
Time limit for single worker: 60000 ms
Expand Down
56 changes: 47 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ func main() {
stopOnMsg := flag.String("stop-on", "", "If any job gets this message, all stops")
whileMsg := flag.String("while", "", "Keep running while all have this message")
interpreter := flag.String("interpreter", cmdInterpreter, "Interpreter for command")
forever := flag.Bool("forever", false, "Run forever")
flag.Parse()

// check flag conflicts
if *forever && *fpath != "" {
log.Fatalf("You must choose one: `--forever` or `-fpath` flag!")
}

if *interpreter != "" {
cmdInterpreter = *interpreter
}
Expand All @@ -49,8 +55,20 @@ func main() {
*delayMs = 1
}

if *forever {
*n = 0
*delayMs = 250 // larger delay
}

var flines []string
if _, err := os.Stat(*fpath); err == nil {
if *fpath != "" {
_, err := os.Stat(*fpath)
if err != nil {
log.Fatalf("\n\nFile `%s` error: %s\n", *fpath, err)
return
}

// file exists, try to read
lines, err := readLines(*fpath)
if err != nil {
log.Fatalf("\n\nFILE READ ERROR!\n%s\n", err)
Expand All @@ -60,10 +78,16 @@ func main() {
}

fmt.Printf("%20s: [%s]\n", "Command", *cmd)
fmt.Printf("%20s: [%s]\n", "Interpreter", *interpreter)

if *interpreter != os.Getenv("SHELL") {
fmt.Printf("%20s: [%s]\n", "Interpreter", *interpreter)
}

if len(flines) > 0 {
fmt.Printf("%20s: [%s]\n", "File to read", *fpath)
fmt.Printf("%20s: [%d]\n", "Lines in file", len(flines))
} else if *forever {
fmt.Printf("%20s: ∞ \n", "Run forever")
} else {
fmt.Printf("%20s: [%d]\n", "Job count to perform", *n)
}
Expand Down Expand Up @@ -105,14 +129,28 @@ func main() {
flines = append(flines, strconv.Itoa(i))
}
}

// lesgoooooo!
total := len(flines)
for i, fline := range flines {
i := i
sTotal := fmt.Sprintf("%d", total)
if *forever {
sTotal = "∞"
}

for index := 0; ; index++ {
i := index

// stop on limit reached if not forever
if !*forever && (i > total-1) {
break
}

// Replace variables
flcmd := *cmd
flcmd = strings.ReplaceAll(flcmd, "{{N}}", fmt.Sprintf("%d", i))
flcmd = strings.ReplaceAll(flcmd, "{{LINE}}", fline)

// Replace variables from file
if !*forever && i <= total-1 {
flcmd = strings.ReplaceAll(flcmd, "{{LINE}}", flines[i])
}

sg.Go(func() error {
// log.Printf("`%v`", flcmd)
Expand All @@ -131,12 +169,12 @@ func main() {

errStr = strings.TrimSpace(errStr)
if errStr != "" {
log.Printf("ERROR: [%d/%d] [%s] ==> [%s]", i, total, flcmd, errStr)
log.Printf("ERROR: [%d/%s] [%s] ==> [%s]", i, sTotal, flcmd, errStr)
// do not mark semgroup job with error. we printed it
return nil

}
log.Printf("[%d/%d] [%s] ==> [%s]", i, total, flcmd, cmdOut)
log.Printf("[%d/%s] [%s] ==> [%s]", i, sTotal, flcmd, cmdOut)

// Stop if not expected message
if *whileMsg != "" && !strings.Contains(string(cmdOut), *whileMsg) {
Expand Down

0 comments on commit 39daddc

Please sign in to comment.