Skip to content

Commit

Permalink
add -F cmd line option
Browse files Browse the repository at this point in the history
  • Loading branch information
jftuga committed Oct 8, 2022
1 parent 4666073 commit 8081694
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ Binaries for Windows, macOS, Linux and FreeBSD are provided on the
* For help, run `chars -h`

```
chars v2.2.0
chars v2.3.0
Determine the end-of-line format, tabs, bom, and nul
https://github.com/jftuga/chars
Usage:
chars [filename or file-glob 1] [filename or file-glob 2] ...
-b examine binary files
-F when used with -f, only display a list of failed files (one per line
-b examine binary files
-e string
exclude based on regular expression; use .* instead of *
-f string
fail with OS exit code=100 if any of the included characters exist; ex: -f crlf,nul,bom8
-j output results in JSON format; can't be used with -l
-j output results in JSON format; can't be used with -l
-l int
shorten files names to a maximum of this length
-v display version and then exit
-v display version and then exit
Notes:
Use - to read a file from STDIN
Expand Down Expand Up @@ -113,6 +114,18 @@ $ chars -f lf,tab /etc/group ; echo $?
```

## Example 5
* Fail when certain characters are detected, with `-f`
* Only output failed file names, with `-F`

```shell
$ chars -f lf,tab -F /etc/gr* ; echo $?
/etc/group
/etc/group.bak

100
```

## Example 6

* Output to JSON, with `-j`
* Use `-e` to exclude and filenames starting with `go`, such as `go.mod` and `go.sum`
Expand Down
30 changes: 25 additions & 5 deletions chars.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
const PgmName string = "chars"
const PgmDesc string = "Determine the end-of-line format, tabs, bom, and nul"
const PgmUrl string = "https://github.com/jftuga/chars"
const PgmVersion string = "2.2.0"
const PgmVersion string = "2.3.0"
const BlockSize int = 4096

type SpecialChars struct {
Expand All @@ -42,6 +42,7 @@ type SpecialChars struct {
Bom16 uint64 `json:"bom16"`
Nul uint64 `json:"nul"`
BytesRead uint64 `json:"bytesRead"`
Failure bool `json:"failure"`
}

type CharsError struct {
Expand Down Expand Up @@ -201,6 +202,19 @@ func OutputTextTable(allStats []SpecialChars, maxLength int) error {
return w.Flush()
}

// OutputFailedFileList - only display a list of file names that have failed when using -F cmd line option
func OutputFailedFileList(allStats []SpecialChars) {
if len(allStats) == 0 {
return
}

for _, stat := range allStats {
if stat.Failure {
_, _ = fmt.Println(stat.Filename)
}
}
}

// GetJSON - return results JSON format
func GetJSON(allStats []SpecialChars) string {
j, err := json.MarshalIndent(allStats, "", " ")
Expand Down Expand Up @@ -298,11 +312,12 @@ func ProcessStdin(allStats *[]SpecialChars, examineBinary bool, fail string) (ui

// GetFailures - parse as comma-delimited list and return the number of characters in the given character list
func GetFailures(commaList string, allStats *[]SpecialChars) uint64 {
var failed uint64
var failed, totalFailures uint64

classes := strings.Split(strings.ToLower(commaList), ",")
for _, class := range classes {
for _, entry := range *allStats {
for i, entry := range *allStats {
failed = 0
for _, class := range classes {
switch class {
case "crlf":
failed += entry.Crlf
Expand All @@ -320,6 +335,11 @@ func GetFailures(commaList string, allStats *[]SpecialChars) uint64 {
fmt.Fprintf(os.Stderr, "Unknown character passed to -f: %s\n", class)
}
}

if failed > 0 {
totalFailures += failed
(*allStats)[i].Failure = true
}
}
return failed
return totalFailures
}
3 changes: 3 additions & 0 deletions cmd/chars/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
argsJSON := flag.Bool("j", false, "output results in JSON format; can't be used with -l")
argsVersion := flag.Bool("v", false, "display version and then exit")
argsFail := flag.String("f", "", "fail with OS exit code=100 if any of the included characters exist; ex: -f crlf,nul,bom8")
argsFailedFileList := flag.Bool("F", false, "when used with -f, only display a list of failed files (one per line")

flag.Usage = Usage
flag.Parse()
Expand Down Expand Up @@ -95,6 +96,8 @@ func main() {
if err != nil {
os.Exit(4)
}
} else if *argsFailedFileList && len(*argsFail) > 0 && failed > 0 {
chars.OutputFailedFileList(allStats)
} else {
err := chars.OutputTextTable(allStats, *argsMaxLength)
if err != nil {
Expand Down

0 comments on commit 8081694

Please sign in to comment.