Skip to content

Commit ced1af6

Browse files
authored
Simplify recursive init outputs (#2204)
Follow up of #2150 The `strings.Builder`-based approach is complicated to implement, so we'll change it to a simpler output.
1 parent 7a23f6a commit ced1af6

File tree

2 files changed

+19
-51
lines changed

2 files changed

+19
-51
lines changed

cmd/init.go

+17-46
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6-
"log"
76
"os"
8-
"strings"
97

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

27-
var builder strings.Builder
28-
29-
if opts.Recursive {
30-
fmt.Fprint(cli.outStream, "Installing plugins on each working directory...\n\n")
31-
}
32-
33-
any_installed := false
25+
installed := false
3426
for _, wd := range workingDirs {
35-
builder.Reset()
3627
err := cli.withinChangedDir(wd, func() error {
37-
installed := false
38-
if opts.Recursive {
39-
builder.WriteString("====================================================\n")
40-
builder.WriteString(fmt.Sprintf("working directory: %s\n\n", wd))
41-
}
42-
4328
cfg, err := tflint.LoadConfig(afero.Afero{Fs: afero.NewOsFs()}, opts.Config)
4429
if err != nil {
45-
fmt.Fprint(cli.outStream, builder.String())
46-
return fmt.Errorf("Failed to load TFLint config; %w", err)
30+
if opts.Recursive {
31+
return fmt.Errorf("Failed to load TFLint config in %s; %w", wd, err)
32+
} else {
33+
return fmt.Errorf("Failed to load TFLint config; %w", err)
34+
}
4735
}
4836

49-
found := false
5037
for _, pluginCfg := range cfg.Plugins {
5138
installCfg := plugin.NewInstallConfig(cfg, pluginCfg)
5239

5340
// If version or source is not set, you need to install it manually
5441
if installCfg.ManuallyInstalled() {
5542
continue
5643
}
57-
found = true
5844

5945
_, err := plugin.FindPluginPath(installCfg)
6046
if os.IsNotExist(err) {
61-
fmt.Fprint(cli.outStream, builder.String())
62-
builder.Reset()
63-
fmt.Fprintf(cli.outStream, "Installing \"%s\" plugin...\n", pluginCfg.Name)
47+
if opts.Recursive {
48+
fmt.Fprintf(cli.outStream, "Installing \"%s\" plugin in %s...\n", pluginCfg.Name, wd)
49+
} else {
50+
fmt.Fprintf(cli.outStream, "Installing \"%s\" plugin...\n", pluginCfg.Name)
51+
}
6452

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

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

7966
if err != nil {
80-
fmt.Fprint(cli.outStream, builder.String())
81-
return fmt.Errorf("Failed to find a plugin; %w", err)
67+
if opts.Recursive {
68+
return fmt.Errorf("Failed to find a plugin in %s; %w", wd, err)
69+
} else {
70+
return fmt.Errorf("Failed to find a plugin; %w", err)
71+
}
8272
}
83-
84-
builder.WriteString(fmt.Sprintf("Plugin \"%s\" is already installed\n", pluginCfg.Name))
85-
}
86-
87-
if opts.Recursive && !found {
88-
builder.WriteString("No plugins to install\n")
89-
}
90-
91-
if installed || !opts.Recursive {
92-
fmt.Fprint(cli.outStream, builder.String())
93-
return nil
94-
}
95-
96-
// If there are no changes, send logs to debug
97-
prefix := "[DEBUG] "
98-
lines := strings.Split(builder.String(), "\n")
99-
100-
for _, line := range lines {
101-
log.Printf("%s%s", prefix, line)
10273
}
10374

10475
return nil
@@ -108,7 +79,7 @@ func (cli *CLI) init(opts Options) int {
10879
return ExitCodeError
10980
}
11081
}
111-
if opts.Recursive && !any_installed {
82+
if !installed {
11283
fmt.Fprint(cli.outStream, "All plugins are already installed\n")
11384
}
11485

integrationtest/init/init_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestIntegration(t *testing.T) {
5454
}
5555

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

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

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

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

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

0 commit comments

Comments
 (0)