-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
215 lines (180 loc) · 4.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
"github.com/spf13/cobra"
)
type flagOptions struct {
lineFlag bool
wordFlag bool
charFlag bool
}
var (
flagSet flagOptions
totalLineCount, totalWordCount, totalCharCount int
)
const maxOpenFileLimit = 10
func init() {
// Add flags to count lines, words, and characters
rootCmd.Flags().BoolVarP(&flagSet.lineFlag, "lines", "l", false, "Count number of lines")
rootCmd.Flags().BoolVarP(&flagSet.wordFlag, "words", "w", false, "Count number of words")
rootCmd.Flags().BoolVarP(&flagSet.charFlag, "chars", "c", false, "Count number of characters")
}
func main() {
// Execute the cobra command
if err := rootCmd.Execute(); err != nil {
printToStderr(err)
os.Exit(1)
}
}
var rootCmd = &cobra.Command{
Use: "wc",
Short: "wc is a word, line, and character count tool",
Long: `wc is a word, line, and character count tool that reads from the standard input or from a file and outputs the number of lines, words, and characters`,
Run: func(cmd *cobra.Command, args []string) {
// If length args is equal to '0' then set args as "-" to be identified as os.Stdin
if len(args) == 0 {
args = []string{"-"}
}
var wg sync.WaitGroup
maxOpenFilesLimitBuffer := make(chan int, maxOpenFileLimit)
for _, arg := range args {
go worker(arg, &wg, maxOpenFilesLimitBuffer)
wg.Add(1)
}
wg.Wait()
// print total only if more than one file is passed
if len(args) > 1 {
totalResult, err := result{
lineCount: totalLineCount,
wordCount: totalWordCount,
charCount: totalCharCount,
filename: "total",
}.generateOutput()
if err != nil {
printToStderr(err)
}
printToStdout(totalResult)
}
},
}
type result struct {
lineCount int
wordCount int
charCount int
filename string
err error
}
func worker(arg string, wg *sync.WaitGroup, maxOpenFileLimitBuffer chan int) {
lines := make(chan string)
errChan := make(chan error)
maxOpenFileLimitBuffer <- 1
defer func() {
wg.Done()
<-maxOpenFileLimitBuffer
}()
//keep reading lines from files and let the count function run as it happens.
go readLinesInFile(arg, lines, errChan)
result := count(lines, errChan)
totalLineCount += result.lineCount
totalWordCount += result.wordCount
totalCharCount += result.charCount
result.filename = arg
output, err := result.generateOutput()
if err != nil {
printToStderr(err)
return
}
printToStdout(output)
}
func readLinesInFile(filename string, lines chan<- string, errChan chan<- error) {
var scanner *bufio.Scanner
const chunkSize = 1024 * 1024 // 1 MB
defer close(lines)
defer close(errChan)
if filename == "-" {
scanner = bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, chunkSize), chunkSize)
} else {
file, err := os.Open(filename)
if err != nil {
err = fmt.Errorf(
"gowc: " + strings.Replace(err.Error(), "open ", "", 1) + "\n",
)
errChan <- err
}
defer file.Close()
scanner = bufio.NewScanner(file)
scanner.Buffer(make([]byte, chunkSize), chunkSize)
}
for scanner.Scan() {
line := scanner.Text()
lines <- line // send line to the channel
}
if err := scanner.Err(); err != nil {
err = fmt.Errorf(
"gowc: " + strings.Replace(err.Error(), "read ", "", 1) + "\n",
)
errChan <- err
}
}
func count(lines <-chan string, errChan <-chan error) result {
var r result
for {
select {
case err := <-errChan:
if err != nil {
r.err = err
errChan = nil
return r
}
case line, ok := <-lines:
if !ok {
return r
}
r.lineCount++
words := strings.Fields(line)
r.wordCount += len(words)
r.charCount += len(line) + 1
}
}
}
func (r result) generateOutput() (string, error) {
var output string
if r.err != nil {
return "", r.err
}
// append only if lineFlag is set
if flagSet.lineFlag {
output += fmt.Sprintf("%8d", r.lineCount)
}
// append only if wordFlag is set
if flagSet.wordFlag {
output += fmt.Sprintf("%8d", r.wordCount)
}
// append only if charFlag is set
if flagSet.charFlag {
output += fmt.Sprintf("%8d", r.charCount)
}
if !flagSet.lineFlag && !flagSet.wordFlag && !flagSet.charFlag {
output += fmt.Sprintf("%8d", r.lineCount)
output += fmt.Sprintf("%8d", r.wordCount)
output += fmt.Sprintf("%8d", r.charCount)
}
// append the filename only if reading from a file intead of os.Stdin after appending the count
if r.filename == "-" {
output += "\n"
} else {
output += fmt.Sprint(" " + r.filename + "\n")
}
return output, nil
}
func printToStderr(err error) {
fmt.Fprint(os.Stderr, err.Error())
}
func printToStdout(s string) {
fmt.Fprint(os.Stdout, s)
}