Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: 2

project_name: ridlfmt

builds:
- id: ridlfmt
main: .
binary: ridlfmt
env:
- CGO_ENABLED=0
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
ldflags:
- -s -w -X main.version=v{{.Version}}

archives:
- formats: [binary]
name_template: "{{ .Binary }}.{{ .Os }}-{{ .Arch }}"

checksum:
name_template: checksums.txt

changelog:
use: github-native
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release tag

on:
push:
tags:
- "v*"

permissions:
contents: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Test
run: go test ./...
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser
version: "~> v2"
args: release -f .github/.goreleaser.yml --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea/*

bin/*
dist/
_examples/e2.ridl
coverage.txt
coverage.html
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
VERSION ?= $(shell git describe --tags --dirty 2>/dev/null || echo v0.0.0-dev)

build:
go build -o ./bin/ridlfmt ./main.go
go build -ldflags="-s -w -X main.version=$(VERSION)" -o ./bin/ridlfmt .

install:
go install .
go install -ldflags="-s -w -X main.version=$(VERSION)" .

rerun-install:
go run github.com/goware/rerun/cmd/rerun -watch . -ignore out -run sh -c 'go install .'
go run github.com/goware/rerun/cmd/rerun -watch . -ignore out -run sh -c 'make install'

rerun-1:
go run github.com/goware/rerun/cmd/rerun -watch . -ignore out -run sh -c 'go run . -s _examples/e1.ridl'
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ It uses similiar API as `gofmt`
ridlfmt -h
usage: ridlfmt [flags] [path...]

-h show help
-s sort errors by code
-w write result to (source) file instead of stdout
Directory paths format .ridl files recursively (also accepts ./...).

-h show help
-s sort errors by code
-v, -version show version
-w write result to (source) file instead of stdout
```

## Installation
Expand All @@ -30,7 +33,7 @@ file arguments are formatted regardless of their extension. `./...` and
`directory/...` are also supported as recursive directory shortcuts.

```bash
ridlfmt schema.ridl # Print a formatted file
ridlfmt schema.ridl # Print a formatted file
ridlfmt . # Print all .ridl files recursively
ridlfmt -w ./... # Format all .ridl files recursively in place
ridlfmt -w -s schemas # Format a directory and sort errors by code
Expand Down
30 changes: 27 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
"log"
"os"
"path/filepath"
"runtime/debug"
"strings"

"github.com/webrpc/ridlfmt/formatter"
)

// version can be set by release builds with -ldflags "-X main.version=vX.Y.Z".
var version = "devel"

func main() {
flagSet := flag.NewFlagSet("ridlfmt", flag.ExitOnError)
if err := runRidlfmt(flagSet, os.Args[1:]); err != nil {
Expand All @@ -27,6 +31,9 @@ func runRidlfmt(flagSet *flag.FlagSet, args []string) error {
sortErrorsFlag := flagSet.Bool("s", false, "sort errors by code")
writeFlag := flagSet.Bool("w", false, "write output to input file (overwrites the file)")
helpFlag := flagSet.Bool("h", false, "show help")
var versionFlag bool
flagSet.BoolVar(&versionFlag, "version", false, "show version")
flagSet.BoolVar(&versionFlag, "v", false, "show version")

if err := flagSet.Parse(args); err != nil {
return fmt.Errorf("parse args: %w", err)
Expand All @@ -37,6 +44,12 @@ func runRidlfmt(flagSet *flag.FlagSet, args []string) error {
os.Exit(0)
}

if versionFlag {
info, _ := debug.ReadBuildInfo()
fmt.Printf("ridlfmt %s\n", buildVersion(info))
return nil
}

fileArgs := flagSet.Args()

if len(fileArgs) == 0 && !isInputFromPipe() {
Expand Down Expand Up @@ -76,6 +89,16 @@ func runRidlfmt(flagSet *flag.FlagSet, args []string) error {
return nil
}

func buildVersion(info *debug.BuildInfo) string {
if version != "devel" {
return version
}
if info != nil && info.Main.Version != "" && info.Main.Version != "(devel)" {
return info.Main.Version
}
return "devel"
}

// expandFileArgs expands directories and directory/... to regular .ridl files.
// WalkDir visits files in lexical order and does not follow symbolic links.
func expandFileArgs(args []string) ([]string, error) {
Expand Down Expand Up @@ -194,8 +217,9 @@ func usage() {

Directory paths format .ridl files recursively (also accepts ./...).

-h show help
-s sort errors by code
-w write result to (source) file instead of stdout
-h show help
-s sort errors by code
-v, -version show version
-w write result to (source) file instead of stdout
`)
}
44 changes: 44 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"testing"

Expand Down Expand Up @@ -358,6 +359,49 @@ func TestSubprocess_HelpFlag(t *testing.T) {
_, stderr, code := runMainSubprocess(t, []string{"-h"}, "", false)
assert.Equal(t, 0, code)
assert.Contains(t, stderr, "usage: ridlfmt [flags] [path...]")
assert.Contains(t, stderr, "-v, -version")
}

func TestBuildVersion(t *testing.T) {
oldVersion := version
t.Cleanup(func() { version = oldVersion })
for _, tt := range []struct {
name string
override string
info *debug.BuildInfo
want string
}{
{"release module", "devel", &debug.BuildInfo{Main: debug.Module{Version: "v0.3.0"}}, "v0.3.0"},
{"prerelease module", "devel", &debug.BuildInfo{Main: debug.Module{Version: "v0.4.0-rc.1"}}, "v0.4.0-rc.1"},
{"pseudo version", "devel", &debug.BuildInfo{Main: debug.Module{Version: "v0.3.1-0.20261021070000-abcdef123456"}}, "v0.3.1-0.20261021070000-abcdef123456"},
{"local build", "devel", &debug.BuildInfo{Main: debug.Module{Version: "(devel)"}}, "devel"},
{"empty module version", "devel", &debug.BuildInfo{}, "devel"},
{"missing build info", "devel", nil, "devel"},
{"linker override", "v0.4.0", &debug.BuildInfo{Main: debug.Module{Version: "v0.3.0"}}, "v0.4.0"},
{"linker override without build info", "v0.4.0", nil, "v0.4.0"},
} {
t.Run(tt.name, func(t *testing.T) {
version = tt.override
assert.Equal(t, tt.want, buildVersion(tt.info))
})
}
}

func TestSubprocess_VersionFlags(t *testing.T) {
for _, flag := range []string{"-version", "-v"} {
for _, piped := range []bool{false, true} {
name := flag + "/terminal"
if piped {
name = flag + "/pipe"
}
t.Run(name, func(t *testing.T) {
stdout, stderr, code := runMainSubprocess(t, []string{flag, "missing.ridl"}, "invalid input", piped)
assert.Equal(t, 0, code)
assert.Equal(t, "ridlfmt devel\n", stdout)
assert.Empty(t, stderr)
})
}
}
}

func TestSubprocess_NoInputFiles(t *testing.T) {
Expand Down
Loading