com.spotify.helios.cli.Utils Maven / Gradle / Ivy
/*
* Copyright (c) 2014 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.spotify.helios.cli;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.ListenableFuture;
import com.spotify.helios.client.HeliosClient;
import com.spotify.helios.common.descriptors.HostSelector;
import net.sourceforge.argparse4j.inf.Argument;
import net.sourceforge.argparse4j.inf.Namespace;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static java.lang.String.format;
public class Utils {
public static Map allAsMap(final Map> map)
throws ExecutionException, InterruptedException {
final Map result = Maps.newHashMap();
for (Map.Entry> e : map.entrySet()) {
result.put(e.getKey(), e.getValue().get());
}
return result;
}
public static HeliosClient getClient(final Target target, final PrintStream err,
final String username) {
List endpoints = Collections.emptyList();
try {
endpoints = target.getEndpointSupplier().get();
} catch (Exception ignore) {
// TODO (dano): Nasty. Refactor target to propagate resolution failure in a checked manner.
}
if (endpoints.size() == 0) {
err.println("Failed to resolve helios master in " + target);
return null;
}
return HeliosClient.newBuilder()
.setEndpointSupplier(target.getEndpointSupplier())
.setUser(username)
.build();
}
public static boolean userConfirmed(final PrintStream out, final BufferedReader stdin)
throws IOException {
out.printf("Do you want to continue? [y/N]%n");
final String line = stdin.readLine().trim();
if (line.length() < 1) {
return false;
}
final char c = line.charAt(0);
if (c != 'Y' && c != 'y') {
return false;
}
return true;
}
public static Map argToStringMap(final Namespace namespace, final Argument arg) {
final List> args = namespace.getList(arg.getDest());
final Map map = Maps.newHashMap();
if (args != null) {
for (final List group : args) {
for (final String s : group) {
final String[] parts = s.split("=", 2);
if (parts.length != 2) {
throw new IllegalArgumentException("Bad " + arg.textualName() + " value: " + s);
}
map.put(parts[0], parts[1]);
}
}
}
return map;
}
public static , V> void printMap(final PrintStream out, final String name,
final Function transform,
final Map values) {
out.print(name);
boolean first = true;
for (final K key : Ordering.natural().sortedCopy(values.keySet())) {
if (!first) {
out.print(Strings.repeat(" ", name.length()));
}
final V value = values.get(key);
out.printf("%s=%s%n", key, transform.apply(value));
first = false;
}
if (first) {
out.println();
}
}
public static List parseHostSelectors(final Namespace namespace,
final Argument arg) {
final List> args = namespace.getList(arg.getDest());
final List ret = Lists.newArrayList();
if (args != null) {
for (final List group : args) {
for (final String s : group) {
final HostSelector hostSelector = HostSelector.parse(s);
if (hostSelector == null) {
throw new IllegalArgumentException(format("Bad host selector expression: '%s'", s));
}
ret.add(hostSelector);
}
}
}
return ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy