Skip to content

Commit

Permalink
feat(deps): installing the goimports dep (#44)
Browse files Browse the repository at this point in the history
installing the goimports dep
  • Loading branch information
Jacobbrewer1 authored Nov 3, 2024
1 parent 59af4f8 commit 9f80c3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (g *generateCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface
return subcommands.ExitFailure
}

if err := generation.GoimportsInstallIfNeeded(); err != nil {
slog.Error("Error installing goimports", slog.String("error", err.Error()))
return subcommands.ExitFailure
}

tables, err := generation.LoadSQL(g.sqlLocation)
if err != nil {
slog.Error("Error loading SQL", slog.String("templatesLocation", g.templatesLocation), slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
Expand Down
24 changes: 24 additions & 0 deletions pkg/generation/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"text/template"

Expand Down Expand Up @@ -83,3 +84,26 @@ func generate(t *templateInfo, tmpl *template.Template, outputLoc string, fileEx

return tmpl.Execute(f, t)
}

func GoimportsInstallIfNeeded() error {
if !IsGoimportsInstalled() {
slog.Info("Installing goimports")
if err := InstallGoimports(); err != nil {
return fmt.Errorf("error installing goimports: %w", err)
}
}

return nil
}

func IsGoimportsInstalled() bool {
_, err := exec.LookPath("goimports")
return err == nil
}

func InstallGoimports() error {
cmd := exec.Command("go", "install", "golang.org/x/tools/cmd/goimports@latest")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

0 comments on commit 9f80c3c

Please sign in to comment.