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

devonfw / IDEasy / 11782990941

11 Nov 2024 05:06PM UTC coverage: 66.902% (-0.05%) from 66.948%
11782990941

push

github

web-flow
#415: log command before sudo pw (#732)

2421 of 3960 branches covered (61.14%)

Branch coverage included in aggregate %.

6311 of 9092 relevant lines covered (69.41%)

3.06 hits per line

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

2.02
cli/src/main/java/com/devonfw/tools/ide/tool/GlobalToolCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.Arrays;
6
import java.util.List;
7
import java.util.Set;
8

9
import com.devonfw.tools.ide.common.Tag;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.io.FileAccess;
12
import com.devonfw.tools.ide.log.IdeLogLevel;
13
import com.devonfw.tools.ide.process.EnvironmentContext;
14
import com.devonfw.tools.ide.process.ProcessContext;
15
import com.devonfw.tools.ide.process.ProcessErrorHandling;
16
import com.devonfw.tools.ide.repo.ToolRepository;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

19
/**
20
 * {@link ToolCommandlet} that is installed globally.
21
 */
22
public abstract class GlobalToolCommandlet extends ToolCommandlet {
23

24
  /**
25
   * The constructor.
26
   *
27
   * @param context the {@link IdeContext}.
28
   * @param tool the {@link #getName() tool name}.
29
   * @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
30
   */
31
  public GlobalToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
32

33
    super(context, tool, tags);
5✔
34
  }
1✔
35

36
  /**
37
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
38
   *
39
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
40
   * @param commandStrings commandStrings The package manager command strings to execute.
41
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@code false} otherwise.
42
   */
43
  protected boolean runWithPackageManager(boolean silent, String... commandStrings) {
44

45
    List<PackageManagerCommand> pmCommands = Arrays.stream(commandStrings).map(PackageManagerCommand::of).toList();
×
46
    return runWithPackageManager(silent, pmCommands);
×
47
  }
48

49
  /**
50
   * Performs the installation or uninstallation of the {@link #getName() tool} via a package manager.
51
   *
52
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
53
   * @param pmCommands A list of {@link PackageManagerCommand} to be used for installation or uninstallation.
54
   * @return {@code true} if installation or uninstallation succeeds with any of the package manager commands, {@code false} otherwise.
55
   */
56
  protected boolean runWithPackageManager(boolean silent, List<PackageManagerCommand> pmCommands) {
57

58
    for (PackageManagerCommand pmCommand : pmCommands) {
×
59
      PackageManager packageManager = pmCommand.packageManager();
×
60
      Path packageManagerPath = this.context.getPath().findBinary(Path.of(packageManager.getBinaryName()));
×
61
      if (packageManagerPath == null || !Files.exists(packageManagerPath)) {
×
62
        this.context.debug("{} is not installed", packageManager.toString());
×
63
        continue; // Skip to the next package manager command
×
64
      }
65

66
      if (executePackageManagerCommand(pmCommand, silent)) {
×
67
        return true; // Success
×
68
      }
69
    }
×
70
    return false; // None of the package manager commands were successful
×
71
  }
72

73
  private void logPackageManagerCommands(PackageManagerCommand pmCommand) {
74

75
    this.context.interaction("We need to run the following privileged command(s):");
×
76
    for (String command : pmCommand.commands()) {
×
77
      this.context.interaction(command);
×
78
    }
×
79
    this.context.interaction("This will require root permissions!");
×
80
  }
×
81

82
  /**
83
   * Executes the provided package manager command.
84
   *
85
   * @param pmCommand The {@link PackageManagerCommand} containing the commands to execute.
86
   * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
87
   * @return {@code true} if the package manager commands execute successfully, {@code false} otherwise.
88
   */
89
  private boolean executePackageManagerCommand(PackageManagerCommand pmCommand, boolean silent) {
90

91
    String bashPath = this.context.findBashRequired();
×
92
    logPackageManagerCommands(pmCommand);
×
93
    for (String command : pmCommand.commands()) {
×
94
      ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING).executable(bashPath)
×
95
          .addArgs("-c", command);
×
96
      int exitCode = pc.run();
×
97
      if (exitCode != 0) {
×
98
        this.context.warning("{} command did not execute successfully", command);
×
99
        return false;
×
100
      }
101
    }
×
102

103
    if (!silent) {
×
104
      this.context.success("Successfully installed {}", this.tool);
×
105
    }
106
    return true;
×
107
  }
108

109
  @Override
110
  protected boolean isExtract() {
111

112
    // for global tools we usually download installers and do not want to extract them (e.g. installer.msi file shall
113
    // not be extracted)
114
    return false;
×
115
  }
116

117
  @Override
118
  public boolean install(boolean silent, EnvironmentContext environmentContext) {
119

120
    Path binaryPath = this.context.getPath().findBinary(Path.of(getBinaryName()));
×
121
    // if force mode is enabled, go through with the installation even if the tool is already installed
122
    if (binaryPath != null && Files.exists(binaryPath) && !this.context.isForceMode()) {
×
123
      IdeLogLevel level = silent ? IdeLogLevel.DEBUG : IdeLogLevel.INFO;
×
124
      this.context.level(level).log("{} is already installed at {}", this.tool, binaryPath);
×
125
      return false;
×
126
    }
127
    String edition = getConfiguredEdition();
×
128
    ToolRepository toolRepository = this.context.getDefaultToolRepository();
×
129
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
130
    VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, configuredVersion);
×
131
    // download and install the global tool
132
    FileAccess fileAccess = this.context.getFileAccess();
×
133
    Path target = toolRepository.download(this.tool, edition, resolvedVersion);
×
134
    Path executable = target;
×
135
    Path tmpDir = null;
×
136
    boolean extract = isExtract();
×
137
    if (extract) {
×
138
      tmpDir = fileAccess.createTempDir(getName());
×
139
      Path downloadBinaryPath = tmpDir.resolve(target.getFileName());
×
140
      fileAccess.extract(target, downloadBinaryPath);
×
141
      executable = fileAccess.findFirst(downloadBinaryPath, Files::isExecutable, false);
×
142
    }
143
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING).executable(executable);
×
144
    int exitCode = pc.run();
×
145
    if (tmpDir != null) {
×
146
      fileAccess.delete(tmpDir);
×
147
    }
148
    if (exitCode == 0) {
×
149
      this.context.success("Successfully installed {} in version {}", this.tool, resolvedVersion);
×
150
    } else {
151
      this.context.warning("{} in version {} was not successfully installed", this.tool, resolvedVersion);
×
152
      return false;
×
153
    }
154
    postInstall();
×
155
    return true;
×
156
  }
157

158
  @Override
159
  public VersionIdentifier getInstalledVersion() {
160
    //TODO: handle "get-version <globaltool>"
161
    this.context.error("Couldn't get installed version of " + this.getName());
×
162
    return null;
×
163
  }
164

165
  @Override
166
  public String getInstalledEdition() {
167
    //TODO: handle "get-edition <globaltool>"
168
    this.context.error("Couldn't get installed edition of " + this.getName());
×
169
    return null;
×
170
  }
171

172
  @Override
173
  public void uninstall() {
174
    //TODO: handle "uninstall <globaltool>"
175
    this.context.error("Couldn't uninstall " + this.getName());
×
176
  }
×
177
}
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

© 2025 Coveralls, Inc