Compare commits

...

21 Commits
v0.3.2 ... main

Author SHA1 Message Date
github-actions[bot] 43c38d47d1 go mod tidy 2022-11-20 18:01:42 +00:00
renovate[bot] 1345795c05 fix(deps): update module github.com/sirupsen/logrus to v1.9.0 2022-11-20 18:01:18 +00:00
github-actions[bot] 5d197234fd gofmt 2021-10-24 12:51:17 +00:00
Oskar 1cb931d9ef
chore: new tests
* Create bytes_test.go
* Create cpu_test.go
* Create memory_test.go
* Create uptime_test.go
2021-10-24 14:50:52 +02:00
Oskar df1b532e2a
Create codeql-analysis.yml 2021-10-24 14:11:36 +02:00
Oskar a557f452a7
feat(log): SetReportCaller to true
example output:
INFO[0000]/home/trex/go/src/awesomeProject/main.go:11 main.main() hello world  

from https://stackoverflow.com/a/68301267
2021-10-21 23:18:24 +02:00
Oskar b6fc96d90a
ci: update workflows (#3)
* Update ci.yml
* Delete auto-fix.yml
2021-10-21 23:09:46 +02:00
Medzik 857ddb66b0 feat(common): add retry function 2021-09-04 11:37:07 +00:00
Medzik 2006d90d17 update 2021-08-22 12:16:39 +02:00
Oskar ce20c581e9
Update auto-fix.yml 2021-08-20 22:43:02 +02:00
Medzik b4526de784 update workflows 2021-08-19 18:18:31 +00:00
Medzik 43d2cb010f git pull 2021-08-19 18:15:13 +00:00
Medzik d6dff6dea0 add readme 2021-08-19 18:14:37 +00:00
github-actions[bot] c55208e624 gofmt 2021-08-19 15:19:45 +00:00
Medzik ddd6e60ada update 2021-08-19 15:18:49 +00:00
Medzik 8f783c9193 update 2021-08-19 15:17:58 +00:00
Medzik f7f080b647 update workflows 2021-08-19 14:58:06 +00:00
github-actions[bot] 9ac45c7a3a gofmt 2021-08-19 14:56:19 +00:00
Medzik 7cc987e305 update workflows 2021-08-19 14:52:26 +00:00
Oskar b3c926d9d6
fix: delete pid (unused value) 2021-08-18 23:21:21 +02:00
MedzikUser 2affef3083 feat(testing feature): [updater] ignore or auto update major 2021-08-15 21:33:53 +02:00
24 changed files with 269 additions and 41 deletions

69
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,69 @@
name: CI
on:
push:
branches:
- main
pull_request:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Repo clone (go mod tidy / gofmt)
if: github.event_name != 'pull_request'
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Setup Git
if: github.event_name != 'pull_request'
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
- name: Tidy
if: github.event_name != 'pull_request'
run: |
go mod tidy
git add .
git diff-index --quiet HEAD || git commit -m "go mod tidy"
- name: GoFmt
if: github.event_name != 'pull_request'
run: |
go fmt ./...
git add .
git diff-index --quiet HEAD || git commit -m "gofmt"
- name: Pull changes
if: github.event_name != 'pull_request'
run: git pull -r
- name: Push changes
if: github.event_name != 'pull_request'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
- name: Repo clone (test)
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Test
run: go test -v ./...
- name: Semantic Release
if: github.event_name != 'pull_request'
uses: go-semantic-release/action@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

45
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@ -0,0 +1,45 @@
name: "CodeQL"
on:
push:
branches:
pull_request:
branches:
schedule:
- cron: '42 7 * * 0'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
steps:
- name: Clone repository
uses: actions/checkout@v2
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

View File

@ -1,19 +0,0 @@
name: Test
on:
push:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Test
run: go test -v ./...

1
.gitignore vendored
View File

@ -8,7 +8,6 @@
# Binaries
*.exe
*.exe~
*.out
*.test
/dist/

11
README.md Normal file
View File

@ -0,0 +1,11 @@
# Go Utils
## 🗃 Install
```go
go get github.com/MedzikUser/go-utils
```
## 🗒 Docs
Click [here](https://pkg.go.dev/github.com/MedzikUser/go-utils)

View File

@ -6,6 +6,7 @@ import (
/*
This function checks for an error.
If the error isn't nil it return true and print error otherwise false.
err := errors.New("Test Error")

View File

@ -25,3 +25,7 @@ var Log = &logrus.Logger{
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
func init() {
Log.SetReportCaller(true)
}

37
common/retry.go Normal file
View File

@ -0,0 +1,37 @@
package common
import (
"fmt"
"time"
)
/*
If there is an error in the performed function, it will be restarted, this process will be repeated the given number of times
e.g.
err := Retry(3, 5*time.Second, connectToDB)
if err != nil {
Log.Error(err)
}
*/
func Retry(attempts int, sleep time.Duration, f func() error) (err error) {
for i := 0; ; i++ {
err = f()
if err == nil {
return
}
if i >= (attempts - 1) {
break
}
time.Sleep(sleep)
sleep *= 2
Log.Error("Retrying after error: ", err)
}
return fmt.Errorf("after %d attempts, last error: %s", attempts, err)
}

View File

@ -6,6 +6,7 @@ import (
"github.com/MedzikUser/go-utils/utils"
)
// Return uptime of program e.g. "10m 4s"
func Uptime(start time.Time) string {
return utils.FormatSeconds(time.Since(start))
}

12
common/uptime_test.go Normal file
View File

@ -0,0 +1,12 @@
package common
import (
"fmt"
"testing"
"time"
)
func TestCheckUptime(T *testing.T) {
uptime := Uptime(time.Date(2016, 6, 2, 1, 1, 1, 1, time.UTC))
fmt.Println(uptime)
}

5
go.mod
View File

@ -5,8 +5,7 @@ go 1.16
require (
github.com/MedzikUser/go-github-selfupdate v1.3.1
github.com/blang/semver/v4 v4.0.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.0
github.com/struCoder/pidusage v0.2.1
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)

15
go.sum
View File

@ -24,12 +24,11 @@ github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/struCoder/pidusage v0.2.1 h1:dFiEgUDkubeIj0XA1NpQ6+8LQmKrLi7NiIQl86E6BoY=
github.com/struCoder/pidusage v0.2.1/go.mod h1:bewtP2KUA1TBUyza5+/PCpSQ6sc/H6jJbIKAzqW86BA=
github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
@ -51,8 +50,8 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
@ -68,3 +67,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

3
main.go Normal file
View File

@ -0,0 +1,3 @@
package main
func main() {}

View File

@ -12,12 +12,14 @@ import (
type StatCPU struct {
// Percent of CPU Usage
Usage string
// Number of CPUs
// NumCPU returns the number of logical CPUs usable by the current process.
//
// The set of available CPUs is checked by querying the operating system at process startup. Changes to operating system CPU allocation after process startup are not reflected.
Num int
// CPU Arch
// GOARCH is the running program's architecture target: one of 386, amd64, arm, s390x, and so on.
Arch string
// Process ID
pid int
PID int
}
// Get CPU stats
@ -25,9 +27,9 @@ func CPU() (StatCPU, error) {
pid := os.Getpid()
stat := StatCPU{
Num: runtime.NumCPU(),
Arch: runtime.GOARCH,
pid: pid,
Num: runtime.NumCPU(),
Arch: runtime.GOARCH,
PID: pid,
}
sysInfo, err := pidusage.GetStat(pid)

10
stats/cpu_test.go Normal file
View File

@ -0,0 +1,10 @@
package stats
import "testing"
func TestCPU(t *testing.T) {
_, err := CPU()
if err != nil {
t.Fatal(err)
}
}

View File

@ -7,10 +7,18 @@ import (
)
type StatMemory struct {
Alloc string
// Alloc is bytes of allocated heap objects.
Alloc string
// TotalAlloc is cumulative bytes allocated for heap objects.
//
// TotalAlloc increases as heap objects are allocated, but unlike Alloc and HeapAlloc, it does not decrease when objects are freed.
TotalAlloc string
Sys string
NumGC uint32
// Sys is the total bytes of memory obtained from the OS.
//
// Sys is the sum of the XSys fields below. Sys measures the virtual address space reserved by the Go runtime for the heap, stacks, and other internal data structures. It's likely that not all of the virtual address space is backed by physical memory at any given moment, though in general it all was at some point.
Sys string
// NumGC is the number of completed GC cycles.
NumGC uint32
}
// Get Memory stats

7
stats/memory_test.go Normal file
View File

@ -0,0 +1,7 @@
package stats
import "testing"
func TestMemory(t *testing.T) {
Memory()
}

View File

@ -14,7 +14,7 @@ import (
func (c *Client) AutoUpdater() {
// Check on start
err := c.Update()
common.CheckErr(err, "AutoUpdater")
common.CheckErr(err, "Auto Updater")
ticker := time.NewTicker(c.CheckEvery)
@ -24,7 +24,7 @@ func (c *Client) AutoUpdater() {
select {
case <-ticker.C:
err := c.Update()
common.CheckErr(err, "AutoUpdater")
common.CheckErr(err, "Auto Updater")
case <-quit:
ticker.Stop()

View File

@ -20,4 +20,9 @@ type Client struct {
}
*/
AfterUpdate func()
// Update Rules
// Should I update the major? (X.y.z)
Major bool
}

View File

@ -1,6 +1,7 @@
package updater
import (
"errors"
"os"
"github.com/MedzikUser/go-github-selfupdate/selfupdate"
@ -16,6 +17,9 @@ func (c *Client) Update() error {
updater, err := selfupdate.NewUpdater(selfupdate.Config{
APIToken: c.GitHubToken,
})
if err != nil {
return err
}
release, found, err := updater.DetectLatest(c.GitHub)
if err != nil {
@ -23,10 +27,15 @@ func (c *Client) Update() error {
}
version, err := semver.Parse(c.Version)
if err != nil || !found || release.Version.LTE(version) {
return err
}
if !c.Major && release.Version.Major > version.Major {
return errors.New("major update")
}
exe, err := os.Executable()
if err != nil {
return err

View File

@ -2,6 +2,11 @@ package utils
import "fmt"
/*
Format bytes e.g.
Bytes(53) // out: "53 B"
Bytes(1522) // out: "1.5 kB"
*/
func Bytes(b uint64) string {
const unit = 1000
if b < unit {

17
utils/bytes_test.go Normal file
View File

@ -0,0 +1,17 @@
package utils
import "testing"
func TestBytes(t *testing.T) {
// 1
r := Bytes(53)
if r != "53 B" {
t.Fatal(r)
}
// 2
r = Bytes(1522)
if r != "1.5 kB" {
t.Fatal(r)
}
}

View File

@ -1,5 +1,6 @@
package utils
// Math round down
func Round(val float64) int {
if val < 0 {
return int(val - 1.0)

View File

@ -5,6 +5,7 @@ import (
"time"
)
// Format Seconds to days, hours, minutes and seconds example out. "10d 3m 59s"
func FormatSeconds(s time.Duration) string {
hours := Round(s.Hours())
minutes := Round(s.Minutes())