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

devonfw / IDEasy / 12772197608

14 Jan 2025 04:36PM UTC coverage: 66.655% (-1.2%) from 67.826%
12772197608

Pull #933

github

web-flow
Merge 4195adc3d into 6f167c101
Pull Request #933: prevent stacktrace of CliException even with debug logging

1621 of 2629 branches covered (61.66%)

Branch coverage included in aggregate %.

3990 of 5789 relevant lines covered (68.92%)

5.28 hits per line

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

0.0
cli/src/main/java/com/devonfw/tools/ide/cli/Ideasy.java
1
package com.devonfw.tools.ide.cli;
2

3
import java.net.URLEncoder;
4
import java.nio.charset.StandardCharsets;
5

6
import com.devonfw.tools.ide.commandlet.ContextCommandlet;
7
import com.devonfw.tools.ide.context.AbstractIdeContext;
8
import com.devonfw.tools.ide.context.IdeContext;
9
import com.devonfw.tools.ide.context.IdeContextConsole;
10
import com.devonfw.tools.ide.context.IdeStartContextImpl;
11
import com.devonfw.tools.ide.log.IdeLogLevel;
12
import com.devonfw.tools.ide.property.FlagProperty;
13
import com.devonfw.tools.ide.property.Property;
14

15
/**
16
 * The main program of the CLI (command-line-interface).
17
 */
18
public final class Ideasy {
×
19

20
  private AbstractIdeContext context;
21

22
  /**
23
   * The actual main method of the CLI program.
24
   *
25
   * @param args the command-line arguments.
26
   */
27
  public static void main(String... args) {
28

29
    int exitStatus = new Ideasy().run(args);
×
30
    System.exit(exitStatus);
×
31
  }
×
32

33
  private IdeContext context() {
34

35
    if (this.context == null) {
×
36
      // fallback in case of exception before initialization
37
      this.context = new IdeContextConsole(IdeLogLevel.INFO, null, false);
×
38
    }
39
    return this.context;
×
40
  }
41

42
  /**
43
   * Non-static variant of {@link #main(String...) main method} without invoking {@link System#exit(int)} so it can be tested.
44
   *
45
   * @param args the command-line arguments.
46
   * @return the exit code.
47
   */
48
  public int run(String... args) {
49

50
    int exitStatus;
51
    try {
52
      exitStatus = runOrThrow(args);
×
53
    } catch (CliException error) {
×
54
      exitStatus = error.getExitCode();
×
55
      context().error(error.getMessage());
×
56
    } catch (Throwable error) {
×
57
      exitStatus = 255;
×
58
      String title = error.getMessage();
×
59
      if (title == null) {
×
60
        title = error.getClass().getName();
×
61
      } else {
62
        title = error.getClass().getSimpleName() + ": " + title;
×
63
      }
64
      String message = "An unexpected error occurred!\n" //
×
65
          + "We are sorry for the inconvenience.\n" //
66
          + "Please check the error below, resolve it and try again.\n" //
67
          + "If the error is not on your end (network connectivity, lack of permissions, etc.) please file a bug:\n" //
68
          + "https://github.com/devonfw/IDEasy/issues/new?assignees=&labels=bug&projects=&template=bug.md&title="
69
          + URLEncoder.encode(title, StandardCharsets.UTF_8);
×
70
      context().error(error, message);
×
71
    }
×
72
    return exitStatus;
×
73
  }
74

75
  /**
76
   * Like {@link #run(String...)} but does not catch {@link Throwable}s so you can handle them yourself.
77
   *
78
   * @param args the command-line arguments.
79
   * @return the exit code.
80
   */
81
  public int runOrThrow(String... args) {
82

83
    CliArguments arguments = new CliArguments(args);
×
84
    this.context = initContext(arguments);
×
85
    return this.context.run(arguments);
×
86
  }
87

88
  private AbstractIdeContext initContext(CliArguments arguments) {
89

90
    ContextCommandlet contextCommandlet = new ContextCommandlet();
×
91
    while (arguments.hasNext()) {
×
92
      CliArgument current = arguments.next();
×
93
      String key = current.getKey();
×
94
      Property<?> property = contextCommandlet.getOption(key);
×
95
      if (property == null) {
×
96
        break;
×
97
      }
98
      String value = current.getValue();
×
99
      if (value == null) {
×
100
        if (property instanceof FlagProperty) {
×
101
          ((FlagProperty) property).setValue(Boolean.TRUE);
×
102
        } else {
103
          System.err.println("Missing value for option " + key);
×
104
        }
105
      } else {
106
        property.setValueAsString(value, this.context);
×
107
      }
108
    }
×
109
    contextCommandlet.run();
×
110
    IdeStartContextImpl startContext = contextCommandlet.getStartContext();
×
111
    return new IdeContextConsole(startContext);
×
112
  }
113

114
}
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