Skip to content

Commit 955a8c4

Browse files
authored
Merge pull request #8 from leozz37/develop
Develop
2 parents e10f255 + faa5602 commit 955a8c4

File tree

10 files changed

+152
-6
lines changed

10 files changed

+152
-6
lines changed

β€Ž.github/workflows/go.ymlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
run: go mod download
1717

1818
- name: Test
19-
run: go test -v -covermode=count -coverprofile=coverage.out
19+
run: go test -v -covermode=count -coverprofile=coverage.out ./hare
2020

2121
- name: Upload coverage
2222
run: bash <(curl -s https://codecov.io/bash)

β€Ž.gitignoreβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
*hare-cli
78

89
# Test binary, built with `go test -c`
910
*.test

β€ŽMakefileβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
APP_NAME=hare-cli
2+
3+
build:
4+
@go build -o ./$(APP_NAME) cmd/cli/cli.go
5+
6+
install:
7+
@go build -o $(GOPATH)/bin/hare cmd/cli/cli.go
8+
9+
uninstall:
10+
@rm $(GOPATH)/bin/hare
11+
12+
deps:
13+
@go mod download
14+
15+
test:
16+
@go test ./...
17+
18+
test-coverage:
19+
@go test -v -covermode=count -coverprofile=coverage.out ./...

β€ŽREADME.mdβ€Ž

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[![Release](https://img.shields.io/github/v/release/leozz37/hare)](https://github.com/leozz37/hare/releases)
1414
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
1515

16-
Hare is a user-friendly lib for sockets in Golang. You can send and listen to TCP connections with a few lines of code.
16+
Hare is a user-friendly lib for sockets in Golang and a CLI tool for interaction with sockets. You can send and listen to TCP connections with a few lines of code or commands.
1717

1818
## Contents
1919

@@ -27,6 +27,26 @@ Hare is a user-friendly lib for sockets in Golang. You can send and listen to TC
2727

2828
## πŸ–₯️ Installation
2929

30+
Installation guid for the CLI Tool and the Golang Library.
31+
32+
### πŸ’» CLI Tool
33+
34+
To install the CLI tool, you can install it through [Homebrew](https://brew.sh/):
35+
36+
```shell
37+
$ brew tap leozz37/hare
38+
39+
$ brew install hare
40+
```
41+
42+
Or you can install manually with the `Makefile` script:
43+
44+
```shell
45+
$ make install
46+
```
47+
48+
### πŸ‡ Golang Lib
49+
3050
First, you need [Go](https://golang.org/) installed (version 1.12+ is required), then you can install Hare:
3151

3252
```shell
@@ -39,15 +59,46 @@ Import it in your code:
3959
import "github.com/leozz37/hare"
4060
```
4161

42-
(Optional) install [jaguar](https://github.com/leozz37/jaguar) for testing the sockets:
62+
## πŸ• Quickstart
63+
64+
Quick start for the CLI Tool and the Golang Library.
65+
66+
### πŸ’» CLI Tool
67+
68+
To use the CLI tool, these are the flags:
69+
70+
```
71+
-d string
72+
Data to be sended
73+
-h string
74+
Host address to bo operated (default "localhost")
75+
-l Listen to a given address
76+
-p string
77+
Port address to bo operated [REQUIRED]
78+
-s Send message to a given address
79+
```
80+
81+
You can run the `--help` flag:
82+
83+
```shell
84+
$ hare --help
85+
```
86+
87+
To `Listen` to port `3000` for example, run:
4388

4489
```shell
45-
$ brew tap leozz37/jaguar
90+
$ hare -l -p 3000
91+
```
92+
93+
To `Send` a payload with the message `Hello World` to port `3000` for example, run:
4694

47-
$ brew install jaguar
95+
```shell
96+
$ hare -s -p 3000 -d 'Hello World'
4897
```
4998

50-
## πŸ• Quickstart
99+
![cli-example](resources/images/cli-example.png)
100+
101+
### πŸ‡ Golang Lib
51102

52103
[Sample code](./examples/send.go) for sending payloads:
53104

β€Žcmd/cli/cli.goβ€Ž

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
7+
"github.com/fatih/color"
8+
"github.com/leozz37/hare/hare"
9+
)
10+
11+
func main() {
12+
sendPtr := flag.Bool("s", false, "Send message to a given address")
13+
listenPtr := flag.Bool("l", false, "Listen to a given address")
14+
portPtr := flag.String("p", "", "Port address to bo operated")
15+
hostPtr := flag.String("h", "localhost", "Host address to bo operated")
16+
dataPtr := flag.String("d", "", "Data to be sended")
17+
18+
flag.Parse()
19+
if *sendPtr && *listenPtr {
20+
flag.PrintDefaults()
21+
fmt.Errorf("You can only listen OR send to a port")
22+
}
23+
if *portPtr == "" {
24+
flag.PrintDefaults()
25+
fmt.Errorf("Flag PORT is required.")
26+
}
27+
28+
if *listenPtr {
29+
err := listenPort(*portPtr, *hostPtr)
30+
if err != nil {
31+
fmt.Errorf("Could listen to address")
32+
}
33+
}
34+
if *sendPtr {
35+
err := sendMessage(*portPtr, *hostPtr, *dataPtr)
36+
if err != nil {
37+
fmt.Errorf("Could send to address")
38+
}
39+
}
40+
}
41+
42+
func listenPort(port, host string) error {
43+
red := color.New(color.FgRed).SprintFunc()
44+
fmt.Printf("Listening to port: %s\n", red(port))
45+
46+
r, err := hare.Listen(port)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
for {
52+
if r.HasNewMessages() {
53+
fmt.Println(r.GetMessage())
54+
}
55+
}
56+
}
57+
58+
func sendMessage(port, host, data string) error {
59+
err := hare.Send(port, data)
60+
if err != nil {
61+
panic(err)
62+
}
63+
return nil
64+
}

β€Žgo.modβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/leozz37/hare
22

33
go 1.15
4+
5+
require github.com/fatih/color v1.12.0 // indirect

β€Žgo.sumβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
2+
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
3+
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
4+
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
5+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
6+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
7+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
9+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
File renamed without changes.
File renamed without changes.
35.2 KB
Loading

0 commit comments

Comments
Β (0)