2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.sample.yaml;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.stream.Collectors;
26 public class SampleYamlGenerator {
29 private SampleYamlGenerator() {
30 throw new IllegalStateException("Utility class");
32 public static void generateSampleYaml(String cmdName, List<String> input, String output, String version,
33 String targetPath, boolean debug, String name) throws IOException {
35 PrintWriter writer = new PrintWriter(targetPath, "UTF-8");
36 writeKeyValuePair(writer, "open_cli_sample_version", "1.0");
37 writeKeyValuePair(writer, "name", cmdName);
38 writeKeyValuePair(writer, "version", version);
43 writeKey(writer, "samples");
44 writeKey(writer, name);
46 writeKeyValuePair(writer, "name", cmdName);
47 writeKeyValuePair(writer, "input", input.stream().collect(Collectors.joining(" ")).trim());
48 writeKeyValuePair(writer, "moco", new File(targetPath).getName().replaceAll("-sample.yaml", "-moco.json"));
49 writeMultilineKeyValue(writer, "output", output.trim(), debug);
58 private static void writeMultilineKeyValue(PrintWriter writer, String key, String value, boolean debug) { //NOSONAR
59 writer.write(printTabs() + key + ":");
60 if (value.isEmpty()) {
65 String[] lines = value.split("\n");
66 Arrays.stream(lines).forEach(line -> writer.write(printTabs() + line + "\n")); // NOSONAR
69 private static String printTabs() {
70 StringBuffer spaces = new StringBuffer();
71 for (int i=0; i < nTab; i++) {
74 return spaces.toString();
77 private static void writeKeyValuePair(PrintWriter writer, String key, String value) {
78 writer.write(printTabs() +key + ": " + value + "\n");
81 private static void writeKey(PrintWriter writer, String key) {
82 writer.write(printTabs() + key + ":\n");
86 private static void writeEndKey() {