Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions challenge-01/analyzer/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/priyanshsao/coding-challenges-go/challenge-01/config"
"github.com/sirupsen/logrus"
)

// ipMode represents input mode
Expand All @@ -16,6 +17,18 @@ const (
STD_IN
)

func (ipM ipMode) String() string {

switch ipM {
case TERMINAL:
return "Terminal"
case STD_IN:
return "Standard input"
}

return ""
}

// Read reads the input and stores it in Config.
func Read(c *config.Config) error {

Expand All @@ -29,7 +42,7 @@ func Read(c *config.Config) error {
switch mode {
case TERMINAL:
if len(c.Args) == 0 {
// Todo: add debug.
logrus.Debugf("unable to read: %s", ErrNoArgProvided)
return ErrNoArgProvided
}
reader, err = readTerm(c.Args[0])
Expand All @@ -50,20 +63,23 @@ func getIpMode() (ipMode, error) {

stat, err := os.Stdin.Stat()
if err != nil {
// Todo: add debug here
logrus.Debugf("unable to get input mode: %v", err)
return -1, err
}

if (stat.Mode() & os.ModeCharDevice) == 0 {
logrus.Debugf("input mode detected: %v", STD_IN)
return STD_IN, nil
}

logrus.Debugf("input mode detected: %v", TERMINAL)
return TERMINAL, nil
}

func readTerm(filePath string) (*os.File, error) {

if trimmedFpath := strings.TrimSpace(filePath); trimmedFpath == "" {
logrus.Debugf("unable to read from terminal: %s", ErrEmptyFilePath)
return nil, ErrEmptyFilePath
}

Expand Down
9 changes: 6 additions & 3 deletions challenge-01/cmd/ccwc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
c.Args = flag.CommandLine.Args()

if flag.NFlag() == 0 {
// Todo: add debug logs
logrus.Debugf("no flags provided, using defaults.")

opts[config.BYTES] = true
opts[config.LINES] = true
Expand All @@ -63,14 +63,17 @@ func main() {
return
}

fmt.Println(c.Result)
logrus.Info(c.Result)
}

func setLogger() {

// remove unwanted things and enforce colors
// remove unwanted things and enforce colors.
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableLevelTruncation: true,
})

// set log level to debug.
logrus.SetLevel(logrus.DebugLevel)
}
Loading