Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic code reading and upload to IFCP #936

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions .github/workflows/govulncheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
concurrency:
cancel-in-progress: true
group: govulncheck-${{ github.ref }}
env:
GOEXPERIMENT: 'rangefunc'
name: govulncheck
jobs:
govulncheck:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ concurrency:
cancel-in-progress: true
group: linter-${{ github.ref }}
name: golangci-lint
env:
GOEXPERIMENT: 'rangefunc'
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21.5'
go-version: '1.22.0'
- uses: actions/checkout@v3
- uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prettier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21.5'
go-version: '1.22.0'
- uses: actions/checkout@v3
- name: List files to check
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: '1.21.5'
go-version: '1.22.0'

- name: Setup Zig
uses: goto-bus-stop/setup-zig@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-integ-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.21.5'
go-version: '1.22.0'
- name: pre-build
if: ${{ inputs.pre-build-script }}
run: ${{ inputs.pre-build-script }}
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ concurrency:
name: unit tests
permissions:
contents: write
env:
GOEXPERIMENT: 'rangefunc'
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21.5'
go-version: '1.22.0'
- uses: actions/checkout@v3
- uses: actions/cache@v2
with:
Expand All @@ -28,9 +30,17 @@ jobs:
restore-keys: |
${{ runner.os }}-go-

- uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install lang servers
run: |
python3 -m pip install python-lsp-server[all]

- name: Run test
run: |
touch pkg/auth/auth0_client_secret.key
export PATH=$HOME/.local/bin:$PATH
go test -race -v -coverprofile=c.out ./...

- name: Update coverage report
Expand Down
6 changes: 6 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ tab-width = 2
[run]
# Don't bother linting test files
skip-files = ["pkg/.*_test.go"]

[[issues.exclude-rules]]
# Temporarily disable linting rangefunc until it's supported
# https://github.com/dominikh/go-tools/issues/1494
linters = ["typecheck"]
text = "cannot range over"
129 changes: 129 additions & 0 deletions cmd/ast/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"context"
"io"
"os"
"path/filepath"
"strings"

"github.com/alecthomas/kong"
sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/dockerfile"
"github.com/smacker/go-tree-sitter/golang"
"github.com/smacker/go-tree-sitter/javascript"
"github.com/smacker/go-tree-sitter/python"
"github.com/smacker/go-tree-sitter/typescript/typescript"
"gopkg.in/yaml.v3"
)

type Args struct {
File string `arg:"" help:"The file to parse." type:"existingfile"`
}

func main() {
var args Args
ctx := kong.Parse(&args)

if err := ctx.Run(); err != nil {
panic(err)
}
}

func (a Args) Run(kctx *kong.Context) error {
p := sitter.NewParser()
f, err := os.Open(a.File)
if err != nil {
return err
}
defer f.Close()

switch filepath.Ext(a.File) {
case ".py":
p.SetLanguage(python.GetLanguage())

case ".go":
p.SetLanguage(golang.GetLanguage())

case ".js":
p.SetLanguage(javascript.GetLanguage())

case ".ts":
p.SetLanguage(typescript.GetLanguage())

case ".Dockerfile":
p.SetLanguage(dockerfile.GetLanguage())

case "":
switch filepath.Base(a.File) {
case "Dockerfile":
p.SetLanguage(dockerfile.GetLanguage())
}
}

content, err := io.ReadAll(f)
if err != nil {
return err
}

tree, err := p.ParseCtx(context.Background(), nil, content)
if err != nil {
return err
}

err = yaml.NewEncoder(os.Stdout).Encode((*ast)(tree.RootNode()))
if err != nil {
return err
}

return nil
}

type ast sitter.Node

func (a ast) MarshalYAML() (interface{}, error) {
return a.toNode(), nil
}

func (a ast) toNode() *yaml.Node {
n := (*sitter.Node)(&a)
y := &yaml.Node{}

if n.NamedChildCount() > 0 {
y.Kind = yaml.MappingNode
for i := 0; i < int(n.ChildCount()); i++ {
child := n.Child(i)
if !child.IsNamed() {
continue
}
key := &yaml.Node{
Kind: yaml.ScalarNode,
Value: n.FieldNameForChild(i),
}
if key.Value != "" {
key.Value += ": "
}
key.Value += "(" + child.Type() + ")"

value := (*ast)(child).toNode()

c := child.Content()
if !strings.Contains(c, "\n") {
key.LineComment = c
}

y.Content = append(y.Content, key, value)
}
} else {
y.Kind = yaml.ScalarNode
y.Tag = "!!null"
y.Value = ""

c := n.Content()
if !strings.Contains(c, "\n") {
y.LineComment = c
}
}

return y
}
Loading
Loading