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

devonfw / IDEasy / 12875818493

20 Jan 2025 09:19PM UTC coverage: 68.372% (-0.1%) from 68.512%
12875818493

push

github

web-flow
#939: fix NPE on path completion and implement repository completion (#956)

2737 of 4375 branches covered (62.56%)

Branch coverage included in aggregate %.

7075 of 9976 relevant lines covered (70.92%)

3.09 hits per line

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

3.14
cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.io.IOException;
4
import java.nio.file.Path;
5
import java.nio.file.Paths;
6
import java.util.Iterator;
7

8
import org.fusesource.jansi.AnsiConsole;
9
import org.jline.reader.Completer;
10
import org.jline.reader.EndOfFileException;
11
import org.jline.reader.LineReader;
12
import org.jline.reader.LineReaderBuilder;
13
import org.jline.reader.MaskingCallback;
14
import org.jline.reader.Parser;
15
import org.jline.reader.UserInterruptException;
16
import org.jline.reader.impl.DefaultParser;
17
import org.jline.reader.impl.completer.AggregateCompleter;
18
import org.jline.reader.impl.completer.StringsCompleter;
19
import org.jline.terminal.Terminal;
20
import org.jline.terminal.TerminalBuilder;
21
import org.jline.widget.AutosuggestionWidgets;
22

23
import com.devonfw.tools.ide.cli.CliArgument;
24
import com.devonfw.tools.ide.cli.CliArguments;
25
import com.devonfw.tools.ide.cli.CliException;
26
import com.devonfw.tools.ide.completion.IdeCompleter;
27
import com.devonfw.tools.ide.context.AbstractIdeContext;
28
import com.devonfw.tools.ide.context.IdeContext;
29
import com.devonfw.tools.ide.property.BooleanProperty;
30
import com.devonfw.tools.ide.property.KeywordProperty;
31
import com.devonfw.tools.ide.property.Property;
32

33
/**
34
 * {@link Commandlet} for internal interactive shell with build-in auto-completion and help.
35
 */
36
public final class ShellCommandlet extends Commandlet {
37

38
  private static final int AUTOCOMPLETER_MAX_RESULTS = 50;
39

40
  private static final int RC_EXIT = 987654321;
41

42
  /**
43
   * The constructor.
44
   *
45
   * @param context the {@link IdeContext}.
46
   */
47
  public ShellCommandlet(IdeContext context) {
48

49
    super(context);
3✔
50
    addKeyword(getName());
4✔
51
  }
1✔
52

53
  @Override
54
  public String getName() {
55

56
    return "shell";
2✔
57
  }
58

59
  @Override
60
  public boolean isIdeHomeRequired() {
61

62
    return false;
2✔
63
  }
64

65
  @Override
66
  public void run() {
67

68
    try {
69
      Parser parser = new DefaultParser();
×
70
      try (Terminal terminal = TerminalBuilder.builder().build()) {
×
71
        // initialize our own completer here and add exit as an autocompletion option
72
        Completer completer = new AggregateCompleter(
×
73
            new StringsCompleter("exit"), new IdeCompleter((AbstractIdeContext) this.context));
74

75
        LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).parser(parser)
×
76
            .variable(LineReader.LIST_MAX, AUTOCOMPLETER_MAX_RESULTS).build();
×
77

78
        // Create autosuggestion widgets
79
        AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
×
80
        // Enable autosuggestions
81
        autosuggestionWidgets.enable();
×
82

83
        // TODO: implement TailTipWidgets, see: https://github.com/devonfw/IDEasy/issues/169
84

85
        String rightPrompt = null;
×
86
        String line;
87

88
        AnsiConsole.systemInstall();
×
89
        while (true) {
90
          try {
91
            String prompt = context.getCwd() + "$ ide ";
×
92
            line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
×
93
            line = line.trim();
×
94
            if (line.equals("exit")) {
×
95
              return;
×
96
            }
97
            reader.getHistory().add(line);
×
98
            int rc = runCommand(line);
×
99
            if (rc == RC_EXIT) {
×
100
              return;
×
101
            }
102
          } catch (UserInterruptException e) {
×
103
            // Ignore CTRL+C
104
            return;
×
105
          } catch (EndOfFileException e) {
×
106
            // CTRL+D
107
            return;
×
108
          } finally {
109
            AnsiConsole.systemUninstall();
×
110
          }
×
111
        }
112

113
      } catch (IOException e) {
×
114
        throw new RuntimeException(e);
×
115
      }
116
    } catch (CliException e) {
×
117
      throw e;
×
118
    } catch (Exception e) {
×
119
      throw new RuntimeException("Unexpected error during interactive auto-completion", e);
×
120
    }
121
  }
122

123
  /**
124
   * Converts String of arguments to array and runs the command
125
   *
126
   * @param args String of arguments
127
   * @return status code
128
   */
129
  private int runCommand(String args) {
130

131
    if ("exit".equals(args) || "quit".equals(args)) {
×
132
      return RC_EXIT;
×
133
    }
134
    String[] arguments = args.split(" ", 0);
×
135
    CliArguments cliArgs = new CliArguments(arguments);
×
136
    cliArgs.next();
×
137

138
    if ("cd".equals(arguments[0])) {
×
139
      return changeDirectory(cliArgs);
×
140
    }
141

142
    return ((AbstractIdeContext) this.context).run(cliArgs);
×
143
  }
144

145
  private int changeDirectory(CliArguments cliArgs) {
146
    if (!cliArgs.hasNext()) {
×
147
      Path homeDir = this.context.getUserHome();
×
148
      context.setCwd(homeDir, context.getWorkspaceName(), context.getIdeHome());
×
149
      return 0;
×
150
    }
151

152
    String targetDir = String.valueOf(cliArgs.next());
×
153
    Path path = Paths.get(targetDir);
×
154

155
    // If the given path is relative, resolve it relative to the current directory
156
    if (!path.isAbsolute()) {
×
157
      path = context.getCwd().resolve(targetDir).normalize();
×
158
    }
159

160
    if (context.getFileAccess().isExpectedFolder(path)) {
×
161
      context.setCwd(path, context.getWorkspaceName(), context.getIdeHome());
×
162
      return 0;
×
163
    } else {
164
      return 1;
×
165
    }
166
  }
167

168
  /**
169
   * @param argument the current {@link CliArgument} (position) to match.
170
   * @param commandlet the potential {@link Commandlet} to match.
171
   * @return {@code true} if the given {@link Commandlet} matches to the given {@link CliArgument}(s) and those have been applied (set in the {@link Commandlet}
172
   *     and {@link Commandlet#validate() validated}), {@code false} otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
173
   */
174
  private boolean apply(CliArgument argument, Commandlet commandlet) {
175

176
    this.context.trace("Trying to match arguments to commandlet {}", commandlet.getName());
×
177
    CliArgument currentArgument = argument;
×
178
    Iterator<Property<?>> valueIterator = commandlet.getValues().iterator();
×
179
    Property<?> currentProperty = null;
×
180
    boolean endOpts = false;
×
181
    while (!currentArgument.isEnd()) {
×
182
      if (currentArgument.isEndOptions()) {
×
183
        endOpts = true;
×
184
      } else {
185
        String arg = currentArgument.get();
×
186
        this.context.trace("Trying to match argument '{}'", currentArgument);
×
187
        if ((currentProperty != null) && (currentProperty.isExpectValue())) {
×
188
          currentProperty.setValueAsString(arg, this.context);
×
189
          if (!currentProperty.isMultiValued()) {
×
190
            currentProperty = null;
×
191
          }
192
        } else {
193
          Property<?> property = null;
×
194
          if (!endOpts) {
×
195
            property = commandlet.getOption(currentArgument.getKey());
×
196
          }
197
          if (property == null) {
×
198
            if (!valueIterator.hasNext()) {
×
199
              this.context.trace("No option or next value found");
×
200
              return false;
×
201
            }
202
            currentProperty = valueIterator.next();
×
203
            this.context.trace("Next value candidate is {}", currentProperty);
×
204
            if (currentProperty instanceof KeywordProperty keyword) {
×
205
              if (keyword.matches(arg)) {
×
206
                keyword.setValue(Boolean.TRUE);
×
207
                this.context.trace("Keyword matched");
×
208
              } else {
209
                this.context.trace("Missing keyword");
×
210
                return false;
×
211
              }
212
            } else {
213
              boolean success = currentProperty.assignValueAsString(arg, this.context, commandlet);
×
214
              if (!success && currentProperty.isRequired()) {
×
215
                return false;
×
216
              }
217
            }
218
            if ((currentProperty != null) && !currentProperty.isMultiValued()) {
×
219
              currentProperty = null;
×
220
            }
221
          } else {
222
            this.context.trace("Found option by name");
×
223
            String value = currentArgument.getValue();
×
224
            if (value != null) {
×
225
              property.setValueAsString(value, this.context);
×
226
            } else if (property instanceof BooleanProperty) {
×
227
              ((BooleanProperty) property).setValue(Boolean.TRUE);
×
228
            } else {
229
              currentProperty = property;
×
230
              if (property.isEndOptions()) {
×
231
                endOpts = true;
×
232
              }
233
              throw new UnsupportedOperationException("not implemented");
×
234
            }
235
          }
236
        }
237
      }
238
      currentArgument = currentArgument.getNext(!endOpts);
×
239
    }
240
    return commandlet.validate().isValid();
×
241
  }
242
}
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