Skip to content

Commit e80fa0d

Browse files
authored
feat: Find project root of a Golang project (#1)
1 parent e443ec5 commit e80fa0d

File tree

10 files changed

+177
-0
lines changed

10 files changed

+177
-0
lines changed

.github/dependabot.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
version: 2
3+
updates:
4+
- package-ecosystem: "docker"
5+
directory: "/"
6+
schedule:
7+
interval: "weekly"
8+
groups:
9+
docker-all:
10+
patterns:
11+
- "*"
12+
- package-ecosystem: "github-actions"
13+
directory: "/"
14+
schedule:
15+
interval: "weekly"
16+
groups:
17+
github-actions-all:
18+
patterns:
19+
- "*"
20+
- package-ecosystem: "gomod"
21+
directory: "/"
22+
schedule:
23+
interval: "weekly"
24+
groups:
25+
gomod-all:
26+
patterns:
27+
- "*"

.github/workflows/golang.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
name: Golang
3+
"on":
4+
# required by gomod-go-version-updater to trigger this action once pr has
5+
# been reviewed
6+
pull_request_review:
7+
types: [submitted]
8+
push:
9+
permissions:
10+
contents: read
11+
packages: read
12+
jobs:
13+
mcvs-golang-action:
14+
# yamllint disable rule:braces
15+
# yamllint disable rule:indentation
16+
strategy:
17+
matrix:
18+
args:
19+
- { testing-type: "component" }
20+
- { testing-type: "coverage" }
21+
- { testing-type: "integration" }
22+
- { testing-type: "lint", build-tags: "component" }
23+
- { testing-type: "lint", build-tags: "e2e" }
24+
- { testing-type: "lint", build-tags: "integration" }
25+
- { testing-type: "security-golang-modules" }
26+
- { testing-type: "security-grype" }
27+
- { testing-type: "security-trivy" }
28+
- { testing-type: "unit" }
29+
runs-on: ubuntu-22.04
30+
# yamllint enable rule:braces
31+
# yamllint enable rule:indentation
32+
env:
33+
TASK_X_REMOTE_TASKFILES: 1
34+
steps:
35+
- uses: actions/checkout@v4.2.2
36+
- uses: schubergphilis/mcvs-golang-action@v0.16.1
37+
with:
38+
code-coverage-expected: 0.0
39+
build-tags: ${{ matrix.args.build-tags }}
40+
golang-unit-tests-exclusions: |-
41+
\(cmd\/mcvs-scanner\|cmd\/mcvs-scanner-cli\|deprecated\|api\/swagger\|mocks\)
42+
testing-type: ${{ matrix.args.testing-type }}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: gomod-go-version-updater-action
3+
"on":
4+
schedule:
5+
- cron: "42 5 * * 1-5"
6+
permissions:
7+
contents: write
8+
pull-requests: write
9+
repository-projects: write
10+
jobs:
11+
gomod-go-version-updater-action:
12+
runs-on: ubuntu-22.04
13+
steps:
14+
- uses: schubergphilis/gomod-go-version-updater-action@v0.2.2
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: MCVS-PR-validation-action
3+
"on":
4+
pull_request:
5+
types:
6+
- edited
7+
- opened
8+
- reopened
9+
- synchronize
10+
workflow_call:
11+
permissions:
12+
contents: read
13+
pull-requests: read
14+
jobs:
15+
MCVS-PR-validation-action:
16+
runs-on: ubuntu-22.04
17+
steps:
18+
- uses: actions/checkout@v4.2.2
19+
- uses: schubergphilis/mcvs-pr-validation-action@v0.2.0

.github/workflows/stale.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: Close stale issues and PRs
3+
"on":
4+
schedule:
5+
- cron: "42 4 * * 1-5"
6+
permissions:
7+
contents: write
8+
issues: write
9+
pull-requests: write
10+
jobs:
11+
stale:
12+
runs-on: ubuntu-22.04
13+
steps:
14+
- uses: actions/stale@v9.1.0
15+
with:
16+
days-before-stale: 21
17+
days-before-close: 7
18+
exempt-pr-labels: "dependencies,security"
19+
stale-issue-message: |
20+
Issue will be marked as stale because it has not been updated in a
21+
while.
22+
stale-pr-message: |
23+
PR will be marked as stale because it has not been updated in a
24+
while.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.task
2+
.vscode

.trivyignore

Whitespace-only changes.

Taskfile.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
version: 3
3+
4+
vars:
5+
REMOTE_URL: https://raw.githubusercontent.com
6+
REMOTE_URL_REF: v0.15.5
7+
REMOTE_URL_REPO: schubergphilis/mcvs-golang-action
8+
9+
includes:
10+
remote:
11+
taskfile: >-
12+
{{.REMOTE_URL}}/{{.REMOTE_URL_REPO}}/{{.REMOTE_URL_REF}}/Taskfile.yml

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/schubergphilis.com/mcvs-golang-project-root
2+
3+
go 1.23.4

pkg/projectroot/projectroot.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package projectroot
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
// FindProjectRoot finds the root directory of the project.
10+
func FindProjectRoot() (string, error) {
11+
currentDir, err := os.Getwd()
12+
if err != nil {
13+
return "", err
14+
}
15+
16+
for {
17+
if fileExists(filepath.Join(currentDir, "go.mod")) {
18+
return currentDir, nil
19+
}
20+
21+
parentDir := filepath.Dir(currentDir)
22+
if currentDir == parentDir {
23+
break
24+
}
25+
currentDir = parentDir
26+
}
27+
28+
return "", fmt.Errorf("project root not found")
29+
}
30+
31+
func fileExists(path string) bool {
32+
_, err := os.Stat(path)
33+
return !os.IsNotExist(err)
34+
}

0 commit comments

Comments
 (0)