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

net.oneandone.stool.Config Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/**
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * 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 net.oneandone.stool;

import net.oneandone.stool.configuration.Property;
import net.oneandone.stool.configuration.StageConfiguration;
import net.oneandone.stool.locking.Mode;
import net.oneandone.stool.stage.Stage;
import net.oneandone.stool.util.Session;
import net.oneandone.sushi.cli.ArgumentException;
import net.oneandone.sushi.cli.Remaining;
import net.oneandone.sushi.util.Strings;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Config extends StageCommand {
    private final Map all;
    private final Map selected;

    private boolean get;
    private boolean set;

    public Config(Session session) {
        super(session, Mode.NONE, Mode.EXCLUSIVE, Mode.NONE);
        all = StageConfiguration.properties(session.extensionsFactory);
        selected = new HashMap<>();
    }

    @Remaining
    public void property(String str) {
        int idx;
        String key;
        String value;
        Property property;

        idx = str.indexOf('=');
        if (idx == -1) {
            key = str;
            value = null;
            get = true;
        } else {
            key = str.substring(0, idx);
            value = str.substring(idx + 1);
            set = true;
        }
        try {
            property = all.get(key);
            if (property == null) {
                throw new ArgumentException("unknown property: " + key);
            }
        } catch (SecurityException e) {
            throw new ArgumentException(e.getMessage());
        }
        if (selected.containsKey(property)) {
            throw new ArgumentException("duplicate property: " + key);
        }
        if (get && set) {
            throw new ArgumentException("cannot mix get and set arguments");
        }
        if (selected.put(property, value) != null) {
            throw new ArgumentException("duplicate property: " + key);
        }
    }

    @Override
    public void doInvoke(Stage stage) throws Exception {
        StageConfiguration configuration;
        boolean error;
        Property prop;
        String value;
        Collection props;
        int width;

        configuration = stage.config();
        if (set) {
            stage.checkOwnership();
            error = false;
            for (Map.Entry entry : selected.entrySet()) {
                prop = entry.getKey();
                value = entry.getValue();
                value = value.replace("{}", prop.get(configuration));
                try {
                    prop.set(configuration, value);
                    console.info.println(prop.name + "=" + value);
                } catch (RuntimeException e) {
                    console.info.println("invalid value for property " + prop.name + " : " + e.getMessage());
                    e.printStackTrace(console.verbose);
                    error = true;
                }
            }
            if (!error) {
                session.saveStageProperties(configuration, stage.backstage);
                if (session.isSelected(stage)) {
                    session.environment.setAll(session.environment(stage));
                }
            }
        } else {
            props = get ? selected.keySet() : all.values();
            width = 0 ;
            if (props.size() > 1) {
                for (Property property : props) {
                    width = Math.max(width, property.name.length());
                }
                width += 3;
            }
            for (Property property : props) {
                console.info.println(Strings.padLeft(property.name, width) + " : " + property.get(configuration));
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy