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

com.github.triceo.robozonky.installer.panels.CommandLinePart Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta-5
Show newest version
/*
 * Copyright 2017 Lukáš Petrovický
 *
 * 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.github.triceo.robozonky.installer.panels;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.github.triceo.robozonky.internal.api.Defaults;

public class CommandLinePart {

    private final Map> options = new LinkedHashMap<>();
    private final Map properties = new TreeMap<>();
    private final Map environmentVariables = new TreeMap<>();


    public CommandLinePart setOption(final String key, final String... value) {
        this.options.put(key, Collections.unmodifiableCollection(Arrays.asList(value)));
        return this;
    }

    public CommandLinePart setProperty(final String key, final String value) {
        this.properties.put(key, value);
        return this;
    }

    public CommandLinePart setEnvironmentVariable(final String key, final String value) {
        this.environmentVariables.put(key, value);
        return this;
    }

    public Map> getOptions() {
        return Collections.unmodifiableMap(options);
    }

    public Map getProperties() {
        return Collections.unmodifiableMap(properties);
    }

    public Map getEnvironmentVariables() {
        return Collections.unmodifiableMap(environmentVariables);
    }

    private Collection getOptionItems() {
        return options.entrySet().stream()
                .flatMap((e) -> {
                    final Stream key = Stream.of(e.getKey());
                    final Stream values = e.getValue().stream().map(v -> "\"" + v + "\"");
                    return Stream.concat(key, values);
                }).collect(Collectors.toList());
    }

    public String convertOptions() {
        return this.getOptionItems().stream().collect(Collectors.joining(" "));
    }

    public void storeOptions(final File cliFile) throws IOException {
        Files.write(cliFile.toPath(), this.getOptionItems(), Defaults.CHARSET);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy