Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 87a2a62

Browse files
Yash-Varshneypdxjohnny
authored andcommitted
examples: shouldi: Add golangci-lint operation
1 parent 7efa9d9 commit 87a2a62

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Static file serving from a dirctory with `-static`
1919
- `api.js` file serving with the `-js` flag
2020
- Docs page for JavaScript example
21+
- shouldi got an operation to run golangci-lint on Golang code
2122
### Fixed
2223
- Port assignment for the HTTP API via the `-port` flag
2324
### Changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
import asyncio
3+
from typing import Dict, Any
4+
5+
from dffml.df.base import op
6+
from dffml.df.types import Definition
7+
8+
package_src_dir = Definition(name="package_src_dir", primitive="str")
9+
golangci_lint_output = Definition(
10+
name="golangci_lint_output", primitive="Dict[str, Any]"
11+
)
12+
13+
14+
@op(inputs={"pkg": package_src_dir}, outputs={"report": golangci_lint_output})
15+
async def run_golangci_lint(pkg: str) -> Dict[str, Any]:
16+
"""
17+
CLI usage: dffml service dev run -log debug shouldi.golangci_lint:run_golangci_lint -pkg .
18+
"""
19+
proc = await asyncio.create_subprocess_exec(
20+
"golangci-lint",
21+
"run",
22+
"--out-format",
23+
"json",
24+
pkg,
25+
stdout=asyncio.subprocess.PIPE,
26+
stderr=asyncio.subprocess.PIPE,
27+
)
28+
stdout, _stderr = await proc.communicate()
29+
if len(stdout) == 0:
30+
raise Exception(_stderr)
31+
32+
golangci_lint_op = stdout.decode()
33+
issues = json.loads(golangci_lint_op)
34+
35+
return {"issues": len(issues["Issues"])}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
3+
from dffml.util.asynctestcase import AsyncTestCase
4+
5+
from shouldi.golangci_lint import run_golangci_lint
6+
7+
8+
class TestRunGolangci_lintOp(AsyncTestCase):
9+
async def test_run(self):
10+
results = await run_golangci_lint(os.getcwd())
11+
self.assertEqual(type(results["issues"]), int)

0 commit comments

Comments
 (0)