From 4e46342d67341fa4b41746eed3bee0d328046fd6 Mon Sep 17 00:00:00 2001 From: Maximilian Hafer Date: Mon, 21 Oct 2024 23:02:36 +0200 Subject: [PATCH] enhance Makefile, fix linter issues and improve CI workflows --- .github/workflows/go.yaml | 6 +++++ .gitignore | 2 +- Makefile | 20 +++++++++++++-- go.mod | 1 - go.sum | 4 --- pkg/cryptostream/aesstream.go | 13 ---------- pkg/packager/packager_test.go | 48 +---------------------------------- pkg/zipper/zipper.go | 5 +++- 8 files changed, 30 insertions(+), 69 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index abae623..e4bb001 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -1,14 +1,20 @@ on: [push, pull_request] +env: + GO_VERSION: 1.23 jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} - run: go test -v ./... lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} - uses: golangci/golangci-lint-action@v6 \ No newline at end of file diff --git a/.gitignore b/.gitignore index bdd50a1..86a2660 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .idea -bin +bin/ dist/ diff --git a/Makefile b/Makefile index 108cd61..8b77c15 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,22 @@ APP_NAME=content-prep +GOLANGCI_LINT_VERSION=1.61.0 +LOCALBIN=$(shell pwd)/bin + +.PHONY: dep +dep: + @echo "Installing dependencies..." + go mod tidy .PHONY: build -build: +build: dep @echo "Building..." - @go build -o bin/$(APP_NAME) main.go \ No newline at end of file + go build -o dist/$(APP_NAME) main.go + +.PHONY: lint +lint: $(LOCALBIN)/golangci-lint dep + @echo "Running golangci-lint version ${GOLANGCI_LINT_VERSION}..." + @$(LOCALBIN)/golangci-lint run + +$(LOCALBIN)/golangci-lint: + @echo "Installing golangci-lint version ${GOLANGCI_LINT_VERSION}..." + @curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v${GOLANGCI_LINT_VERSION} \ No newline at end of file diff --git a/go.mod b/go.mod index c4ad2e0..09ccfa6 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.23.2 require ( github.com/pkg/errors v0.9.1 - github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index 454760e..f6db646 100644 --- a/go.sum +++ b/go.sum @@ -35,8 +35,6 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= @@ -54,7 +52,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= @@ -68,7 +65,6 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= diff --git a/pkg/cryptostream/aesstream.go b/pkg/cryptostream/aesstream.go index f49b536..1fff79a 100644 --- a/pkg/cryptostream/aesstream.go +++ b/pkg/cryptostream/aesstream.go @@ -7,25 +7,12 @@ import ( "crypto/sha256" "errors" "io" - - "github.com/sirupsen/logrus" ) const BufferSize int = 2097152 const IvSize int = 16 const HMACKeySize = 64 -type loggingWriter struct { - w io.Writer - mode string -} - -func (l loggingWriter) Write(p []byte) (n int, err error) { - logrus.WithField("mode", l.mode).Info("Write:", p) - - return l.w.Write(p) -} - // Encrypt the stream using the given AES-CTR and SHA256-HMAC key func Encrypt(in io.ReadSeeker, out io.WriteSeeker, keyAes []byte, iv []byte, hmacKey []byte) error { AES, err := aes.NewCipher(keyAes) diff --git a/pkg/packager/packager_test.go b/pkg/packager/packager_test.go index 4806ec4..23f3259 100644 --- a/pkg/packager/packager_test.go +++ b/pkg/packager/packager_test.go @@ -13,7 +13,6 @@ import ( "testing" "testing/fstest" - "github.com/pkg/errors" "github.com/stretchr/testify/suite" ) @@ -26,52 +25,6 @@ func (m mykeygen) GenerateKey(length int) ([]byte, error) { return []byte(strings.Repeat(".", length)), nil } -type mywriter struct { - buf []byte - pos int -} - -func (m *mywriter) Write(p []byte) (n int, err error) { - minCap := m.pos + len(p) - if minCap > cap(m.buf) { // Make sure buf has enough capacity: - buf2 := make([]byte, len(m.buf), minCap+len(p)) // add some extra - copy(buf2, m.buf) - m.buf = buf2 - } - if minCap > len(m.buf) { - m.buf = m.buf[:minCap] - } - copy(m.buf[m.pos:], p) - m.pos += len(p) - return len(p), nil -} - -func (m *mywriter) Read(p []byte) (n int, err error) { - if m.pos >= len(m.buf) { - return 0, io.EOF - } - n = copy(p, m.buf[m.pos:]) - m.pos += n - return n, nil -} - -func (m *mywriter) Seek(offset int64, whence int) (int64, error) { - newPos, offs := 0, int(offset) - switch whence { - case io.SeekStart: - newPos = offs - case io.SeekCurrent: - newPos = m.pos + offs - case io.SeekEnd: - newPos = len(m.buf) + offs - } - if newPos < 0 { - return 0, errors.New("negative result pos") - } - m.pos = newPos - return int64(newPos), nil -} - func TestPackagerTestSuite(t *testing.T) { suite.Run(t, new(PackagerTestSuite)) } @@ -107,6 +60,7 @@ func (s *PackagerTestSuite) TestPackager() { } out, err := os.Create(path.Join(s.testDir, "test.intunewin")) + s.Require().NoError(err) err = p.CreatePackage(context.Background(), s.fs, "test.exe", out) s.Require().NoError(err) diff --git a/pkg/zipper/zipper.go b/pkg/zipper/zipper.go index 32e4ac5..e4e5605 100644 --- a/pkg/zipper/zipper.go +++ b/pkg/zipper/zipper.go @@ -125,7 +125,10 @@ func UnzipToDirectory(ctx context.Context, archiveFilePath, outputFolderPath str return errors.Errorf("illegal file path: %s", filePath) } if f.FileInfo().IsDir() { - os.MkdirAll(filePath, os.ModePerm) + err = os.MkdirAll(filePath, os.ModePerm) + if err != nil { + return errors.Wrapf(err, "failed to create directory") + } continue }