Skip to content

Commit

Permalink
Spit and polish (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnstoppableMango authored Dec 20, 2024
1 parent 5eaad3a commit d2175de
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 91 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaO
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/unmango/go v0.0.28 h1:Nyni154Sq3qJm8QPYQvh+4qfSLq4ABBR+qS5d/4qV5g=
github.com/unmango/go v0.0.28/go.mod h1:3fpXaemxHO2HYJnxw8WqpYjnk3/msBEeYaIhh87ims8=
github.com/unmango/go v0.0.29 h1:ZBjJHFPKd4L0b0cez/4PfoszRLcNcGiCZELCYY5EJSE=
github.com/unmango/go v0.0.29/go.mod h1:/qlvxBPfsiji2ugRHCIA0yMIwDo5fn0Ih8pL4B969Ec=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
Expand Down
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")

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", t.Dotnet.Enabled, "")
f.StringVar(&t.Dotnet.Name, "dotnetName", t.Dotnet.Name, "")
f.StringVar(&t.Dotnet.Path, "dotnetPath", t.Dotnet.Path, "")

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

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

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

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

return f.Parse(args)
}

func Parse(args []string) (o Options, err error) {
err = o.Apply(args)
return
}
Loading

0 comments on commit d2175de

Please sign in to comment.