Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: 1.25
go-version: 'stable'
check-latest: true

- name: Test
run: go test -v ./...
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.11.2
version: v2.12.2
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ run:
tests: false
linters:
default: all
enable:
- wsl_v5
- gomodguard_v2
disable:
- cyclop
- wsl
- gomodguard
- depguard
- err113
- exhaustruct
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.PHONY: test coverage lint vet

build:
go build
CGO_ENABLED=0 go build
release-snapshot:
goreleaser release --snapshot --clean
lint:
go fmt $(go list ./... | grep -v /vendor/)
vet:
Expand All @@ -12,4 +14,4 @@ coverage:
go test -v -cover -coverprofile=coverage.out ./... &&\
go tool cover -html=coverage.out -o coverage.html
clean:
rm -f support-collector_*.zip
rm -f support-collector support-collector_*.zip
10 changes: 0 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ go 1.25.0

require (
github.com/Showmax/go-fqdn v1.0.0
github.com/mattn/go-colorable v0.1.14
github.com/mitchellh/go-ps v1.0.0
github.com/sirupsen/logrus v1.9.4
github.com/spf13/pflag v1.0.10
github.com/stretchr/testify v1.11.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-isatty v0.0.22 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.43.0 // indirect
)
19 changes: 0 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM=
github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4=
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
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.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
50 changes: 26 additions & 24 deletions internal/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"archive/zip"
"bytes"
"fmt"
"github.com/NETWAYS/support-collector/internal/config"
"github.com/NETWAYS/support-collector/internal/metrics"
"io"
"log/slog"
"os"
"strings"
"time"

"github.com/NETWAYS/support-collector/internal/config"
"github.com/NETWAYS/support-collector/internal/metrics"
"github.com/NETWAYS/support-collector/internal/obfuscate"
"github.com/sirupsen/logrus"

"gopkg.in/yaml.v3"
)

type Collection struct {
Output *zip.Writer
Log *logrus.Logger
Log *slog.Logger
LogData *bytes.Buffer
ExecTimeout time.Duration
Obfuscators []*obfuscate.Obfuscator
Expand All @@ -29,9 +31,11 @@ type Collection struct {

// New initializes new collection with defaults
func New(w io.Writer) (c *Collection) {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))

c = &Collection{
Output: zip.NewWriter(w),
Log: logrus.New(),
Log: logger,
LogData: &bytes.Buffer{},
ExecTimeout: DefaultTimeout,
Obfuscators: nil,
Expand All @@ -41,8 +45,6 @@ func New(w io.Writer) (c *Collection) {
Metric: nil,
}

c.Log.Out = c.LogData

return
}

Expand All @@ -54,12 +56,12 @@ func (c *Collection) Close() error {
func (c *Collection) AddFileToOutput(file *File) (err error) {
file.Data, err = c.callObfuscators(obfuscate.KindFile, file.Name, file.Data)
if err != nil {
c.Log.Warn(err)
c.Log.Warn(err.Error())
}

err = c.AddFileToOutputRaw(file)
if err != nil {
c.Log.Warn(err)
c.Log.Warn(err.Error())
}

return
Expand Down Expand Up @@ -130,7 +132,7 @@ func (c *Collection) AddFileDataRaw(fileName string, data []byte) {

err := c.AddFileToOutputRaw(file)
if err != nil {
c.Log.Warn(err)
c.Log.Warn(err.Error())
}
}

Expand All @@ -139,7 +141,7 @@ func (c *Collection) AddFileYAML(fileName string, data any) {

err := yaml.NewEncoder(&buf).Encode(&data)
if err != nil {
c.Log.Debugf("could not encode YAML data for '%s': %s", fileName, err)
c.Log.Debug("could not encode YAML data", "file", fileName, "error", err.Error())
}

file := NewFile(fileName)
Expand All @@ -165,11 +167,11 @@ func (c *Collection) AddFileJSONRaw(fileName string, data []byte) {
}

func (c *Collection) AddFiles(prefix, source string) {
c.Log.Debug("Collecting files from ", source)
c.Log.Debug("Collecting files from: " + source)

files, err := LoadFiles(prefix, source)
if err != nil {
c.Log.Debug(err)
c.Log.Debug(err.Error())
}

for _, file := range files {
Expand All @@ -190,23 +192,23 @@ func (c *Collection) AddFilesIfFound(prefix string, sources ...string) {
}

if foundFiles == 0 {
c.Log.Debugf("Found no files under: %s", strings.Join(sources, " "))
c.Log.Debug(fmt.Sprintf("Found no files under: %s", strings.Join(sources, " ")))
}
}

func (c *Collection) AddCommandOutputWithTimeout(file string,
timeout time.Duration, command string, arguments ...string) {
c.Log.Debugf("Collecting command output: '%s %s'", command, strings.Join(arguments, " "))
c.Log.Debug(fmt.Sprintf("Collecting command output: '%s %s'", command, strings.Join(arguments, " ")))

output, err := LoadCommandOutputWithTimeout(timeout, command, arguments...)
if err != nil {
c.Log.Debug(err)
c.Log.Debug(err.Error())
}

// obfuscate
output, err = c.callObfuscators(obfuscate.KindOutput, obfuscate.JoinCommand(command, arguments...), output)
if err != nil {
c.Log.Debug(err)
c.Log.Debug(err.Error())
}

c.AddFileDataRaw(file, output)
Expand All @@ -217,33 +219,33 @@ func (c *Collection) AddCommandOutput(file, command string, arguments ...string)
}

func (c *Collection) AddInstalledPackagesRaw(fileName string, pattern ...string) {
c.Log.Debug("Collecting installed packages for pattern ", strings.Join(pattern, " "))
c.Log.Debug("Collecting installed packages for pattern: " + strings.Join(pattern, " "))

packages, err := ListInstalledPackagesRaw(pattern...)
if err != nil {
c.Log.Debug(err)
c.Log.Debug(err.Error())
}

c.AddFileDataRaw(fileName, packages)
}

func (c *Collection) AddServiceStatusRaw(fileName, name string) {
c.Log.Debug("Collecting service status for ", name)
c.Log.Debug("Collecting service status for " + name)

output, err := GetServiceStatusRaw(name)
if err != nil {
c.Log.Debug(err)
c.Log.Debug(err.Error())
}

c.AddFileDataRaw(fileName, output)
}

func (c *Collection) AddGitRepoInfo(fileName, path string) {
c.Log.Debug("Collecting GIT repository details for ", path)
c.Log.Debug("Collecting GIT repository details for " + path)

info, err := LoadGitRepoInfo(path)
if err != nil {
c.Log.Debug(err)
c.Log.Debug(err.Error())
}

c.AddFileYAML(fileName, info)
Expand Down Expand Up @@ -287,7 +289,7 @@ func (c *Collection) callObfuscators(kind obfuscate.Kind, name string, data []by
}

if count > 0 {
c.Log.Debugf("ReplacePattern '%s' replaced %d token in %s", o.ReplacePattern, count, name)
c.Log.Debug(fmt.Sprintf("ReplacePattern '%s' replaced %d token in %s", o.ReplacePattern, count, name))
count = 0
}
}
Expand Down
25 changes: 18 additions & 7 deletions internal/collection/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ package collection
import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCollection_AddFileFromReader(t *testing.T) {
buf := &bytes.Buffer{}
c := New(buf)

err := c.AddFileFromReaderRaw("test.txt", bytes.NewBufferString("content"))
assert.NoError(t, err)

if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

err = c.Close()
assert.NoError(t, err)

assert.Greater(t, buf.Len(), 0)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if buf.Len() == 0 {
t.Error("Expected buffer to be not empty")
}
}

func TestCollection_AddFiles(t *testing.T) {
Expand All @@ -27,7 +33,12 @@ func TestCollection_AddFiles(t *testing.T) {
c.AddFiles("test", "testdata/")

err := c.Close()
assert.NoError(t, err)

assert.Greater(t, buf.Len(), 0)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if buf.Len() == 0 {
t.Error("Expected buffer to be not empty")
}
}
33 changes: 25 additions & 8 deletions internal/collection/exec_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package collection

import (
"bytes"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

var testTimeout = 100 * time.Millisecond

func TestLoadCommandOutputWithTimeout(t *testing.T) {
output, err := LoadCommandOutputWithTimeout(testTimeout, "sh", "-c", "echo good; echo stderr >&2")
assert.NoError(t, err)
assert.Equal(t, []byte("good\nstderr\n"), output)

if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if !bytes.Equal(output, []byte("good\nstderr\n")) {
t.Errorf("expected %q, got %q", []byte("good\nstderr\n"), output)
}

output, err = LoadCommandOutputWithTimeout(testTimeout, "sh", "-c", "exit 1")
assert.Error(t, err)
assert.NotEmpty(t, output)

if err == nil {
t.Fatalf("expected error, got nil")
}

if bytes.Equal(output, []byte{}) {
t.Errorf("expected output not to be empty")
}

output, err = LoadCommandOutputWithTimeout(testTimeout, "sh", "-c", "sleep 1")
assert.Error(t, err)
assert.NotEmpty(t, output)

if err == nil {
t.Fatalf("expected error, got nil")
}

if bytes.Equal(output, []byte{}) {
t.Errorf("expected output not to be empty")
}
}
Loading