diff --git a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java index 0dcbaadb..23af238e 100644 --- a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java +++ b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java @@ -23,11 +23,11 @@ import org.ballerinalang.command.util.Channel; import org.ballerinalang.command.util.Distribution; import org.ballerinalang.command.util.ErrorUtil; +import org.ballerinalang.command.util.OSUtils; import org.ballerinalang.command.util.ToolUtil; import java.io.BufferedReader; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintStream; @@ -42,8 +42,6 @@ */ @CommandLine.Command(name = "list", description = "List Ballerina Distributions") public class ListCommand extends Command implements BCommand { - private static final String LOCAL_DISTRIBUTIONS_FILE = "local-dists.json"; - @CommandLine.Parameters(description = "Command name") private List listCommands; @@ -166,10 +164,11 @@ private static String markVersion(String used, String current) { */ private static void writeLocalDistsIntoJson(List installedVersions) throws IOException { FileWriter writer; + String distListPath = OSUtils.getBallerinaDistListFilePath(); try { - writer = new FileWriter(LOCAL_DISTRIBUTIONS_FILE); + writer = new FileWriter(distListPath); } catch (IOException e) { - throw ErrorUtil.createCommandException("cannot write into " + LOCAL_DISTRIBUTIONS_FILE + " file."); + throw ErrorUtil.createCommandException("cannot write into " + distListPath + " file: " + e.getMessage()); } for(String str: installedVersions) { writer.write(str + System.lineSeparator()); @@ -186,17 +185,15 @@ private static void writeLocalDistsIntoJson(List installedVersions) thro private static void readLocalDistsFromJson(PrintStream outStream, String currentBallerinaVersion) { BufferedReader reader; try { - reader = new BufferedReader(new FileReader(LOCAL_DISTRIBUTIONS_FILE)); + reader = new BufferedReader(new FileReader(OSUtils.getBallerinaDistListFilePath())); String line; while ((line = reader.readLine()) != null) { outStream.println(markVersion(currentBallerinaVersion, line) + " " + ToolUtil.getTypeName(line)); } outStream.println("\n"); reader.close(); - } catch (FileNotFoundException fileNotFoundException) { - throw ErrorUtil.createCommandException("cannot find " + LOCAL_DISTRIBUTIONS_FILE + " file."); - } catch (IOException ioException) { - throw ErrorUtil.createCommandException("cannot read " + LOCAL_DISTRIBUTIONS_FILE + " file."); + } catch (IOException e) { + throw ErrorUtil.createCommandException("failed to read the file: " + e.getMessage()); } } } diff --git a/src/main/java/org/ballerinalang/command/util/OSUtils.java b/src/main/java/org/ballerinalang/command/util/OSUtils.java index 7c86e6cf..88e6c48a 100644 --- a/src/main/java/org/ballerinalang/command/util/OSUtils.java +++ b/src/main/java/org/ballerinalang/command/util/OSUtils.java @@ -19,7 +19,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.net.URISyntaxException; @@ -29,7 +28,6 @@ import java.time.LocalDate; import java.util.Comparator; import java.util.Locale; -import java.util.Properties; import java.util.stream.Collectors; /** @@ -40,6 +38,7 @@ public class OSUtils { private static final String OS = System.getProperty("os.name").toLowerCase(Locale.getDefault()); private static final String BALLERINA_HOME_DIR = ".ballerina"; private static final String BALLERINA_CONFIG = "ballerina-version"; + public static final String BALLERINA_LIST_JSON = "local-dists.json"; private static final String UPDATE_NOTICE = "command-notice"; private static final String BIR_CACHE = "bir_cache"; private static final String JAR_CACHE = "jar_cache"; @@ -99,6 +98,19 @@ public static String getBallerinaVersionFilePath() throws IOException { + BALLERINA_HOME_DIR + File.separator + BALLERINA_CONFIG; } + public static String getBallerinaDistListFilePath() throws IOException { + String userHome = getUserHome(); + File file = new File(userHome + File.separator + + BALLERINA_HOME_DIR + File.separator + BALLERINA_LIST_JSON); + + if (!file.exists()) { + file.getParentFile().mkdirs(); + file.createNewFile(); + } + return getUserHome() + File.separator + + BALLERINA_HOME_DIR + File.separator + BALLERINA_LIST_JSON; + } + /** * Provide configuration file path of the installation. * @return path to the file