UpdateCommand.java

package org.docascode.api;

import org.docascode.api.core.errors.DocAsCodeException;
import org.docascode.api.core.office.Properties;
import org.docascode.api.event.Event;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class UpdateCommand extends DocAsCodeCommand<Map<String,String>> {
    public enum Action {
        ADD,
        LIST
    }

    private Action action = Action.ADD;

    private File file;

    private Map<String,String> properties = new HashMap<>();

    public UpdateCommand setAction(Action action){
        this.action = action;
        return this;
    }

    public UpdateCommand setFile(File file){
        this.file = file;
        return this;
    }

    public UpdateCommand setProperties(Map<String,String> map){
        this.properties = map;
        return this;
    }

    @Override
    public Map<String,String> call() throws DocAsCodeException {
        Map<String,String> result = new HashMap<>();
        if (this.action == Action.LIST) {
            result = Properties.list(file);
        } else {
            log(String.format("Updating file '%s'.",file), Event.Level.INFO);
            for (Map.Entry<String,String> m : properties.entrySet()){
                log(String.format("\t%s:'%s'",m.getKey(),m.getValue()), Event.Level.INFO);
            }
            Properties.set(file,properties);
        }
        return result;
    }
}