diff --git a/cmd/init.go b/cmd/init.go index 59ddaf87d..d233ae56e 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -3,9 +3,7 @@ package cmd import ( "errors" "fmt" - "log" "os" - "strings" "github.com/fatih/color" "github.com/spf13/afero" @@ -24,29 +22,18 @@ 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) @@ -54,13 +41,14 @@ func (cli *CLI) init(opts Options) int { 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 { @@ -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 @@ -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") } diff --git a/integrationtest/init/init_test.go b/integrationtest/init/init_test.go index 43409ab92..707bee504 100644 --- a/integrationtest/init/init_test.go +++ b/integrationtest/init/init_test.go @@ -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) } @@ -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) } @@ -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) }