Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Simplify recursive init outputs #2204

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading