All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudbees.sdk.commands.db.DatabaseList Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2013, CloudBees Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cloudbees.sdk.commands.db;


import com.cloudbees.api.BeesClient;
import com.cloudbees.api.DatabaseListResponse;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;
import com.cloudbees.sdk.commands.Command;
import com.cloudbees.sdk.utils.Helper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Fabian Donze
 */
@BeesCommand(group="Database", description = "List databases")
@CLICommand("db:list")
public class DatabaseList extends Command {
    private String account;
    private Boolean more;

    public DatabaseList() {
    }

    @Override
    protected boolean preParseCommandLine() {
        addOption( "a", "account", true, "Account Name" );
        addOption( null, "more", false, "Display more information" );
        return true;
    }

    @Override
    protected boolean postParseCommandLine() {
        return true;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public boolean displayMore() {
        return more != null ? more : false;
    }

    public void setMore(Boolean more) {
        this.more = more;
    }

    @Override
    protected boolean execute() throws Exception {
        BeesClient client = getBeesClient(BeesClient.class);
        DatabaseListResponse res = client.databaseList(getAccount());

        if (isTextOutput()) {
            if (displayMore()) {
                System.out.println("Database                       Tier            Master");
                System.out.println();
                List list = new ArrayList();
                for (com.cloudbees.api.DatabaseInfo applicationInfo: res.getDatabases()) {
                    String msg = s(applicationInfo.getOwner() + "/" + applicationInfo.getName(), 30);
                    Map settings = applicationInfo.getSettings();
                    String tier = "";
                    if (settings != null && settings.get("tier") != null) tier = settings.get("tier");
                    msg += " " + s(tier, 15) + " " + applicationInfo.getMaster();
                    list.add(msg);
                }
                Collections.sort(list);
                for (String db: list) {
                    System.out.println(db);
                }
                System.out.println();
                System.out.println("Total databases: " + list.size());
            } else {
                System.out.println("Databases:");
                for (com.cloudbees.api.DatabaseInfo applicationInfo: res.getDatabases()) {
                    System.out.println(applicationInfo.getOwner() + "/" + applicationInfo.getName());
                }
            }
        } else
            printOutput(res, DatabaseListResponse.class, DatabaseInfo.class);

        return true;
    }

    private String s(String str, int length) {
        return Helper.getPaddedString(str, length);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy