Skip to content

Commit

Permalink
add support for running ccwc against the stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
firminochangani committed Dec 31, 2024
1 parent 4f89ea6 commit 699139f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
40 changes: 40 additions & 0 deletions golang/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asciicheck
- bodyclose
- dogsled
- exhaustive
- gocognit
- goconst
- gofmt
- goheader
- goimports
- gosec
- misspell
- nakedret
- nestif
- noctx
- rowserrcheck
- sqlclosecheck
- unconvert
- unparam
- whitespace

issues:
exclude:
- "composite literal uses unkeyed fields"
exclude-rules:
- path: _test\.go
linters:
- gosec
- noctx
- unparam
- bodyclose
15 changes: 14 additions & 1 deletion golang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@

A reimplementation of [wc](https://en.wikipedia.org/wiki/Wc_(Unix)) as based on [Coding Challenges / Build Your Own wc Tool](https://codingchallenges.fyi/challenges/challenge-wc).

`TODO: DOC TO BE WRITTEN`
## Instructions

1 - Build a binary by running `make build` which will save the binary in `bin/ccwc`

2 - Running commands against a file: `bin/ccwc -c path/to/file`

3 - Running commands against the standard input: `cat path/to/file | bin/ccwc -c`

## Commands implemented

- `-c` - The number of bytes in each input file is written to the standard output.
- `-l` - The number of lines in each input file is written to the standard output.
- `-m` - The number of characters in each input file is written to the standard output.
- `w` - The number of words in each input file is written to the standard output.
32 changes: 20 additions & 12 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,29 @@ func main() {
countCharactersCmd := flag.Bool("m", false, "ccwc -m path/to/file - Returns the number of characters in the file specified")
flag.Parse()

fileName := os.Args[len(os.Args)-1]

if strings.TrimSpace(fileName) == "" {
fmt.Println("A file must be specified. E.g.: ccwc -c path/to/file")
os.Exit(1)
var err error
var fileName string
var fileSource *os.File

if len(os.Args) == 1 || len(os.Args) == 2 && (os.Args[1] == "-c" || os.Args[1] == "-l" || os.Args[1] == "-w" || os.Args[1] == "-m") {
fileSource = os.Stdin
} else {
fileName = os.Args[len(os.Args)-1]
fileSource, err = os.Open(fileName)
if err != nil {
fmt.Printf("error opening the file '%s': %v", fileName, err)
return
}
}

f, err := os.Open(fileName)
if err != nil {
fmt.Printf("error opening the file '%s': %v", fileName, err)
return
}
defer func() { _ = f.Close() }()
defer func() {
closeErr := fileSource.Close()
if closeErr != nil {
fmt.Printf("error closing file or stdin: %v\n", closeErr)
}
}()

result, err := getStatsFromReader(f)
result, err := getStatsFromReader(fileSource)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down

0 comments on commit 699139f

Please sign in to comment.