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

devonfw / IDEasy / 13788808942

11 Mar 2025 01:08PM UTC coverage: 66.998% (-1.6%) from 68.619%
13788808942

Pull #1017

github

web-flow
Merge 2597874a1 into 2cf68279f
Pull Request #1017: #404: enhance logging with custom slf4j bridge

3034 of 4951 branches covered (61.28%)

Branch coverage included in aggregate %.

7831 of 11266 relevant lines covered (69.51%)

3.03 hits per line

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

56.52
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 org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8

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

18
/**
19
 * The main program of the CLI (command-line-interface).
20
 */
21
public final class Ideasy {
22

23
  private AbstractIdeContext context;
24

25
  private static final Logger logger = LoggerFactory.getLogger(Ideasy.class);
4✔
26

27
  /**
28
   * The default constructor.
29
   */
30
  public Ideasy() {
31
    super();
×
32
  }
×
33

34
  /**
35
   * The constructor.
36
   *
37
   * @param context the predefined {@link IdeContext} for testing.
38
   */
39
  Ideasy(AbstractIdeContext context) {
40

41
    super();
2✔
42
    this.context = context;
3✔
43
  }
1✔
44

45
  private IdeContext context() {
46

47
    if (this.context == null) {
3!
48
      // fallback in case of exception before initialization
49
      return new IdeContextConsole(IdeLogLevel.INFO, null, false);
×
50
    }
51
    return this.context;
3✔
52
  }
53

54
  /**
55
   * Non-static variant of {@link #main(String...) main method} without invoking {@link System#exit(int)} so it can be tested.
56
   *
57
   * @param args the command-line arguments.
58
   * @return the exit code.
59
   */
60
  public int run(String... args) {
61

62
    int exitStatus;
63
    try {
64
      exitStatus = runOrThrow(args);
×
65
    } catch (CliException error) {
1✔
66
      exitStatus = error.getExitCode();
3✔
67
      String errorMessage = error.getMessage();
3✔
68
      if ((errorMessage != null) && !errorMessage.isBlank()) {
5!
69
        context().error(errorMessage);
4✔
70
      }
71
    } catch (Throwable error) {
×
72
      exitStatus = 255;
×
73
      String title = error.getMessage();
×
74
      if (title == null) {
×
75
        title = error.getClass().getName();
×
76
      } else {
77
        title = error.getClass().getSimpleName() + ": " + title;
×
78
      }
79
      String message = "An unexpected error occurred!\n" //
×
80
          + "We are sorry for the inconvenience.\n" //
81
          + "Please check the error below, resolve it and try again.\n" //
82
          + "If the error is not on your end (network connectivity, lack of permissions, etc.) please file a bug:\n" //
83
          + "https://github.com/devonfw/IDEasy/issues/new?assignees=&labels=bug&projects=&template=bug.md&title="
84
          + URLEncoder.encode(title, StandardCharsets.UTF_8);
×
85
      context().error(error, message);
×
86
    }
1✔
87
    return exitStatus;
2✔
88
  }
89

90
  /**
91
   * Like {@link #run(String...)} but does not catch {@link Throwable}s so you can handle them yourself.
92
   *
93
   * @param args the command-line arguments.
94
   * @return the exit code.
95
   */
96
  public int runOrThrow(String... args) {
97

98
    CliArguments arguments = new CliArguments(args);
5✔
99
    initContext(arguments);
3✔
100
    return this.context.run(arguments);
×
101
  }
102

103
  private void initContext(CliArguments arguments) {
104

105
    ContextCommandlet contextCommandlet = new ContextCommandlet();
4✔
106
    while (arguments.hasNext()) {
3!
107
      CliArgument current = arguments.next();
3✔
108
      String key = current.getKey();
3✔
109
      Property<?> property = contextCommandlet.getOption(key);
4✔
110
      if (property == null) {
2✔
111
        break;
1✔
112
      }
113
      String value = current.getValue();
3✔
114
      if (value == null) {
2!
115
        if (property instanceof FlagProperty) {
3!
116
          ((FlagProperty) property).setValue(Boolean.TRUE);
5✔
117
        } else {
118
          System.err.println("Missing value for option " + key);
×
119
        }
120
      } else {
121
        property.setValueAsString(value, this.context);
×
122
      }
123
    }
1✔
124
    contextCommandlet.run();
2✔
125
    if (this.context == null) {
3!
126
      IdeStartContextImpl startContext = contextCommandlet.getStartContext();
×
127
      this.context = new IdeContextConsole(startContext);
×
128
    }
129
  }
1✔
130

131
  /**
132
   * The actual main method of the CLI program.
133
   *
134
   * @param args the command-line arguments.
135
   */
136
  public static void main(String... args) {
137

138
    int exitStatus = new Ideasy().run(args);
×
139
    System.exit(exitStatus);
×
140
  }
×
141

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