Skip to content

Commit

Permalink
Simplify recursive init outputs
Browse files Browse the repository at this point in the history
Follow up of #2150

The `strings.Builder`-based approach is complicated to implement,
so we'll change it to a simpler output.
  • Loading branch information
wata727 committed Jan 12, 2025
1 parent 7a23f6a commit 83ea536
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 51 deletions.
63 changes: 17 additions & 46 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package cmd
import (
"errors"
"fmt"
"log"
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/afero"
Expand All @@ -24,43 +22,33 @@ func (cli *CLI) init(opts Options) int {
return ExitCodeError
}

var builder strings.Builder

if opts.Recursive {
fmt.Fprint(cli.outStream, "Installing plugins on each working directory...\n\n")
}

any_installed := false
installed := false
for _, wd := range workingDirs {
builder.Reset()
err := cli.withinChangedDir(wd, func() error {
installed := false
if opts.Recursive {
builder.WriteString("====================================================\n")
builder.WriteString(fmt.Sprintf("working directory: %s\n\n", wd))
}

cfg, err := tflint.LoadConfig(afero.Afero{Fs: afero.NewOsFs()}, opts.Config)
if err != nil {
fmt.Fprint(cli.outStream, builder.String())
return fmt.Errorf("Failed to load TFLint config; %w", err)
if opts.Recursive {
return fmt.Errorf("Failed to load TFLint config in %s; %w", wd, err)
} else {
return fmt.Errorf("Failed to load TFLint config; %w", err)
}
}

found := false
for _, pluginCfg := range cfg.Plugins {
installCfg := plugin.NewInstallConfig(cfg, pluginCfg)

// If version or source is not set, you need to install it manually
if installCfg.ManuallyInstalled() {
continue
}
found = true

_, err := plugin.FindPluginPath(installCfg)
if os.IsNotExist(err) {
fmt.Fprint(cli.outStream, builder.String())
builder.Reset()
fmt.Fprintf(cli.outStream, "Installing \"%s\" plugin...\n", pluginCfg.Name)
if opts.Recursive {
fmt.Fprintf(cli.outStream, "Installing \"%s\" plugin in %s...\n", pluginCfg.Name, wd)
} else {
fmt.Fprintf(cli.outStream, "Installing \"%s\" plugin...\n", pluginCfg.Name)
}

_, err = installCfg.Install()
if err != nil {
Expand All @@ -71,34 +59,17 @@ func (cli *CLI) init(opts Options) int {
}
}

any_installed = true
installed = true
fmt.Fprintf(cli.outStream, "Installed \"%s\" (source: %s, version: %s)\n", pluginCfg.Name, pluginCfg.Source, pluginCfg.Version)
}

if err != nil {
fmt.Fprint(cli.outStream, builder.String())
return fmt.Errorf("Failed to find a plugin; %w", err)
if opts.Recursive {
return fmt.Errorf("Failed to find a plugin in %s; %w", wd, err)
} else {
return fmt.Errorf("Failed to find a plugin; %w", err)
}
}

builder.WriteString(fmt.Sprintf("Plugin \"%s\" is already installed\n", pluginCfg.Name))
}

if opts.Recursive && !found {
builder.WriteString("No plugins to install\n")
}

if installed || !opts.Recursive {
fmt.Fprint(cli.outStream, builder.String())
return nil
}

// If there are no changes, send logs to debug
prefix := "[DEBUG] "
lines := strings.Split(builder.String(), "\n")

for _, line := range lines {
log.Printf("%s%s", prefix, line)
}

return nil
Expand All @@ -108,7 +79,7 @@ func (cli *CLI) init(opts Options) int {
return ExitCodeError
}
}
if opts.Recursive && !any_installed {
if !installed {
fmt.Fprint(cli.outStream, "All plugins are already installed\n")
}

Expand Down
7 changes: 2 additions & 5 deletions integrationtest/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestIntegration(t *testing.T) {
}

cli.Run([]string{"./tflint", "--init"})
if !strings.Contains(outStream.String(), `Plugin "aws" is already installed`) {
if !strings.Contains(outStream.String(), `All plugins are already installed`) {
t.Fatalf("Expected to contain an already installed log, but did not: stdout=%s, stderr=%s", outStream, errStream)
}

Expand All @@ -74,7 +74,7 @@ func TestIntegration(t *testing.T) {
}

cli.Run([]string{"./tflint", "--chdir", "basic", "--init"})
if !strings.Contains(outStream.String(), `Plugin "aws" is already installed`) {
if !strings.Contains(outStream.String(), `All plugins are already installed`) {
t.Fatalf("Expected to contain an already installed log, but did not: stdout=%s, stderr=%s", outStream, errStream)
}

Expand All @@ -94,9 +94,6 @@ func TestIntegration(t *testing.T) {
}

cli.Run([]string{"./tflint", "--recursive", "--init"})
if !strings.Contains(outStream.String(), "Installing plugins on each working directory...") {
t.Fatalf("Expected to contain working dir log, but did not: stdout=%s, stderr=%s", outStream, errStream)
}
if !strings.Contains(outStream.String(), "All plugins are already installed") {
t.Fatalf("Expected to contain already installed log, but did not: stdout=%s, stderr=%s", outStream, errStream)
}
Expand Down

0 comments on commit 83ea536

Please sign in to comment.