Skip to content

Commit

Permalink
Make golint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp committed May 13, 2018
1 parent 1c58742 commit 33ae6ec
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 56 deletions.
34 changes: 19 additions & 15 deletions internal/app/lwc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/timdp/lwc/internal/pkg/lwcutil"
)

const DEFAULT_INTERVAL int = 100
const defaultInterval int = 100

// Config represents a configuration
type Config struct {
Lines bool
Words bool
Expand All @@ -25,14 +26,16 @@ type Config struct {
g *getopt.Set
}

// PrintUsage prints usage
func (c *Config) PrintUsage() {
writer := lwcutil.GetStdout()
c.g.PrintUsage(writer)
fmt.Fprintln(writer, "\nFull documentation at: <https://github.com/timdp/lwc>")
}

// NewConfig creates a new Config
func NewConfig(args []string) *Config {
intervalMs := DEFAULT_INTERVAL
intervalMs := defaultInterval
var c Config
c.g = getopt.New()
c.g.SetParameters("[file ...]")
Expand All @@ -45,7 +48,7 @@ func NewConfig(args []string) *Config {
"read input from the files specified by NUL-terminated names in file F",
"F")
c.g.FlagLong(&intervalMs, "interval", 'i',
fmt.Sprintf("set the update interval to T ms (default %d ms)", DEFAULT_INTERVAL),
fmt.Sprintf("set the update interval to T ms (default %d ms)", defaultInterval),
"T")
c.g.FlagLong(&c.Help, "help", 0, "display this help and exit")
c.g.FlagLong(&c.Version, "version", 0, "output version information and exit")
Expand All @@ -63,37 +66,38 @@ func NewConfig(args []string) *Config {
return &c
}

func (config *Config) Processors() []Processor {
// Processors creates the processors enabled by the configuration
func (c *Config) Processors() []Processor {
var temp [5]Processor
i := 0
if config.Lines {
temp[i] = Processor{lwcutil.ScanBytes(LINE_FEED, true), ScanCount}
if c.Lines {
temp[i] = Processor{lwcutil.ScanBytes(LineFeed, true), ScanCount}
i++
}
if config.Words {
if c.Words {
temp[i] = Processor{bufio.ScanWords, ScanCount}
i++
}
if config.Chars {
if c.Chars {
temp[i] = Processor{bufio.ScanRunes, ScanCount}
i++
}
if config.Bytes {
if c.Bytes {
temp[i] = Processor{bufio.ScanBytes, ScanCount}
i++
}
if config.MaxLineLength {
if c.MaxLineLength {
temp[i] = Processor{lwcutil.ScanLines, ScanMaxLength}
i++
}
return temp[0:i]
}

func (config *Config) FilesChan() *chan string {
if config.Files0From != "" {
reader := lwcutil.OpenFile(config.Files0From)
// FilesChan creates the channel sending the filenames in the configuration
func (c *Config) FilesChan() *chan string {
if c.Files0From != "" {
reader := lwcutil.OpenFile(c.Files0From)
return lwcutil.NewFilesChanFromReader(reader, byte(0))
} else {
return lwcutil.NewFilesChanFromSlice(config.Files)
}
return lwcutil.NewFilesChanFromSlice(c.Files)
}
10 changes: 5 additions & 5 deletions internal/app/lwc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var configTests = []configTest{
Config{
true, true, false, true, false,
"",
time.Duration(DEFAULT_INTERVAL) * time.Millisecond,
time.Duration(defaultInterval) * time.Millisecond,
false, false,
[]string{},
nil,
Expand All @@ -31,7 +31,7 @@ var configTests = []configTest{
Config{
true, true, false, false, false,
"",
time.Duration(DEFAULT_INTERVAL) * time.Millisecond,
time.Duration(defaultInterval) * time.Millisecond,
false, false,
[]string{},
nil,
Expand All @@ -42,7 +42,7 @@ var configTests = []configTest{
Config{
true, true, false, true, false,
"",
time.Duration(DEFAULT_INTERVAL) * time.Millisecond,
time.Duration(defaultInterval) * time.Millisecond,
false, false,
[]string{"foo"},
nil,
Expand All @@ -53,7 +53,7 @@ var configTests = []configTest{
Config{
true, true, false, true, false,
"",
time.Duration(DEFAULT_INTERVAL) * time.Millisecond,
time.Duration(defaultInterval) * time.Millisecond,
false, false,
[]string{"/path/to/file"},
nil,
Expand All @@ -64,7 +64,7 @@ var configTests = []configTest{
Config{
false, false, false, true, true,
"",
time.Duration(DEFAULT_INTERVAL) * time.Millisecond,
time.Duration(defaultInterval) * time.Millisecond,
false, false,
[]string{"/etc/passwd", "/etc/group"},
nil,
Expand Down
20 changes: 11 additions & 9 deletions internal/app/lwc/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,34 @@ import (
"github.com/timdp/lwc/internal/pkg/lwcutil"
)

const COUNT_FORMAT string = "%8d"
const countFormat string = "%8d"

func FormatCounts(counts *[]uint64, name string, cr bool, lf bool) *bytes.Buffer {
func formatCounts(counts *[]uint64, name string, cr bool, lf bool) *bytes.Buffer {
buf := new(bytes.Buffer)
if cr {
buf.WriteByte(CARRIAGE_RETURN)
buf.WriteByte(CarriageReturn)
}
buf.WriteString(fmt.Sprintf(COUNT_FORMAT, (*counts)[0]))
buf.WriteString(fmt.Sprintf(countFormat, (*counts)[0]))
for i := 1; i < len(*counts); i++ {
buf.WriteByte(SPACE)
buf.WriteString(fmt.Sprintf(COUNT_FORMAT, (*counts)[i]))
buf.WriteByte(Space)
buf.WriteString(fmt.Sprintf(countFormat, (*counts)[i]))
}
if name != "" {
buf.WriteByte(SPACE)
buf.WriteByte(Space)
buf.WriteString(name)
}
if lf {
buf.WriteByte(LINE_FEED)
buf.WriteByte(LineFeed)
}
return buf
}

// PrintCounts writes formatted counts to stdout
func PrintCounts(counts *[]uint64, name string, cr bool, lf bool) {
lwcutil.GetStdout().Write(FormatCounts(counts, name, cr, lf).Bytes())
lwcutil.GetStdout().Write(formatCounts(counts, name, cr, lf).Bytes())
}

// PollCounts periodically writes counts to stdout
func PollCounts(name string, counts *[]uint64, interval time.Duration, done chan bool) {
tick := time.NewTicker(interval)
defer tick.Stop()
Expand Down
5 changes: 2 additions & 3 deletions internal/app/lwc/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ func (t *formatTest) expected() []string {
func withWithout(b bool) string {
if b {
return "with"
} else {
return "without"
}
return "without"
}

func tokenize(str string) []string {
Expand Down Expand Up @@ -77,7 +76,7 @@ var formatTests = []formatTest{

func TestFormatCounts(t *testing.T) {
for i, test := range formatTests {
result := FormatCounts(&test.counts, test.label, test.cr, test.lf).String()
result := formatCounts(&test.counts, test.label, test.cr, test.lf).String()
hasCr := strings.HasPrefix(result, "\r")
if test.cr != hasCr {
t.Errorf("Test #%d failed: expecting string %s CR prefix", i, withWithout(test.cr))
Expand Down
25 changes: 13 additions & 12 deletions internal/app/lwc/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"github.com/timdp/lwc/internal/pkg/lwcutil"
)

// Processor represents a scanner and splitter for input
type Processor struct {
Split bufio.SplitFunc
Scan ScanFunc
}

func ProcessReader(reader io.Reader, processor Processor, count *uint64, total *uint64) {
func processReader(reader io.Reader, processor Processor, count *uint64, total *uint64) {
scanner := bufio.NewScanner(reader)
scanner.Split(processor.Split)
processor.Scan(scanner, count, total)
Expand All @@ -25,17 +26,16 @@ func ProcessReader(reader io.Reader, processor Processor, count *uint64, total *
}
}

func OpenFile(namePtr *string) (string, *os.File) {
func openFile(namePtr *string) (string, *os.File) {
if namePtr == nil {
return "", os.Stdin
} else {
return *namePtr, lwcutil.OpenFile(*namePtr)
}
return *namePtr, lwcutil.OpenFile(*namePtr)
}

func ProcessFile(namePtr *string, processors []Processor, totals *[]uint64, interval time.Duration) {
func processFile(namePtr *string, processors []Processor, totals *[]uint64, interval time.Duration) {
// Open input file (can be stdin)
name, file := OpenFile(namePtr)
name, file := openFile(namePtr)

numCounts := len(processors)

Expand All @@ -60,7 +60,7 @@ func ProcessFile(namePtr *string, processors []Processor, totals *[]uint64, inte
}
go func(reader io.Reader, processor Processor, count *uint64, total *uint64) {
defer wg.Done()
ProcessReader(reader, processor, count, total)
processReader(reader, processor, count, total)
}(pipes[index].R, processor, &counts[index], totalPtr)
}

Expand Down Expand Up @@ -90,6 +90,7 @@ func ProcessFile(namePtr *string, processors []Processor, totals *[]uint64, inte
PrintCounts(&counts, name, live, true)
}

// ProcessFiles processes files as required by the configuration
func ProcessFiles(config *Config) {
files := config.FilesChan()
processors := config.Processors()
Expand All @@ -98,7 +99,7 @@ func ProcessFiles(config *Config) {

// If no files given, process stdin
if name1 == "" {
ProcessFile(nil, processors, nil, config.Interval)
processFile(nil, processors, nil, config.Interval)
return
}

Expand All @@ -113,17 +114,17 @@ func ProcessFiles(config *Config) {
totals = &totalsRaw
}

ProcessFile(&name1, processors, totals, config.Interval)
processFile(&name1, processors, totals, config.Interval)

if name2 != "" {
ProcessFile(&name2, processors, totals, config.Interval)
processFile(&name2, processors, totals, config.Interval)

// Process files sequentially
for name := range *files {
if name == lwcutil.END_OF_FILES {
if name == lwcutil.EndOfFiles {
break
}
ProcessFile(&name, processors, totals, config.Interval)
processFile(&name, processors, totals, config.Interval)
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/app/lwc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
)

// Run runs lwc
func Run(version string, date string) {
// Read command-line args
config := NewConfig(os.Args)
Expand Down
11 changes: 8 additions & 3 deletions internal/app/lwc/runes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package lwc

const CARRIAGE_RETURN byte = 13
const LINE_FEED byte = 10
const SPACE byte = 32
const (
// CarriageReturn means \r
CarriageReturn byte = 13
// LineFeed means \n
LineFeed byte = 10
// Space really means space
Space byte = 32
)
3 changes: 3 additions & 0 deletions internal/app/lwc/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"sync/atomic"
)

// ScanFunc represents a function that applies a scanner and updates counts
type ScanFunc func(*bufio.Scanner, *uint64, *uint64)

// ScanCount applies a scanner and adds one for every occurrence
func ScanCount(scanner *bufio.Scanner, count *uint64, total *uint64) {
for scanner.Scan() {
atomic.AddUint64(count, 1)
Expand All @@ -16,6 +18,7 @@ func ScanCount(scanner *bufio.Scanner, count *uint64, total *uint64) {
}
}

// ScanMaxLength applies a scanner and remembers max string length
func ScanMaxLength(scanner *bufio.Scanner, count *uint64, total *uint64) {
var localMax uint64
var globalMax uint64
Expand Down
10 changes: 7 additions & 3 deletions internal/pkg/lwcutil/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ import (
"io"
)

const END_OF_FILES string = ""
// EndOfFiles marks the end of the list of files
const EndOfFiles string = ""

// ValidateFileName checks whether the given string is a valid file name
func ValidateFileName(name string) {
if len(name) == 0 {
Fatal("invalid zero-length file name")
}
}

// NewFilesChanFromSlice creates a files chan from a string slice
func NewFilesChanFromSlice(values []string) *chan string {
c := make(chan string)
go func() {
for _, value := range values {
ValidateFileName(value)
c <- value
}
c <- END_OF_FILES
c <- EndOfFiles
}()
return &c
}

// NewFilesChanFromReader creates a files chan from a reader, one name per line
func NewFilesChanFromReader(reader io.Reader, separator byte) *chan string {
c := make(chan string)
scanner := bufio.NewScanner(reader)
Expand All @@ -35,7 +39,7 @@ func NewFilesChanFromReader(reader io.Reader, separator byte) *chan string {
ValidateFileName(name)
c <- name
}
c <- END_OF_FILES
c <- EndOfFiles
}()
return &c
}
1 change: 1 addition & 0 deletions internal/pkg/lwcutil/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
)

// OpenFile opens a file by name, or stdin if the name is a hyphen
func OpenFile(name string) *os.File {
if name == "-" {
return os.Stdin
Expand Down
Loading

0 comments on commit 33ae6ec

Please sign in to comment.