Skip to content

Commit

Permalink
Applying 2nd round of review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Sep 8, 2024
1 parent 124d569 commit d9a00ae
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 48 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/lint_format.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
name: Go CI

on:
push:
branches: [ "main" ]

pull_request:
branches: [ "main" ]

on: [push, pull_request]

jobs:
build:
Expand All @@ -22,9 +16,7 @@ jobs:


lint_and_format:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
Expand All @@ -37,5 +29,8 @@ jobs:
version: v1.56.0
args: --timeout 3m
- name: Go Format
run: gofmt -s -w . && git diff --exit-code
uses: Jerome1337/[email protected]
with:
gofmt-path: './src'
gofmt-flags: '-l -d'

13 changes: 6 additions & 7 deletions cmd/cat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ func main() {
flag.BoolVar(&numbering, "n", false, "defines whether or not the lines will be ordered")
flag.Parse()

var scanner *bufio.Scanner

if len(flag.Args()) == 0 || flag.Args()[0] == "-" {
scanner = bufio.NewScanner(os.Stdin)
} else {
scanner := bufio.NewScanner(os.Stdin)
if len(flag.Args()) > 0 && flag.Args()[0] != "-" {
data, err := os.ReadFile(flag.Args()[0])
if err != nil {
log.Fatal("Error in reading the file")
}

scanner = bufio.NewScanner(strings.NewReader(string(data)))
}
internal.Cat(scanner, numbering)
err := internal.Cat(scanner, numbering)
if err != nil {
log.Fatal("Error in scanning std input")
}
}
5 changes: 4 additions & 1 deletion cmd/head/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ func main() {

scanner := bufio.NewScanner(strings.NewReader(string(data)))

internal.Head(scanner, n)
err = internal.Head(scanner, n)
if err != nil {
log.Fatal("Error in scanning std input")
}
}
5 changes: 4 additions & 1 deletion cmd/tail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ func main() {

scanner := bufio.NewScanner(strings.NewReader(string(data)))

internal.Tail(scanner, n)
err = internal.Tail(scanner, n)
if err != nil {
log.Fatal("Error in scanning std input")
}
}
5 changes: 4 additions & 1 deletion cmd/wc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func main() {
isChars = true
}

internal.Wc(scanner, isLines, isWords, isChars)
err = internal.Wc(scanner, isLines, isWords, isChars)
if err != nil {
log.Fatal("Error in scanning std input")
}

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/codescalersinternships/Coreutils-Rawan-Mostafa

go 1.22.3
go 1.18
7 changes: 2 additions & 5 deletions internal/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package internal
import (
"bufio"
"fmt"
"log"
)

func Cat(scanner *bufio.Scanner, numbering bool) {
func Cat(scanner *bufio.Scanner, numbering bool) error {

scanner.Split(bufio.ScanLines)
count := 0
Expand All @@ -17,8 +16,6 @@ func Cat(scanner *bufio.Scanner, numbering bool) {
} else {
fmt.Printf("%s\n", string(scanner.Bytes()))
}
if scanner.Err() != nil {
log.Fatal("Error in scanning std input")
}
}
return scanner.Err()
}
6 changes: 3 additions & 3 deletions internal/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package internal

import (
"fmt"
"strings"
)

func Echo(noNewLine bool, args []string) {

for _, arg := range args {
fmt.Printf("%s ", arg)
}
argString := strings.Join(args, " ")
fmt.Print(argString)
if !noNewLine {
fmt.Print("\n")
}
Expand Down
7 changes: 2 additions & 5 deletions internal/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package internal
import (
"bufio"
"fmt"
"log"
)

func Head(scanner *bufio.Scanner, n int) {
func Head(scanner *bufio.Scanner, n int) error {

scanner.Split(bufio.ScanLines)
count := 0
Expand All @@ -16,7 +15,5 @@ func Head(scanner *bufio.Scanner, n int) {
fmt.Printf("%s \n", string(scanner.Bytes()))
}
}
if scanner.Err() != nil {
log.Fatal("Error in scanning std input")
}
return scanner.Err()
}
9 changes: 3 additions & 6 deletions internal/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package internal
import (
"bufio"
"fmt"
"log"
)

func Tail(scanner *bufio.Scanner, n int) {
func Tail(scanner *bufio.Scanner, n int) error {

scanner.Split(bufio.ScanLines)
count := 0
Expand All @@ -15,14 +14,12 @@ func Tail(scanner *bufio.Scanner, n int) {
count++
lines = append(lines, string(scanner.Bytes()))
}
if scanner.Err() != nil {
log.Fatal("Error in scanning std input")
}

if n >= len(lines) {
n = len(lines)
}
for i := len(lines) - n; i < len(lines); i++ {
fmt.Println(lines[i])
}

return scanner.Err()
}
8 changes: 3 additions & 5 deletions internal/wc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package internal
import (
"bufio"
"fmt"
"log"
)

func Wc(scanner *bufio.Scanner, isLines bool, isWords bool, isChars bool) {
func Wc(scanner *bufio.Scanner, isLines bool, isWords bool, isChars bool) error {

var linesCount int
var wordsCount int
Expand All @@ -21,9 +20,7 @@ func Wc(scanner *bufio.Scanner, isLines bool, isWords bool, isChars bool) {
if char == ' ' || char == '\n' || char == '\t' {
wordsCount++
}
if scanner.Err() != nil {
log.Fatal("Error in scanning std input")
}

}
wordsCount += 1
charsCount += 1
Expand All @@ -39,4 +36,5 @@ func Wc(scanner *bufio.Scanner, isLines bool, isWords bool, isChars bool) {
fmt.Printf("%d ", charsCount)
}
fmt.Print("\n")
return scanner.Err()
}
12 changes: 9 additions & 3 deletions internal/yes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
)

func Yes(inputs []string) {
input := strings.Join(inputs, " ")
for {
fmt.Println(input)
if len(inputs) > 0 {
input := strings.Join(inputs, " ")
for {
fmt.Println(input)
}
} else {
for {
fmt.Println("y")
}
}
}

0 comments on commit d9a00ae

Please sign in to comment.