Skip to content

Commit

Permalink
feat!: github.com/jmattheis/goverter command
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Feb 18, 2024
1 parent a052854 commit 99803cb
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 43 deletions.
11 changes: 11 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"os"

"github.com/jmattheis/goverter/cmd"
)

func main() {
cmd.Run(os.Args, cmd.RunOpts{})
}
7 changes: 3 additions & 4 deletions cli/cli.go → cmd/cli.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cli
package cmd

import (
"flag"
"fmt"

"github.com/jmattheis/goverter"
"github.com/jmattheis/goverter/config"
"github.com/jmattheis/goverter/enum"
)
Expand All @@ -20,7 +19,7 @@ func (s *Strings) Set(value string) error {
return nil
}

func Parse(args []string) (*goverter.GenerateConfig, error) {
func ParseArgs(args []string) (*GenerateConfig, error) {
switch len(args) {
case 0:
return nil, usage("unknown")
Expand Down Expand Up @@ -52,7 +51,7 @@ func Parse(args []string) (*goverter.GenerateConfig, error) {
return nil, usageErr("missing PATTERN", args[0])
}

c := goverter.GenerateConfig{
c := GenerateConfig{
PackagePatterns: patterns,
BuildTags: *buildTags,
OutputBuildConstraint: *outputConstraint,
Expand Down
24 changes: 13 additions & 11 deletions cli/cli_test.go → cmd/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package cli_test
package cmd_test

import (
"strings"
"testing"

"github.com/jmattheis/goverter"
"github.com/jmattheis/goverter/cli"
"github.com/jmattheis/goverter/cmd"
"github.com/jmattheis/goverter/config"
"github.com/jmattheis/goverter/enum"
"github.com/stretchr/testify/require"
)

func TestError(t *testing.T) {
func TestCLIError(t *testing.T) {
tests := []struct {
args []string
contains string
Expand All @@ -27,14 +27,14 @@ func TestError(t *testing.T) {
for _, test := range tests {
test := test
t.Run(strings.Join(test.args, " "), func(t *testing.T) {
_, err := cli.Parse(test.args)
_, err := cmd.ParseArgs(test.args)
require.ErrorContains(t, err, test.contains)
})
}
}

func TestSuccess(t *testing.T) {
actual, err := cli.Parse([]string{
func TestCLISuccess(t *testing.T) {
actual, err := cmd.ParseArgs([]string{
"goverter",
"gen",
"-cwd", "file/path",
Expand All @@ -47,11 +47,12 @@ func TestSuccess(t *testing.T) {
})
require.NoError(t, err)

expected := &goverter.GenerateConfig{
expected := &cmd.GenerateConfig{
PackagePatterns: []string{"pattern1", "pattern2"},
WorkingDir: "file/path",
OutputBuildConstraint: "",
BuildTags: "",
EnumTransformers: map[string]enum.Transformer{},
Global: config.RawLines{
Location: "command line (-g, -global)",
Lines: []string{"g1", "g2", "g3 oops"},
Expand All @@ -60,15 +61,16 @@ func TestSuccess(t *testing.T) {
require.Equal(t, expected, actual)
}

func TestDefault(t *testing.T) {
actual, err := cli.Parse([]string{"goverter", "gen", "pattern"})
func TestCLIDefault(t *testing.T) {
actual, err := cmd.ParseArgs([]string{"goverter", "gen", "pattern"})
require.NoError(t, err)

expected := &goverter.GenerateConfig{
expected := &cmd.GenerateConfig{
PackagePatterns: []string{"pattern"},
WorkingDir: "",
OutputBuildConstraint: "!goverter",
BuildTags: "goverter",
EnumTransformers: map[string]enum.Transformer{},
Global: config.RawLines{
Location: "command line (-g, -global)",
Lines: nil,
Expand Down
15 changes: 6 additions & 9 deletions runner.go → cmd/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goverter
package cmd

import (
"os"
Expand All @@ -24,21 +24,19 @@ type GenerateConfig struct {
OutputBuildConstraint string
// EnumTransformers describes additional enum transformers usable in the enum:transform setting.
EnumTransformers map[string]enum.Transformer
// EnumDetecter is used to detect enum types and its members.
EnumDetecter enum.Detecter
}

// GenerateConverters generates converters.
// GenerateConverters generate and writes converters to files.
func GenerateConverters(c *GenerateConfig) error {
files, err := generateConvertersRaw(c)
files, err := GenerateConvertersRaw(c)
if err != nil {
return err
}

return writeFiles(files)
return WriteFiles(files)
}

func generateConvertersRaw(c *GenerateConfig) (map[string][]byte, error) {
func GenerateConvertersRaw(c *GenerateConfig) (map[string][]byte, error) {
rawConverters, err := comments.ParseDocs(comments.ParseDocsConfig{
BuildTags: c.BuildTags,
PackagePattern: c.PackagePatterns,
Expand All @@ -57,7 +55,6 @@ func generateConvertersRaw(c *GenerateConfig) (map[string][]byte, error) {
OuputBuildConstraint: c.OutputBuildConstraint,

EnumTransformers: c.EnumTransformers,
EnumDetecter: c.EnumDetecter,
})
if err != nil {
return nil, err
Expand All @@ -69,7 +66,7 @@ func generateConvertersRaw(c *GenerateConfig) (map[string][]byte, error) {
})
}

func writeFiles(files map[string][]byte) error {
func WriteFiles(files map[string][]byte) error {
for path, content := range files {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
Expand Down
15 changes: 3 additions & 12 deletions cmd/goverter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ import (
"fmt"
"os"

goverter "github.com/jmattheis/goverter"
"github.com/jmattheis/goverter/cli"
"github.com/jmattheis/goverter/cmd"
)

func main() {
cfg, err := cli.Parse(os.Args)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

if err = goverter.GenerateConverters(cfg); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
_, _ = fmt.Fprintln(os.Stderr, "github.com/jmattheis/goverter/cmd/goverter is deprecated, use github.com/jmattheis/goverter instead.")
cmd.Run(os.Args, cmd.RunOpts{})
}
30 changes: 30 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"fmt"
"os"

"github.com/jmattheis/goverter/enum"
)

type RunOpts struct {
EnumTransformers map[string]enum.Transformer
}

func Run(args []string, opts RunOpts) {
cfg, err := ParseArgs(args)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

if opts.EnumTransformers != nil {
for key, value := range opts.EnumTransformers {
cfg.EnumTransformers[key] = value
}
}
if err = GenerateConverters(cfg); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
6 changes: 3 additions & 3 deletions example/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run github.com/jmattheis/goverter/cmd/goverter gen ./...
//go:generate go run github.com/jmattheis/goverter/cmd/goverter gen -cwd ./wrap-errors-using ./
//go:generate go run github.com/jmattheis/goverter/cmd/goverter gen -cwd ./protobuf ./
//go:generate go run github.com/jmattheis/goverter gen ./...
//go:generate go run github.com/jmattheis/goverter gen -cwd ./wrap-errors-using ./
//go:generate go run github.com/jmattheis/goverter gen -cwd ./protobuf ./
package example
9 changes: 5 additions & 4 deletions runner_test.go → scenario_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goverter
package main

import (
"fmt"
Expand All @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"github.com/jmattheis/goverter/cmd"
"github.com/jmattheis/goverter/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -73,8 +74,8 @@ func TestScenario(t *testing.T) {
patterns = append(patterns, "github.com/jmattheis/goverter/execution")
}

files, err := generateConvertersRaw(
&GenerateConfig{
files, err := cmd.GenerateConvertersRaw(
&cmd.GenerateConfig{
WorkingDir: testWorkDir,
PackagePatterns: patterns,
OutputBuildConstraint: scenario.BuildConstraint,
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestScenario(t *testing.T) {
require.NotEmpty(t, scenario.Success, "scenario.Success may not be empty")
require.Equal(t, scenario.Success, actualOutputFiles)

err = writeFiles(files)
err = cmd.WriteFiles(files)
require.NoError(t, err)
require.NoError(t, compile(testWorkDir), "generated converter doesn't build")
})
Expand Down

0 comments on commit 99803cb

Please sign in to comment.