Skip to content

Commit

Permalink
Spit and polish
Browse files Browse the repository at this point in the history
  • Loading branch information
UnstoppableMango committed Dec 19, 2024
1 parent aea9093 commit 3f951e4
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 89 deletions.
23 changes: 23 additions & 0 deletions pkg/cmd/internal/cwd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package internal

import (
"os"
"path/filepath"
)

// Cwd cleans and roots [op] or retrieves the current directory
func Cwd(op string) (cwd string, err error) {
if cwd = op; op == "" {
if cwd, err = os.Getwd(); err != nil {
return
}
}

if !filepath.IsAbs(cwd) {
if cwd, err = filepath.Abs(cwd); err != nil {
return
}
}

return
}
14 changes: 13 additions & 1 deletion pkg/cmd/internal/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

func PrintFs(fsys afero.Fs) error {
return afero.Walk(fsys, "",
var count int

err := afero.Walk(fsys, "",
func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -17,8 +19,18 @@ func PrintFs(fsys afero.Fs) error {
return nil
}

count++
_, err = fmt.Println(path)
return err
},
)
if err != nil {
return err
}

if count == 0 {
fmt.Println("no output")
}

return nil
}
27 changes: 21 additions & 6 deletions pkg/cmd/tool.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"os"
"fmt"

"github.com/charmbracelet/log"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/unmango/go/fs/ignore"
"github.com/unstoppablemango/tdl/internal/util"
"github.com/unstoppablemango/tdl/pkg/cmd/internal"
"github.com/unstoppablemango/tdl/pkg/logging"
"github.com/unstoppablemango/tdl/pkg/plugin"
"github.com/unstoppablemango/tdl/pkg/target"
"github.com/unstoppablemango/tdl/pkg/tool"
Expand All @@ -17,11 +18,14 @@ import (
var DefaultIgnorePatterns = tool.DefaultIgnorePatterns

func NewTool() *cobra.Command {
return &cobra.Command{
var cwd string

cmd := &cobra.Command{
Use: "tool [NAME]",
Short: "Execute a tool",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
logging.Init()
t, err := target.Parse(args[0])
if err != nil {
util.Fail(err)
Expand All @@ -33,27 +37,38 @@ func NewTool() *cobra.Command {
util.Fail(err)
}

work, err := os.Getwd()
if err != nil {
if cwd, err = internal.Cwd(cwd); err != nil {
util.Fail(err)
}

src := afero.NewBasePathFs(afero.NewOsFs(), work)
src := afero.NewBasePathFs(afero.NewOsFs(), cwd)
if i, err := internal.OpenGitIgnore(ctx); err != nil {
log.Info("not a git repo", "err", err)
src = ignore.NewFsFromGitIgnoreLines(src, DefaultIgnorePatterns...)
} else if src, err = ignore.NewFsFromGitIgnoreReader(src, i); err != nil {
util.Fail(err)
}

out, err := tool.Execute(ctx, src)
extraArgs := []string{}
if l := cmd.Flags().ArgsLenAtDash(); l > 0 {
extraArgs = args[l:]
}

log.Debug("executing", "tool", tool, "cwd", cwd, "args", extraArgs)
out, err := tool.Execute(ctx, src, extraArgs)
if err != nil {
util.Fail(err)
}

fmt.Println("successfully executed")
if err = internal.PrintFs(out); err != nil {
util.Fail(err)
}
},
}

cmd.Flags().StringVarP(&cwd, "cwd", "C", "", "sets the working directory")
cmd.MarkFlagDirname("cwd")

Check failure on line 71 in pkg/cmd/tool.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `cmd.MarkFlagDirname` is not checked (errcheck)

return cmd
}
2 changes: 1 addition & 1 deletion pkg/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Generator interface {

type Tool interface {
fmt.Stringer
Execute(context.Context, afero.Fs) (afero.Fs, error)
Execute(context.Context, afero.Fs, []string) (afero.Fs, error)
}

type Plugin interface {
Expand Down
86 changes: 58 additions & 28 deletions pkg/tool/crd2pulumi/options.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
package crd2pulumi

import "path/filepath"
import (
"path/filepath"

"github.com/spf13/pflag"
)

type LangOptions struct {
Enabled bool
Name string
Path string
}

func (o LangOptions) IsZero() bool {
return !o.Enabled && o.Name == "" && o.Path == ""
}

type Options struct {
NodeJS *LangOptions
Python *LangOptions
Dotnet *LangOptions
Go *LangOptions
Java *LangOptions
NodeJS LangOptions
Python LangOptions
Dotnet LangOptions
Go LangOptions
Java LangOptions
Force bool
Version string
}

func (t Options) langs() map[string]*LangOptions {
return map[string]*LangOptions{
"nodejs": t.NodeJS,
"python": t.Python,
"dotnet": t.Dotnet,
"golang": t.Go,
"java": t.Java,
func (o Options) langs() map[string]LangOptions {
return map[string]LangOptions{
"nodejs": o.NodeJS,
"python": o.Python,
"dotnet": o.Dotnet,
"golang": o.Go,
"java": o.Java,
}
}

func (t Options) Paths(root string) map[string]string {
func (o Options) Paths(root string) map[string]string {
paths := map[string]string{}
for k, v := range t.langs() {
if v == nil {
continue
}

for k, v := range o.langs() {
if v.Path != "" {
paths[k] = v.Path
} else {
Expand All @@ -45,13 +49,9 @@ func (t Options) Paths(root string) map[string]string {
return paths
}

func (t Options) Args(paths map[string]string) []string {
func (o Options) Args(paths map[string]string) []string {
args := ArgBuilder{}
for k, v := range t.langs() {
if v == nil {
continue
}

for k, v := range o.langs() {
if v.Enabled {
args = args.LangOpt(k)
}
Expand All @@ -63,12 +63,42 @@ func (t Options) Args(paths map[string]string) []string {
}
}

if t.Version != "" {
args = args.VersionOpt(t.Version)
if o.Version != "" {
args = args.VersionOpt(o.Version)
}
if t.Force {
if o.Force {
args = args.ForceOpt()
}

return args
}

func (t *Options) apply(args []string) error {
f := pflag.NewFlagSet("crd2pulumi", pflag.ContinueOnError)

f.BoolVarP(&t.Dotnet.Enabled, "dotnet", "d", false, "")
f.StringVar(&t.Dotnet.Name, "dotnetName", "", "")
f.StringVar(&t.Dotnet.Path, "dotnetPath", "", "")

f.BoolVarP(&t.Go.Enabled, "go", "g", false, "")
f.StringVar(&t.Go.Name, "goName", "", "")
f.StringVar(&t.Go.Path, "goPath", "", "")

f.BoolVarP(&t.NodeJS.Enabled, "nodejs", "n", false, "")
f.StringVar(&t.NodeJS.Name, "nodejsName", "", "")
f.StringVar(&t.NodeJS.Path, "nodejsPath", "", "")

f.BoolVarP(&t.Python.Enabled, "python", "p", false, "")
f.StringVar(&t.Python.Name, "pythonName", "", "")
f.StringVar(&t.Python.Path, "pythonPath", "", "")

f.BoolVarP(&t.Force, "force", "f", false, "")
f.StringVarP(&t.Version, "version", "v", "", "")

return f.Parse(args)
}

func Parse(args []string) (o Options, err error) {
err = o.apply(args)
return
}
93 changes: 55 additions & 38 deletions pkg/tool/crd2pulumi/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var _ = Describe("Options", func() {
func(key, value string) {
root := "blah"
t := crd2pulumi.Options{
NodeJS: &crd2pulumi.LangOptions{},
Python: &crd2pulumi.LangOptions{},
Dotnet: &crd2pulumi.LangOptions{},
Go: &crd2pulumi.LangOptions{},
Java: &crd2pulumi.LangOptions{},
NodeJS: crd2pulumi.LangOptions{},
Python: crd2pulumi.LangOptions{},
Dotnet: crd2pulumi.LangOptions{},
Go: crd2pulumi.LangOptions{},
Java: crd2pulumi.LangOptions{},
}

paths := t.Paths(root)
Expand All @@ -40,11 +40,11 @@ var _ = Describe("Options", func() {
Entry("java", "java", "blah/java"),
func(key, value string) {
t := crd2pulumi.Options{
NodeJS: &crd2pulumi.LangOptions{Path: value},
Python: &crd2pulumi.LangOptions{Path: value},
Dotnet: &crd2pulumi.LangOptions{Path: value},
Go: &crd2pulumi.LangOptions{Path: value},
Java: &crd2pulumi.LangOptions{Path: value},
NodeJS: crd2pulumi.LangOptions{Path: value},
Python: crd2pulumi.LangOptions{Path: value},
Dotnet: crd2pulumi.LangOptions{Path: value},
Go: crd2pulumi.LangOptions{Path: value},
Java: crd2pulumi.LangOptions{Path: value},
}

paths := t.Paths("doesn't matter")
Expand All @@ -53,31 +53,13 @@ var _ = Describe("Options", func() {
},
)

It("should exclude nil options", func() {
t := crd2pulumi.Options{
NodeJS: &crd2pulumi.LangOptions{},
Python: nil,
Dotnet: nil,
Go: nil,
Java: nil,
}

paths := t.Paths("doesn't matter")

Expect(paths).To(HaveKey("nodejs"))
Expect(paths).NotTo(HaveKey("python"))
Expect(paths).NotTo(HaveKey("dotnet"))
Expect(paths).NotTo(HaveKey("golang"))
Expect(paths).NotTo(HaveKey("java"))
})

It("should include a path when an option is enabled", func() {
t := crd2pulumi.Options{
NodeJS: &crd2pulumi.LangOptions{Enabled: true},
Python: nil,
Dotnet: nil,
Go: nil,
Java: nil,
NodeJS: crd2pulumi.LangOptions{Enabled: true},
Python: crd2pulumi.LangOptions{},
Dotnet: crd2pulumi.LangOptions{},
Go: crd2pulumi.LangOptions{},
Java: crd2pulumi.LangOptions{},
}

paths := t.Paths("/root")
Expand All @@ -89,11 +71,11 @@ var _ = Describe("Options", func() {
Describe("Args", func() {
It("should work", func() {
t := crd2pulumi.Options{
NodeJS: &crd2pulumi.LangOptions{Path: "doesn't matter, the path is take from paths"},
Python: &crd2pulumi.LangOptions{Name: "peethon"},
Dotnet: &crd2pulumi.LangOptions{Enabled: true},
Go: &crd2pulumi.LangOptions{},
Java: &crd2pulumi.LangOptions{},
NodeJS: crd2pulumi.LangOptions{Path: "doesn't matter, the path is take from paths"},
Python: crd2pulumi.LangOptions{Name: "peethon"},
Dotnet: crd2pulumi.LangOptions{Enabled: true},
Go: crd2pulumi.LangOptions{},
Java: crd2pulumi.LangOptions{},
Force: true,
Version: "v420",
}
Expand All @@ -114,4 +96,39 @@ var _ = Describe("Options", func() {
))
})
})

Describe("Parse", func() {
It("should return empty options", func() {
o, err := crd2pulumi.Parse([]string{})

Expect(err).NotTo(HaveOccurred())
Expect(o.Dotnet.Enabled).To(BeFalse())
Expect(o.Dotnet.Name).To(BeEmpty())
Expect(o.Dotnet.Path).To(BeEmpty())
Expect(o.Go.Enabled).To(BeFalse())
Expect(o.Go.Name).To(BeEmpty())
Expect(o.Go.Path).To(BeEmpty())
Expect(o.NodeJS.Enabled).To(BeFalse())
Expect(o.NodeJS.Name).To(BeEmpty())
Expect(o.NodeJS.Path).To(BeEmpty())
Expect(o.Python.Enabled).To(BeFalse())
Expect(o.Python.Name).To(BeEmpty())
Expect(o.Python.Path).To(BeEmpty())
Expect(o.Force).To(BeFalse())
Expect(o.Version).To(BeEmpty())
})

It("should enable dotnet options", func() {
o, err := crd2pulumi.Parse([]string{
"--dotnet",
"--dotnetName", "bleh",
"--dotnetPath", "blah",
})

Expect(err).NotTo(HaveOccurred())
Expect(o.Dotnet.Enabled).To(BeTrue())
Expect(o.Dotnet.Name).To(Equal("bleh"))
Expect(o.Dotnet.Path).To(Equal("blah"))
})
})
})
Loading

0 comments on commit 3f951e4

Please sign in to comment.