com.github.triceo.robozonky.installer.panels.CommandLinePart Maven / Gradle / Ivy
/*
* 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.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 LinkedHashMap<>();
private final Map environmentVariables = new LinkedHashMap<>();
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);
}
}