JB, Thanks.

The working code is bellow.

Regards,

Davi

import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.Session;

import io.infolayer.siteview.keychain.vault.IKeychainServiceManagement;

@Service
@Command(scope = "keychain", name = "add", description = "Add a KeyChain
entry.")
public class KeyChainAddCommand implements Action {

        @Reference
        private IKeychainServiceManagement service;
        
        @Reference
        private Session session;
        
        @Option(name = "-t", aliases = "--type", description = "Entry type: [1 =
Username/Password pair]", required = false, multiValued = false)
    int type = 1;

        @Override
        public Object execute() throws Exception {
                
                if (!service.tryOpen()) {
                System.out.println("Keychain is closed. Open it first.");
                return null;
        }
                
                System.out.print("Adding Entry type " + type);
                
                switch (type) {
                case 1:
                        
                        System.out.println(": Username/Password.");
                        
                        if (session != null) {
                                char[] u = session.readLine("Username:", 
null).toCharArray(); //
readLine("Username: ").toCharArray();
                                char[] p = session.readLine("Password: ", 
'*').toCharArray();
                                String description = 
session.readLine("Description: ", null);
                                String tags = session.readLine("Tags (comma 
separated): ", null);
                                String label = service.storeEntry(u, p, type, 
description, tags);
                                System.out.println("Entry label is: " + label);
                        }else {
                                System.out.println("Unable to get console 
Session.");
                        }
                        
                        break;

                default:
                        System.out.println("Type must be: [1 = 
Username/Password pair]");
                        break;
                }
                
                return null;
        }

}




--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html

Reply via email to