• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 26877520721

03 Jun 2026 09:59AM UTC coverage: 71.036% (-0.03%) from 71.063%
26877520721

push

github

web-flow
#1391 terraform completion (#1989)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

4530 of 7066 branches covered (64.11%)

Branch coverage included in aggregate %.

11728 of 15821 relevant lines covered (74.13%)

3.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.7
cli/src/main/java/com/devonfw/tools/ide/commandlet/EnvironmentCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.nio.file.Path;
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.stream.Collectors;
8

9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

12
import com.devonfw.tools.ide.context.AbstractIdeContext;
13
import com.devonfw.tools.ide.context.IdeContext;
14
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
15
import com.devonfw.tools.ide.environment.VariableLine;
16
import com.devonfw.tools.ide.environment.VariableSource;
17
import com.devonfw.tools.ide.log.IdeLogLevel;
18
import com.devonfw.tools.ide.os.WindowsPathSyntax;
19
import com.devonfw.tools.ide.process.EnvironmentContext;
20
import com.devonfw.tools.ide.process.EnvironmentVariableCollectorContext;
21
import com.devonfw.tools.ide.property.FlagProperty;
22
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
23
import com.devonfw.tools.ide.tool.ToolInstallation;
24
import com.devonfw.tools.ide.version.VersionIdentifier;
25

26
/**
27
 * {@link Commandlet} to print the environment variables.
28
 */
29
public final class EnvironmentCommandlet extends Commandlet {
30

31
  private static final Logger LOG = LoggerFactory.getLogger(EnvironmentCommandlet.class);
4✔
32

33
  /** {@link FlagProperty} to enable Bash (MSys) path conversion on Windows. */
34
  public final FlagProperty bash;
35

36
  /**
37
   * The constructor.
38
   *
39
   * @param context the {@link IdeContext}.
40
   */
41
  public EnvironmentCommandlet(IdeContext context) {
42

43
    super(context);
3✔
44
    addKeyword(getName());
4✔
45
    this.bash = add(new FlagProperty("--bash"));
9✔
46
  }
1✔
47

48
  @Override
49
  public String getName() {
50

51
    return "env";
2✔
52
  }
53

54
  @Override
55
  public boolean isIdeHomeRequired() {
56

57
    return false;
2✔
58
  }
59

60
  @Override
61
  public boolean isProcessableOutput() {
62

63
    return true;
×
64
  }
65

66
  @Override
67
  protected void doRun() {
68

69
    boolean winCmd = false;
2✔
70
    WindowsPathSyntax pathSyntax = null;
2✔
71
    if (this.context.getSystemInfo().isWindows()) {
5✔
72
      if (this.bash.isTrue()) {
4!
73
        pathSyntax = WindowsPathSyntax.MSYS;
×
74
      } else {
75
        winCmd = true;
2✔
76
        pathSyntax = WindowsPathSyntax.WINDOWS;
2✔
77
      }
78
    }
79

80
    ((AbstractIdeContext) this.context).setPathSyntax(pathSyntax);
5✔
81
    List<VariableLine> variables = this.context.getVariables().collectVariables();
5✔
82
    Map<String, VariableLine> variableMap = variables.stream().collect(Collectors.toMap(VariableLine::getName, v -> v));
10✔
83

84
    Path softwarePath = this.context.getSoftwarePath();
4✔
85
    if (softwarePath != null) {
2✔
86
      EnvironmentVariableCollectorContext environmentVariableCollectorContext = new EnvironmentVariableCollectorContext(variableMap,
11✔
87
          new VariableSource(EnvironmentVariablesType.TOOL, softwarePath), pathSyntax);
88
      setEnvironmentVariablesInLocalTools(environmentVariableCollectorContext);
3✔
89
    }
90

91
    printLines(variableMap, winCmd);
4✔
92

93
    // Bash completions must be printed after the environment variables because they may reference variables such as TERRAFORM_HOME.
94
    if (this.bash.isTrue()) {
4✔
95
      printBashCompletions();
2✔
96
    }
97
  }
1✔
98

99
  private void printLines(Map<String, VariableLine> variableMap, boolean winCmd) {
100
    if (LOG.isDebugEnabled()) {
3✔
101
      Map<EnvironmentVariablesType, List<VariableLine>> type2lines = variableMap.values().stream().collect(Collectors.groupingBy(l -> l.getSource().type()));
12✔
102
      for (EnvironmentVariablesType type : EnvironmentVariablesType.values()) {
16✔
103
        List<VariableLine> lines = type2lines.get(type);
5✔
104
        if (lines != null) {
2✔
105
          boolean sourcePrinted = false;
2✔
106
          sortVariables(lines);
2✔
107
          for (VariableLine line : lines) {
10✔
108
            if (!sourcePrinted) {
2✔
109
              LOG.debug("from {}:", line.getSource());
5✔
110
              sourcePrinted = true;
2✔
111
            }
112
            IdeLogLevel.PROCESSABLE.log(LOG, format(line, winCmd));
7✔
113
          }
1✔
114
        }
115
      }
116
    } else {
1✔
117
      List<VariableLine> variables = new ArrayList<>(variableMap.values());
6✔
118
      sortVariables(variables);
2✔
119
      for (VariableLine line : variables) {
10✔
120
        IdeLogLevel.PROCESSABLE.log(LOG, format(line, winCmd));
7✔
121
      }
1✔
122
    }
123
  }
1✔
124

125
  private static void sortVariables(List<VariableLine> lines) {
126

127
    lines.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
9✔
128
  }
1✔
129

130
  private String format(VariableLine line, boolean winCmd) {
131

132
    if (winCmd) {
2✔
133
      return line.getName() + "=" + line.getValue();
6✔
134
    } else {
135
      String lineValue = line.getValue();
3✔
136
      lineValue = "\"" + lineValue + "\"";
3✔
137
      line = line.withValue(lineValue);
4✔
138
      return line.toString();
3✔
139
    }
140
  }
141

142
  private void setEnvironmentVariablesInLocalTools(EnvironmentContext environmentContext) {
143
    // installed tools in IDE_HOME/software
144
    for (Commandlet commandlet : this.context.getCommandletManager().getCommandlets()) {
13✔
145
      if (commandlet instanceof LocalToolCommandlet tool) {
6✔
146
        try {
147
          if (tool.isInstalled()) {
3✔
148
            // for performance optimization, we do a hack here and assume that the installedVersion is never used by any setEnvironment method implementation.
149
            VersionIdentifier installedVersion = VersionIdentifier.LATEST;
2✔
150
            ToolInstallation toolInstallation = new ToolInstallation(tool.getToolPath(), tool.getToolPath(),
7✔
151
                tool.getToolBinPath(), installedVersion, false);
5✔
152
            tool.setEnvironment(environmentContext, toolInstallation, false);
5✔
153
          }
154
        } catch (Exception e) {
×
155
          LOG.warn("An error occurred while setting the environment variables in local tools:", e);
×
156
        }
1✔
157
      }
158
    }
1✔
159
  }
1✔
160

161
  /**
162
   * Prints optional Bash completion setup commands for installed local tools.
163
   * <p>
164
   * These lines are only emitted for {@code env --bash}. The IDEasy shell wrapper evaluates this output, so tools can dynamically register completions without
165
   * modifying user files such as {@code ~/.bashrc}.
166
   */
167
  private void printBashCompletions() {
168

169
    for (Commandlet commandlet : this.context.getCommandletManager().getCommandlets()) {
13✔
170
      if (commandlet instanceof LocalToolCommandlet tool) {
6✔
171
        try {
172
          if (tool.isInstalled()) {
3!
173
            String bashCompletion = tool.getBashCompletion();
×
174
            if ((bashCompletion != null) && !bashCompletion.isBlank()) {
×
175
              IdeLogLevel.PROCESSABLE.log(LOG, bashCompletion);
×
176
            }
177
          }
178
        } catch (Exception e) {
1✔
179
          LOG.warn("An error occurred while collecting Bash completion for tool {}.", tool.getName(), e);
6✔
180
        }
1✔
181
      }
182
    }
1✔
183
  }
1✔
184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc