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
033    @Override
034    public void exec(CLIContext ctx, String[] args) throws Exception {
035        Q2 q2 = ctx.getCLI().getQ2();
036        File deployDir = q2.getDeployDir();
037        
038        ctx.println(printDirectoryTree(deployDir));
039        return;
040    }
041
042    /**
043     * Utils
044     */
045    public static String printDirectoryTree(File folder) {
046        if (!folder.isDirectory()) {
047            throw new IllegalArgumentException("folder is not a Directory");
048        }
049        int indent = 0;
050        StringBuilder sb = new StringBuilder();
051        printDirectoryTree(folder, indent, sb);
052        return sb.toString();
053    }
054
055    private static void printDirectoryTree(File folder, int indent, StringBuilder sb) {
056        if (!folder.isDirectory()) {
057            throw new IllegalArgumentException("folder is not a Directory");
058        }
059        sb.append(getIndentString(indent));
060        sb.append("+--");
061        sb.append(folder.getName());
062        sb.append("/");
063        sb.append("\n");
064        for (File file : folder.listFiles()) {
065            if (file.isDirectory()) {
066                printDirectoryTree(file, indent + 1, sb);
067            } else {
068                printFile(file, indent + 1, sb);
069            }
070        }
071
072    }
073
074    private static void printFile(File file, int indent, StringBuilder sb) {
075        sb.append(getIndentString(indent));
076        sb.append("+--");
077        sb.append(file.getName());
078        sb.append("\n");
079    }
080
081    private static String getIndentString(int indent) {
082        StringBuilder sb = new StringBuilder();
083        for (int i = 0; i < indent; i++) {
084            sb.append("|  ");
085        }
086        return sb.toString();
087    }
088
089}