diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..d207b1802 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.go text eol=lf diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml new file mode 100644 index 000000000..356d9b401 --- /dev/null +++ b/.github/workflows/build-linux.yml @@ -0,0 +1,77 @@ +name: Build(Linux packages) + +on: + push: + branches: + - master + - refactor-b # temp + tags: + - v* + pull_request: + +jobs: + build-linux: + name: Build (Unix-like OSes) + uses: ./.github/workflows/go-build.yml + with: + os-version: ubuntu-latest + run: | + sudo apt-get update + sudo apt-get install -y devscripts debhelper fakeroot binutils-mips-linux-gnu binutils-aarch64-linux-gnu binutils-arm-linux-gnueabihf + docker pull mackerel/docker-mackerel-rpm-builder:c7 + make rpm deb rpm-kcps deb-kcps rpm-stage deb-stage tgz + make crossbuild + upload-artifact-path: | + rpmbuild/RPMS/*/*.rpm + packaging/*.deb + snapshot/*.zip + snapshot/*.tar.gz + build/*.tar.gz + build-windows-x64: + name: Build (64bit Windows) + uses: ./.github/workflows/go-build.yml + with: + os-version: windows-2022 + architecture: amd64 + with-cgo: true + run: | + commit=$(git rev-parse --short HEAD) + mkdir build/ + go build -o build/mackerel-agent.exe -ldflags="-X main.gitcommit=$commit" github.com/mackerelio/mackerel-agent + go build -o build/mackerel-agent-kcps.exe -ldflags="-X main.gitcommit=$commit -X github.com/mackerelio/mackerel-agent/config.apibase=http://198.18.0.16" github.com/mackerelio/mackerel-agent + + cd wix + for p in $(./pluginlist.sh) + do + name=$(basename "$p") + go build -o "../build/$name.exe" "$p" + done + go build -o ../build/wrapper.exe wrapper/wrapper_windows.go wrapper/install.go + go build -o ../build/replace.exe replace/replace_windows.go replace/shell_windows.go + go build -o ../build/generate_wxs.exe generate_wxs/generate_wxs.go + upload-artifact-name: windows-build-artifacts-x64 + upload-artifact-path: build/ + build-windows-x86: + name: Build (32bit Windows) + uses: ./.github/workflows/go-build.yml + with: + os-version: windows-2022 + architecture: '386' + with-cgo: true + run: | + commit=$(git rev-parse --short HEAD) + mkdir build/ + go build -o build/mackerel-agent.exe -ldflags="-X main.gitcommit=$commit" github.com/mackerelio/mackerel-agent + go build -o build/mackerel-agent-kcps.exe -ldflags="-X main.gitcommit=$commit -X github.com/mackerelio/mackerel-agent/config.apibase=http://198.18.0.16" github.com/mackerelio/mackerel-agent + + cd wix + for p in $(./pluginlist.sh) + do + name=$(basename "$p") + go build -o "../build/$name.exe" "$p" + done + go build -o ../build/wrapper.exe wrapper/wrapper_windows.go wrapper/install.go + go build -o ../build/replace.exe replace/replace_windows.go replace/shell_windows.go + go build -o ../build/generate_wxs.exe generate_wxs/generate_wxs.go + upload-artifact-name: windows-build-artifacts-x86 + upload-artifact-path: build/ diff --git a/.github/workflows/go-build.yml b/.github/workflows/go-build.yml new file mode 100644 index 000000000..1dadc80e7 --- /dev/null +++ b/.github/workflows/go-build.yml @@ -0,0 +1,99 @@ +name: Build(Go) +run-name: Build (${{ inputs.os-version }}/${{ inputs.go-version }}) + +on: + workflow_call: + inputs: + os-version: + description: 'GitHub-hosted runnners' + default: ubuntu-22.04 + required: false + type: string + go-version: + description: 'target Go versions' + default: 1.22.x + required: false + type: string + architecture: + description: 'Target architecture for Go to use (GOARCH)' + default: '' + required: false + type: string + platform: + description: 'Target operating system for Go to use (GOOS)' + default: '' + required: false + type: string + with-cgo: + description: 'xx (CGO_ENABLED)' + default: false + type: boolean + run: + description: 'The commands to run in bash' + default: go build + required: false + type: string + upload-artifact-path: + description: > + A file, directory or wildcard pattern that describes what to upload. + If empty, the workflow will upload no artifacts. + default: '' + required: false + type: string + upload-artifact-name: + description: 'Artifact name' + default: artifact + required: false + type: string + +env: + DEBIAN_FRONTEND: noninteractive +jobs: + run: + runs-on: ${{ inputs.os-version }} + env: + CGO_ENABLED: ${{ inputs.with-cgo && 1 || 0 }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: ${{ inputs.go-version }} + cache: true + - name: Setup environment variables + id: spec + run: | + GOHOSTARCH="$(go env GOHOSTARCH)" + GOARCH="${INPUT_ARCHITECTURE:-$GOHOSTARCH}" + echo "GOARCH=$GOARCH" >>"$GITHUB_ENV" + + GOHOSTOS="$(go env GOHOSTOS)" + GOOS="${INPUT_PLATFORM:-$GOHOSTOS}" + echo "GOOS=$GOOS" >>"$GITHUB_ENV" + + if [[ $GOHOSTOS = windows && $CGO_ENABLED = 1 ]] + then + case "$GOARCH" in + *64) + echo 'MSYS=MINGW64' >>"$GITHUB_ENV" ;; + *) + echo 'MSYS=MINGW32' >>"$GITHUB_ENV" ;; + esac + fi + shell: bash + - if: env.MSYS != '' + uses: msys2/setup-msys2@v2 + with: + msystem: ${{ env.MSYS }} + path-type: inherit + install: mingw-w64-i686-gcc + - if: env.MSYS == '' + run: ${{ inputs.run }} + shell: bash + - if: env.MSYS != '' + run: ${{ inputs.run }} + shell: msys2 {0} + - if: inputs.upload-artifact-path != '' + uses: actions/upload-artifact@v4 + with: + name: ${{ inputs.upload-artifact-name }} + path: ${{ inputs.upload-artifact-path }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 26e4d743c..700c12f75 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -8,57 +8,23 @@ on: - v* jobs: - test-linux: - name: Test (Linux) - runs-on: ubuntu-latest - steps: - - name: Set up Go 1.x - uses: actions/setup-go@v5 - with: - go-version: 1.20.x - - name: Check out code into the Go module directory - uses: actions/checkout@v4 - - uses: golangci/golangci-lint-action@v3 - - run: make convention - - run: make cover - - run: test "$(gofmt -l . | wc -l)" = 0 - - uses: shogo82148/actions-goveralls@v1 - with: - path-to-profile: .profile.cov - - test-windows: - name: Test (Windows) - # Please do not specify `windows-latest` - # Build process is complex then need to check operation. - runs-on: windows-2022 - steps: - - uses: actions/checkout@v4 - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: 1.20.x - - - uses: golangci/golangci-lint-action@v3 - with: - args: --timeout 5m - - uses: actions/cache@v3 - with: - path: | - ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**\go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - - name: Run Test - run: | - go test -short ./... - shell: bash + lint: + uses: mackerelio/workflows/.github/workflows/go-lint.yml@v1.1.0 + convention: + uses: mackerelio/workflows/.github/workflows/setup-go-matrix.yml@v1.1.0 + with: + run: make convention + test: + uses: mackerelio/workflows/.github/workflows/go-test.yml@v1.1.0 + with: + # Please do not specify `windows-latest` + # Build process is complex then need to check operation. + os-versions: '["ubuntu-latest", "windows-2022"]' build-linux: name: Build (Linux) runs-on: ubuntu-latest - needs: test-linux + needs: test if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') env: DEBIAN_FRONTEND: noninteractive @@ -95,7 +61,7 @@ jobs: build-windows: name: Build (Windows) runs-on: windows-2022 - needs: test-windows + needs: test if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') strategy: matrix: diff --git a/.gitignore b/.gitignore index 43868cb9c..65ea65feb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ !.circleci !.dependabot !.github +!.gitattributes !.gitignore !.goxc.json !.travis.yml diff --git a/.golangci.yml b/.golangci.yml index 571d18bff..ed7b28d12 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,3 +1,9 @@ +linters: + enable: + - gofmt +linters-settings: + gofmt: + simplify: true issues: exclude-rules: - path: _test\.go diff --git a/command/command_test.go b/command/command_test.go index 4899da840..1de72cec9 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -240,10 +240,10 @@ func TestCollectHostParamWithChecks(t *testing.T) { customIdentifier := "app.example.com" conf := config.Config{ CheckPlugins: map[string]*config.CheckPlugin{ - "chk1": &config.CheckPlugin{ + "chk1": { CustomIdentifier: nil, }, - "chk2": &config.CheckPlugin{ + "chk2": { CustomIdentifier: &customIdentifier, }, }, diff --git a/metrics/agent.go b/metrics/agent.go index cc08f8e87..f486e9495 100644 --- a/metrics/agent.go +++ b/metrics/agent.go @@ -37,7 +37,7 @@ func (g *AgentGenerator) CustomIdentifier() *string { func (g *AgentGenerator) PrepareGraphDefs() ([]*mkr.GraphDefsParam, error) { meta := &pluginMeta{ Graphs: map[string]customGraphDef{ - "agent.memory": customGraphDef{ + "agent.memory": { Label: "Agent Memory", Unit: "bytes", Metrics: []customGraphMetricDef{ @@ -47,7 +47,7 @@ func (g *AgentGenerator) PrepareGraphDefs() ([]*mkr.GraphDefsParam, error) { {Name: "heapSys", Label: "Heap Sys"}, }, }, - "agent.runtime": customGraphDef{ + "agent.runtime": { Label: "Agent Runtime", Unit: "integer", Metrics: []customGraphMetricDef{