Skip to content

Commit

Permalink
Merge pull request #1 from codescalersinternships/developmentC
Browse files Browse the repository at this point in the history
Unix Coreutils
  • Loading branch information
xmonader authored Jan 13, 2025
2 parents 2df9440 + 02db807 commit b3a8c49
Show file tree
Hide file tree
Showing 25 changed files with 675 additions and 1 deletion.
36 changes: 36 additions & 0 deletions .github/workflows/lint_format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Go CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
- name: Build
run: go build -v ./...


lint_and_format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.22'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.56.0
args: --timeout 3m
- name: Go Format
uses: Jerome1337/[email protected]
with:
gofmt-path: './src'
gofmt-flags: '-l -d'

32 changes: 32 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: goreleaser

on:
push:
tags:
- v*


jobs:
goreleaser:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: "true"

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21

- name: Run GoReleaser
uses: goreleaser/[email protected]
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

36 changes: 36 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
builds:
- main: ./cmd/cat
id: "cat"
binary: cat

- main: ./cmd/echo
id: "echo"
binary: echo

- main: ./cmd/env
id: "env"
binary: env

- main: ./cmd/false
id: "false"
binary: False_

- main: ./cmd/head
id: "head"
binary: head

- main: ./cmd/tail
id: "tail"
binary: tail

- main: ./cmd/tree
id: "tree"
binary: tree

- main: ./cmd/wc
id: "wc"
binary: wc

- main: ./cmd/yes
id: "yes"
binary: yes_
151 changes: 150 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,150 @@
# Coreutils-Rawan-Mostafa
# Unix Coreutils
## Description
This repo implements the Unix coreutils like `head` `tail` `cat` `echo` `wc` `tree` `yes` `false` `true` `env` in Golang

## Table of Contents

- <a href ="#head">head</a>
- <a href ="#tail">tail</a>
- <a href ="#cat">cat</a>
- <a href ="#echo">echo</a>
- <a href ="#wc">wc</a>
- <a href ="#yes">yes</a>
- <a href ="#true">true</a>
- <a href ="#false">false</a>
- <a href ="#env">env</a>


<hr style="background-color: #4b4c60"></hr>

<a id = "head"></a>

## 1- head
### Description
head outputs the first part of the file
### Flags
`-n=num` : print the first NUM lines instead of the first 10
### How to use
```
go run cmd/head/main.go [OPTIONS] [FILE]
```

<a id = "tail"></a>

## 2- tail
### Description
tail outputs the last part of the file
### Flags
`-n=NUM` : print the last NUM lines instead of the last 10
### How to use
```
go run cmd/tail/main.go [OPTIONS] [FILE]
```

<a id = "cat"></a>

## 3- cat
### Description
cat concatenate files and print on the standard output
With no FILE, or when FILE is -, read standard input.
### Flags
`-n` : number all the output lines
### How to use
```
go run cmd/cat/main.go [OPTIONS] [FILE]
```

<a id = "echo"></a>

## 4- echo
### Description
echo echoes the STRING(s) to standard output.
### Flags
`-n` : don't output the trailing newline
### How to use

```
go run cmd/echo/main.go [OPTIONS] [STRING]
```

<a id = "wc"></a>

## 5- wc

### Description
wc print newline, word, and byte counts for each file

### Flags
- `-c` : print the bytes counts
- `-m` : print the character counts
- `-l` : print the newline counts

### How to use

```
go run cmd/wc/main.go [OPTIONS] [FILE]
```

<a id = "tree"></a>

## 6- tree

### Description
tree prints the heirarchy of a given directory
If no directory given , prints that of the current directory
### Flags
`-L=LEVEL` : print the heirarchy down to LEVEL number of levels

### How to use

```
go run cmd/tree/main.go [OPTIONS] [FILE]
```

<a id = "yes"></a>

## 7- yes

### Description
yes repeatedly output a line with all specified STRING(s), or 'y'until killed

### How to use

```
go run cmd/yes/main.go [STRING]
```

<a id = "true"></a>

## 8- true

### Description
true do nothing, successfully, exit with a status code indicating success.

### How to use

```
go run cmd/true/main.go
```

## 9- false

### Description
true do nothing, unsuccessfully, exit with a status code indicating failure.

### How to use

```
go run cmd/false/main.go
```

## 10- env

### Description
env prints the resulting environment.

### How to use

```
go run cmd/env/main.go
```
31 changes: 31 additions & 0 deletions cmd/cat/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"bufio"
"flag"
"log"
"os"
"strings"

internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)

func main() {

var numbering bool
flag.BoolVar(&numbering, "n", false, "defines whether or not the lines will be ordered")
flag.Parse()

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)))
}
err := internal.Cat(scanner, numbering)
if err != nil {
log.Fatal("Error in scanning std input")
}
}
16 changes: 16 additions & 0 deletions cmd/echo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"flag"

internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)

func main() {
var noNewLine bool

flag.BoolVar(&noNewLine, "n", false, "defines whether or not to add a new line")
flag.Parse()

internal.Echo(noNewLine, flag.Args())
}
9 changes: 9 additions & 0 deletions cmd/env/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)

func main() {
internal.Env()
}
9 changes: 9 additions & 0 deletions cmd/false/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)

func main() {
internal.False()
}
34 changes: 34 additions & 0 deletions cmd/head/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"bufio"
"flag"
"log"
"os"

internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)

func main() {

var n int

flag.IntVar(&n, "n", 10, "defines the number fo lines to be printed")
flag.Parse()
if len(flag.Args()) == 0 {
log.Fatal("No file argument passed to the command")
}

filePath := flag.Args()[0]
file, err := os.Open(filePath)
if err != nil {
log.Fatal("Error in opening the file")
}
defer file.Close()
scanner := bufio.NewScanner(file)

err = internal.Head(scanner, n)
if err != nil {
log.Fatal("Error in scanning std input")
}
}
Loading

0 comments on commit b3a8c49

Please sign in to comment.