001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.q2.cli.deploy;
020
021import java.io.File;
022
023import org.jpos.q2.CLICommand;
024import org.jpos.q2.CLIContext;
025import org.jpos.q2.Q2;
026/**
027* CLI implementation - Deploy subsystem
028* 
029* @author Felipph Calado - luizfelipph@gmail.com
030*/
031public class LIST implements CLICommand {
032    /** Default constructor; no instance state to initialise. */
033    public LIST() {}
034
035    @Override
036    public void exec(CLIContext ctx, String[] args) throws Exception {
037        Q2 q2 = ctx.getCLI().getQ2();
038        File deployDir = q2.getDeployDir();
039        
040        ctx.println(printDirectoryTree(deployDir));
041        return;
042    }
043
044    /**
045     * Renders the contents of {@code folder} as a tree-formatted string.
046     *
047     * @param folder root directory to render
048     * @return the tree rendering
049     * @throws IllegalArgumentException if {@code folder} is not a directory
050     */
051    public static String printDirectoryTree(File folder) {
052        if (!folder.isDirectory()) {
053            throw new IllegalArgumentException("folder is not a Directory");
054        }
055        int indent = 0;
056        StringBuilder sb = new StringBuilder();
057        printDirectoryTree(folder, indent, sb);
058        return sb.toString();
059    }
060
061    private static void printDirectoryTree(File folder, int indent, StringBuilder sb) {
062        if (!folder.isDirectory()) {
063            throw new IllegalArgumentException("folder is not a Directory");
064        }
065        sb.append(getIndentString(indent));
066        sb.append("+--");
067        sb.append(folder.getName());
068        sb.append("/");
069        sb.append("\n");
070        for (File file : folder.listFiles()) {
071            if (file.isDirectory()) {
072                printDirectoryTree(file, indent + 1, sb);
073            } else {
074                printFile(file, indent + 1, sb);
075            }
076        }
077
078    }
079
080    private static void printFile(File file, int indent, StringBuilder sb) {
081        sb.append(getIndentString(indent));
082        sb.append("+--");
083        sb.append(file.getName());
084        sb.append("\n");
085    }
086
087    private static String getIndentString(int indent) {
088        StringBuilder sb = new StringBuilder();
089        for (int i = 0; i < indent; i++) {
090            sb.append("|  ");
091        }
092        return sb.toString();
093    }
094
095}