增加lint脚本

This commit is contained in:
lwch
2022-08-03 17:24:34 +08:00
parent d67a579bce
commit c3ced80ba6
10 changed files with 62 additions and 7 deletions

40
.github/workflows/lint.yaml vendored Normal file
View File

@@ -0,0 +1,40 @@
name: libagent
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
- ubuntu-22.04
- windows-2019
- windows-2022
- macos-10.15
- macos-11
- macos-12
go:
- '1.17'
- '1.18'
- '1.19'
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- name: Lint
run: |
go install golang.org/x/lint/golint@latest
golint -set_exit_status ./...
go install github.com/gordonklaus/ineffassign@latest
ineffassign ./...

View File

@@ -1,2 +1,7 @@
# libagent
agent封装类库用于快速开发agent
## 开发方式
待补充

View File

@@ -12,6 +12,8 @@ import (
runtime "github.com/jkstack/jkframe/utils"
)
// RewriteServer rewrite server configure
// - use $... to set value by envionment variables
func (cfg *Configure) RewriteServer() {
cfg.Server = strings.TrimSpace(cfg.Server)
if len(cfg.Server) == 0 {
@@ -28,6 +30,10 @@ func (cfg *Configure) RewriteServer() {
}
}
// RewriteID rewrite agent id configure
// - use $IP to set value by ip address in interface to connect server
// - use $HOSTNAME to set value by hostname
// - use $... to set value by envionment variables
func (cfg *Configure) RewriteID() {
cfg.RewriteServer()
cfg.ID = strings.TrimSpace(cfg.ID)
@@ -58,6 +64,7 @@ func (cfg *Configure) RewriteID() {
logging.Info("now agent id is %s", cfg.ID)
}
// SetAgentID reset agent id by value
func (cfg *Configure) SetAgentID(id string) {
cfg.ID = id
}

View File

@@ -8,7 +8,7 @@ import (
func TestCGroups(t *testing.T) {
var cfg Configure
cfg.CpuQuota = 100 // 1core
cfg.CPUQuota = 100 // 1core
cfg.Memory = utils.Bytes(100 * 1024 * 1024) // 100MB
cfg.Disks = append(cfg.Disks, DiskLimit{
Dev: "8:0",

View File

@@ -25,7 +25,7 @@ func (cfg *Configure) doV1(agentName string) {
logging.Warning("can not create cgroup %s: %v", dir, err)
return
}
limitCPUV1(group, cfg.CpuQuota)
limitCPUV1(group, cfg.CPUQuota)
limitMemoryV1(group, int64(cfg.Memory))
limitDiskV1(group, cfg.Disks)
err = group.Add(cgroups.Process{
@@ -38,7 +38,7 @@ func (cfg *Configure) doV1(agentName string) {
}
func wantCGroup(cfg *Configure) bool {
if cfg.CpuQuota > 0 {
if cfg.CPUQuota > 0 {
return true
}
if cfg.Memory > 0 {

View File

@@ -18,7 +18,7 @@ func (cfg *Configure) doV2(agentName string) {
logging.Warning("can not create cgroup: %v", err)
return
}
limitCPUV2(group, cfg.CpuQuota)
limitCPUV2(group, cfg.CPUQuota)
limitMemoryV2(group, int64(cfg.Memory))
limitDiskV2(group, cfg.Disks)
err = group.AddProc(uint64(os.Getpid()))

View File

@@ -7,8 +7,9 @@ import (
"github.com/jkstack/agent/utils"
)
// DiskLimit disk limit configure
type DiskLimit struct {
// device version of lsblk command eg: 8:0
// device number of lsblk command eg: 8:0
Dev string `json:"dev" yaml:"dev" kv:"dev"`
// read bytes
ReadBytes uint64 `json:"read_bytes" yaml:"read_bytes" kv:"read_bytes"`
@@ -23,7 +24,7 @@ type DiskLimit struct {
// Configure limit configure
type Configure struct {
// cpu usage 100 means 1 core
CpuQuota int64 `json:"cpu_quota" yaml:"cpu_quota" kv:"cpu_quota"`
CPUQuota int64 `json:"cpu_quota" yaml:"cpu_quota" kv:"cpu_quota"`
// memory size limit in bytes
Memory utils.Bytes `json:"memory_limit" yaml:"memory_limit" kv:"memory_limit"`
// limit of disk

View File

@@ -7,7 +7,7 @@ import (
"gopkg.in/yaml.v3"
)
// Bytes yaml bytes
// Bytes custom bytes struct
type Bytes uint64
// MarshalJSON marshal bytes by json

View File

@@ -4,6 +4,7 @@ import (
"time"
)
// Duration custom duration struct
type Duration time.Duration
// MarshalKV marshal duration

View File

@@ -2,6 +2,7 @@ package utils
import "github.com/jkstack/jkframe/logging"
// Recover error recover
func Recover(key string) {
if err := recover(); err != nil {
logging.Error("%s: %v", key, err)