-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import modules from zenit repository
- Loading branch information
Nicola Strappazzon C
committed
Oct 8, 2023
1 parent
ade140f
commit 4d2e316
Showing
21 changed files
with
922 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package exec | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/debeando/go-common/cast" | ||
) | ||
|
||
func PGrep(cmd string) int64 { | ||
stdout, _ := Command(fmt.Sprintf("/usr/bin/pgrep -f '%s'", cmd)) | ||
|
||
return cast.StringToInt64(stdout) | ||
} | ||
|
||
func Command(cmd string) (stdout string, exitcode int) { | ||
out, err := exec.Command("/bin/bash", "-c", cmd).Output() | ||
|
||
if exitError, ok := err.(*exec.ExitError); ok { | ||
ws := exitError.Sys().(syscall.WaitStatus) | ||
exitcode = ws.ExitStatus() | ||
} | ||
|
||
stdout = string(out[:]) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package file_test | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/debeando/go-common/file" | ||
) | ||
|
||
var wd string | ||
|
||
func TestMain(m *testing.M) { | ||
wd, _ = os.Getwd() | ||
} | ||
|
||
func TestGetInt64FromFile(t *testing.T) { | ||
expected := int64(1234567890) | ||
result := common.GetInt64FromFile(wd + "/../assets/tests/int64.txt") | ||
|
||
if result != expected { | ||
t.Error("Expected: int64(1234567890)") | ||
} | ||
|
||
expected = int64(0) | ||
result = common.GetInt64FromFile(wd + "/../assets/tests/int64.log") | ||
|
||
if result != expected { | ||
t.Error("Expected: int64(0)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/debeando/go-common/log" | ||
) | ||
|
||
func Post(uri string, data string, headers map[string]string) int { | ||
req, err := http.NewRequest( | ||
"POST", | ||
uri, | ||
strings.NewReader(data), | ||
) | ||
if err != nil { | ||
log.ErrorWithFields("HTTP(s) Client", log.Fields{"error": err}) | ||
} | ||
|
||
for key, value := range headers { | ||
req.Header.Add(key, value) | ||
} | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.ErrorWithFields("HTTP(s) Client", log.Fields{"error": err}) | ||
return 520 // Status code 520 Unknown Error | ||
} | ||
defer resp.Body.Close() | ||
|
||
return resp.StatusCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package http_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
test "github.com/debeando/go-common/http" | ||
) | ||
|
||
func TestPost(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != "POST" { | ||
t.Errorf("Expected 'POST' request, got '%s'", r.Method) | ||
} | ||
|
||
body, _ := ioutil.ReadAll(r.Body) | ||
|
||
if string(body) != "foo" { | ||
t.Errorf("Expected request to 'foo', got '%s'", string(body)) | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer ts.Close() | ||
|
||
response := test.Post(ts.URL, "foo", nil) | ||
|
||
if response != 200 { | ||
t.Errorf("Expected: '200', got: '%v'.", response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package maps | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
func Keys(v []map[string]string) (keys []string) { | ||
if len(v) > 0 { | ||
for key := range v[0] { | ||
keys = append(keys, key) | ||
} | ||
sort.Strings(keys) | ||
} | ||
return | ||
} | ||
|
||
func In(key string, list map[string]string) bool { | ||
if _, ok := list[key]; ok { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package maps_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/debeando/go-common/maps" | ||
) | ||
|
||
func TestKeys(t *testing.T) { | ||
result := maps.Keys([]map[string]string{{"foo": "a", "bar": "1"}, {"foo": "b", "bar": "2"}}) | ||
expected := []string{"foo", "bar"} | ||
|
||
if !reflect.DeepEqual(result, expected) { | ||
t.Error("Expected: []string{\"foo\", \"bar\"}") | ||
} | ||
} | ||
|
||
func TestIn(t *testing.T) { | ||
expected := make(map[string]string) | ||
expected["test"] = "test" | ||
|
||
if !maps.In("test", expected) { | ||
t.Error("Expected: true") | ||
} | ||
|
||
if maps.In("foo", expected) { | ||
t.Error("Expected: false") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package math | ||
|
||
func Percentage(value int64, max uint64) float64 { | ||
v := float64(value) | ||
m := float64(max) | ||
if v >= 0 && m > 0 { | ||
return (v / m) * 100 | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.