Skip to content

Commit

Permalink
Merge pull request #187 from udda1996/fix-use-command
Browse files Browse the repository at this point in the history
Add distribution availability check
  • Loading branch information
hevayo authored Dec 20, 2021
2 parents 52c6696 + b4efb40 commit d93e195
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/ballerinalang/command/cmd/UseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.ballerinalang.command.cmd;

import org.ballerinalang.command.BallerinaCliCommands;
import org.ballerinalang.command.util.Channel;
import org.ballerinalang.command.util.Distribution;
import org.ballerinalang.command.util.ErrorUtil;
import org.ballerinalang.command.util.ToolUtil;
import picocli.CommandLine;
Expand Down Expand Up @@ -68,8 +70,23 @@ public void execute() {
return;
}
printStream.println("Distribution '" + distribution + "' not found");
printStream.println("Run 'bal dist pull " + distribution + "' to fetch and set the distribution as the " +
"active distribution");

List<Channel> channels = ToolUtil.getDistributions(printStream);
boolean validDistribution = false;
for (Channel channel : channels) {
for (Distribution dist : channel.getDistributions()) {
if (distribution.equals(dist.getVersion()) ) {
validDistribution = true;
printStream.println("Run 'bal dist pull " + distribution + "' to fetch and set the distribution " +
"as the active distribution");
break;
}
}
}
if (!validDistribution){
printStream.println( "'" + distribution + "' is not a valid distribution. Use 'bal dist list -a' for the " +
"available distributions list");
}
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/ballerinalang/command/UseCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void useCommandTest() {
new CommandLine(useCmd).parse("slp3");
useCmd.execute();
Assert.assertTrue(outContent.toString().contains("not found"));

UseCommand useCommandInvalidDist = new UseCommand(testStream);
new CommandLine(useCommandInvalidDist).parse("slbeta7");
useCommandInvalidDist.execute();
Assert.assertTrue(outContent.toString().contains("not a valid distribution"));
}

@Test
Expand Down

0 comments on commit d93e195

Please sign in to comment.