From fb114a17a3cf18192af304c127702976bc52d211 Mon Sep 17 00:00:00 2001 From: Amir Malka Date: Wed, 20 Jul 2022 21:14:15 +0300 Subject: [PATCH] Support windows build --- .github/workflows/build.yaml | 40 ++++++- .github/workflows/build_dev.yaml | 41 ++++++- .github/workflows/pr_checks.yaml | 40 ++++++- README.md | 37 ++++-- build.bat | 51 +++++++++ core/cautils/localgitrepository.go | 3 - core/cautils/localgitrepository_test.go | 33 +++--- core/cautils/localgitrepository_windows.go | 125 --------------------- 8 files changed, 213 insertions(+), 157 deletions(-) create mode 100644 build.bat delete mode 100644 core/cautils/localgitrepository_windows.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 25b28de4..ef5b0b0b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -32,6 +32,39 @@ jobs: with: submodules: recursive + - name: Cache Go modules (Linux) + if: matrix.os == 'ubuntu-latest' + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Cache Go modules (macOS) + if: matrix.os == 'macos-latest' + uses: actions/cache@v3 + with: + path: | + ~/Library/Caches/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Cache Go modules (Windows) + if: matrix.os == 'windows-latest' + uses: actions/cache@v3 + with: + path: | + ~\AppData\Local\go-build + ~\go\pkg\mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Set up Go uses: actions/setup-go@v3 with: @@ -39,7 +72,12 @@ jobs: # - name: Test cmd pkg # run: cd cmd && go test -v ./... - - name: Install libgit2 + - name: Install MSYS2 & libgit2 (Windows) + shell: cmd + run: .\build.bat all + if: matrix.os == 'windows-latest' + + - name: Install libgit2 (Linux/macOS) run: make libgit2 if: matrix.os != 'windows-latest' diff --git a/.github/workflows/build_dev.yaml b/.github/workflows/build_dev.yaml index 153bab25..01c92f14 100644 --- a/.github/workflows/build_dev.yaml +++ b/.github/workflows/build_dev.yaml @@ -14,6 +14,40 @@ jobs: - uses: actions/checkout@v3 with: submodules: recursive + + - name: Cache Go modules (Linux) + if: matrix.os == 'ubuntu-latest' + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Cache Go modules (macOS) + if: matrix.os == 'macos-latest' + uses: actions/cache@v3 + with: + path: | + ~/Library/Caches/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Cache Go modules (Windows) + if: matrix.os == 'windows-latest' + uses: actions/cache@v3 + with: + path: | + ~\AppData\Local\go-build + ~\go\pkg\mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Set up Go uses: actions/setup-go@v3 with: @@ -30,7 +64,12 @@ jobs: # - name: Test cmd pkg # run: cd cmd && go test -v ./... - - name: Install libgit2 + - name: Install MSYS2 & libgit2 (Windows) + shell: cmd + run: .\build.bat all + if: matrix.os == 'windows-latest' + + - name: Install libgit2 (Linux/macOS) run: make libgit2 if: matrix.os != 'windows-latest' diff --git a/.github/workflows/pr_checks.yaml b/.github/workflows/pr_checks.yaml index a49c7833..87d288eb 100644 --- a/.github/workflows/pr_checks.yaml +++ b/.github/workflows/pr_checks.yaml @@ -16,12 +16,50 @@ jobs: with: submodules: recursive + - name: Cache Go modules (Linux) + if: matrix.os == 'ubuntu-latest' + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Cache Go modules (macOS) + if: matrix.os == 'macos-latest' + uses: actions/cache@v3 + with: + path: | + ~/Library/Caches/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Cache Go modules (Windows) + if: matrix.os == 'windows-latest' + uses: actions/cache@v3 + with: + path: | + ~\AppData\Local\go-build + ~\go\pkg\mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Set up Go uses: actions/setup-go@v3 with: go-version: 1.18 - - name: Install libgit2 + - name: Install MSYS2 & libgit2 (Windows) + shell: cmd + run: .\build.bat all + if: matrix.os == 'windows-latest' + + - name: Install libgit2 (Linux/macOS) run: make libgit2 if: matrix.os != 'windows-latest' diff --git a/README.md b/README.md index 5202fa97..83e78e79 100644 --- a/README.md +++ b/README.md @@ -316,29 +316,48 @@ View Kubescape scan results directly in [Lens IDE](https://k8slens.dev/) using k ## Windows +1. Install MSYS2 & build libgit _(needed only for the first time)_ + ``` -go build . -``` -OR -``` -make build +build.bat all ``` -## Linux / MacOS +> You can install MSYS2 separately by running `build.bat install` and build libgit2 separately by running `build.bat build` -1. Install libgit2 dependency +2. Build kubescape + + ``` + make build + ``` + + OR + + ``` + go build -tags=static . + ``` + +## Linux / macOS + +1. Install libgit2 dependency _(needed only for the first time)_ ``` make libgit2 ``` -> `cmake` is required to build libgit2. You can install it by running `sudo apt-get install cmake` (Linux) or `brew install cmake` (MacOS) +> `cmake` is required to build libgit2. You can install it by running `sudo apt-get install cmake` (Linux) or `brew install cmake` (macOS) -2. Build +2. Build kubescape ``` make build ``` + + OR + + ``` + go build -tags=static . + ``` + 3. Test ``` diff --git a/build.bat b/build.bat new file mode 100644 index 00000000..33c71431 --- /dev/null +++ b/build.bat @@ -0,0 +1,51 @@ +@ECHO OFF + +IF "%1"=="install" goto Install +IF "%1"=="build" goto Build +IF "%1"=="all" goto All +IF "%1"=="" goto Error ELSE goto Error + +:Install + +if exist C:\MSYS64\ ( + echo "MSYS2 already installed" +) else ( + mkdir temp_install & cd temp_install + + echo "Downloading MSYS2..." + curl -L https://github.com/msys2/msys2-installer/releases/download/2022-06-03/msys2-x86_64-20220603.exe > msys2-x86_64-20220603.exe + + echo "Installing MSYS2..." + msys2-x86_64-20220603.exe install --root C:\MSYS64 --confirm-command + + cd .. && rmdir /s /q temp_install +) + + +echo "Adding MSYS2 to path..." +SET "PATH=C:\MSYS64\mingw64\bin;C:\MSYS64\usr\bin;%PATH%" +echo %PATH% + +echo "Installing MSYS2 packages..." +pacman -S --needed --noconfirm make +pacman -S --needed --noconfirm mingw-w64-x86_64-cmake +pacman -S --needed --noconfirm mingw-w64-x86_64-gcc +pacman -S --needed --noconfirm mingw-w64-x86_64-pkg-config +pacman -S --needed --noconfirm msys2-w32api-runtime + +IF "%1"=="all" GOTO Build +GOTO End + +:Build +SET "PATH=C:\MSYS2\mingw64\bin;C:\MSYS2\usr\bin;%PATH%" +make libgit2 +GOTO End + +:All +GOTO Install + +:Error +echo "Error: Unknown option" +GOTO End + +:End diff --git a/core/cautils/localgitrepository.go b/core/cautils/localgitrepository.go index 9f2dd40a..a1e2ee12 100644 --- a/core/cautils/localgitrepository.go +++ b/core/cautils/localgitrepository.go @@ -1,6 +1,3 @@ -//go:build !windows -// +build !windows - package cautils import ( diff --git a/core/cautils/localgitrepository_test.go b/core/cautils/localgitrepository_test.go index d7a63f7e..933aa10d 100644 --- a/core/cautils/localgitrepository_test.go +++ b/core/cautils/localgitrepository_test.go @@ -6,7 +6,6 @@ import ( "io" "os" "path/filepath" - "runtime" "strings" "testing" @@ -148,29 +147,29 @@ func (s *LocalGitRepositoryTestSuite) TestGetLastCommit() { func (s *LocalGitRepositoryTestSuite) TestGetFileLastCommit() { s.Run("fileA", func() { if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) { - if runtime.GOOS != "windows" { - if commit, err := localRepo.GetFileLastCommit("fileA"); s.NoError(err) { - s.Equal("9fae4be19624297947d2b605cefbff516628612d", commit.SHA) - s.Equal("Amir Malka", commit.Author.Name) - s.Equal("amirm@armosec.io", commit.Author.Email) - s.Equal("2022-05-22 18:55:48 +0300 +0300", commit.Author.Date.String()) - s.Equal("added file A\n", commit.Message) - } + + if commit, err := localRepo.GetFileLastCommit("fileA"); s.NoError(err) { + s.Equal("9fae4be19624297947d2b605cefbff516628612d", commit.SHA) + s.Equal("Amir Malka", commit.Author.Name) + s.Equal("amirm@armosec.io", commit.Author.Email) + s.Equal("2022-05-22 18:55:48 +0300 +0300", commit.Author.Date.String()) + s.Equal("added file A\n", commit.Message) } + } }) s.Run("fileB", func() { if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) { - if runtime.GOOS != "windows" { - if commit, err := localRepo.GetFileLastCommit("dirA/fileB"); s.NoError(err) { - s.Equal("7e09312b8017695fadcd606882e3779f10a5c832", commit.SHA) - s.Equal("Amir Malka", commit.Author.Name) - s.Equal("amirm@armosec.io", commit.Author.Email) - s.Equal("2022-05-22 19:11:57 +0300 +0300", commit.Author.Date.String()) - s.Equal("added file B\n", commit.Message) - } + + if commit, err := localRepo.GetFileLastCommit("dirA/fileB"); s.NoError(err) { + s.Equal("7e09312b8017695fadcd606882e3779f10a5c832", commit.SHA) + s.Equal("Amir Malka", commit.Author.Name) + s.Equal("amirm@armosec.io", commit.Author.Email) + s.Equal("2022-05-22 19:11:57 +0300 +0300", commit.Author.Date.String()) + s.Equal("added file B\n", commit.Message) } + } }) } diff --git a/core/cautils/localgitrepository_windows.go b/core/cautils/localgitrepository_windows.go deleted file mode 100644 index 5a72f597..00000000 --- a/core/cautils/localgitrepository_windows.go +++ /dev/null @@ -1,125 +0,0 @@ -//go:build windows -// +build windows - -package cautils - -import ( - "fmt" - "path" - "strings" - - "github.com/armosec/go-git-url/apis" - gitv5 "github.com/go-git/go-git/v5" - configv5 "github.com/go-git/go-git/v5/config" - plumbingv5 "github.com/go-git/go-git/v5/plumbing" -) - -type LocalGitRepository struct { - goGitRepo *gitv5.Repository - head *plumbingv5.Reference - config *configv5.Config -} - -func NewLocalGitRepository(path string) (*LocalGitRepository, error) { - goGitRepo, err := gitv5.PlainOpenWithOptions(path, &gitv5.PlainOpenOptions{DetectDotGit: true}) - if err != nil { - return nil, err - } - - head, err := goGitRepo.Head() - if err != nil { - return nil, err - } - - if !head.Name().IsBranch() { - return nil, fmt.Errorf("current HEAD reference is not a branch") - } - - config, err := goGitRepo.Config() - if err != nil { - return nil, err - } - - if len(config.Remotes) == 0 { - return nil, fmt.Errorf("no remotes found") - } - - return &LocalGitRepository{ - goGitRepo: goGitRepo, - head: head, - config: config, - }, nil -} - -// GetBranchName get current branch name -func (g *LocalGitRepository) GetBranchName() string { - return g.head.Name().Short() -} - -// GetRemoteUrl get default remote URL -func (g *LocalGitRepository) GetRemoteUrl() (string, error) { - branchName := g.GetBranchName() - if branchRef, branchFound := g.config.Branches[branchName]; branchFound { - remoteName := branchRef.Remote - if len(g.config.Remotes[remoteName].URLs) == 0 { - return "", fmt.Errorf("expected to find URLs for remote '%s', branch '%s'", remoteName, branchName) - } - return g.config.Remotes[remoteName].URLs[0], nil - } - - const defaultRemoteName string = "origin" - if len(g.config.Remotes[defaultRemoteName].URLs) == 0 { - return "", fmt.Errorf("expected to find URLs for remote '%s'", defaultRemoteName) - } - return g.config.Remotes[defaultRemoteName].URLs[0], nil -} - -// GetName get origin name without the .git suffix -func (g *LocalGitRepository) GetName() (string, error) { - originUrl, err := g.GetRemoteUrl() - if err != nil { - return "", err - } - baseName := path.Base(originUrl) - // remove .git - return strings.TrimSuffix(baseName, ".git"), nil -} - -// GetLastCommit get latest commit object -func (g *LocalGitRepository) GetLastCommit() (*apis.Commit, error) { - cIter, err := g.goGitRepo.Log(&gitv5.LogOptions{}) - if err != nil { - return nil, err - } - - commit, err := cIter.Next() - defer cIter.Close() - if err != nil { - return nil, err - } - - return &apis.Commit{ - SHA: commit.Hash.String(), - Author: apis.Committer{ - Name: commit.Author.Name, - Email: commit.Author.Email, - Date: commit.Author.When, - }, - Message: commit.Message, - Committer: apis.Committer{}, - Files: []apis.Files{}, - }, nil -} - -func (g *LocalGitRepository) GetFileLastCommit(filePath string) (*apis.Commit, error) { - return nil, nil -} - -func (g *LocalGitRepository) GetRootDir() (string, error) { - wt, err := g.goGitRepo.Worktree() - if err != nil { - return "", fmt.Errorf("failed to get repo root") - } - - return wt.Filesystem.Root(), nil -}