tillrohrmann commented on a change in pull request #10532: [FLINK-15053][runtime] Escape all dynamical property values for taskmanager URL: https://github.com/apache/flink/pull/10532#discussion_r362499583
########## File path: flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/ShellScript.java ########## @@ -0,0 +1,125 @@ +/* + * 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 org.apache.flink.test.util; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; + +/** + * ShellScript for creating shell script on linux and windows. + */ +public class ShellScript { + + private static final OSType OS_TYPE = getOSType(); + + public static ShellScriptBuilder createShellScriptBuilder() { + if (OS_TYPE == OSType.OS_TYPE_WIN) { + return new WindowsShellScriptBuilder(); + } + return new UnixShellScriptBuilder(); + } + + /** + * Builder to create shell script. + */ + public abstract static class ShellScriptBuilder { + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + private final StringBuilder sb = new StringBuilder(); + + void line(String... command) { + for (String s : command) { + sb.append(s); + } + sb.append(LINE_SEPARATOR); + } + + public void write(File file) throws IOException { + try (FileWriter fwrt = new FileWriter(file); PrintWriter out = new PrintWriter(fwrt)) { + out.append(sb); + } + file.setExecutable(true, false); + } + + public abstract void command(List<String> command); + + public abstract void env(String key, String value); + } + + private static final class UnixShellScriptBuilder extends ShellScriptBuilder { + + UnixShellScriptBuilder(){ + line("#!/usr/bin/env bash"); + line(); + } + + public void command(List<String> command) { + line("exec bash -c \"", String.join(" ", command), "\""); + } + + public void env(String key, String value) { + line("export ", key, "=\"", value, "\""); + } + } + + private static final class WindowsShellScriptBuilder extends ShellScriptBuilder { + + WindowsShellScriptBuilder() { + line("@setlocal"); + line(); + } + + public void command(List<String> command) { + line("@call ", String.join(" ", command)); + } + + public void env(String key, String value) { + line("@set ", key, "=", value); + } + } + + private enum OSType { + OS_TYPE_LINUX, + OS_TYPE_WIN, + OS_TYPE_SOLARIS, + OS_TYPE_MAC, + OS_TYPE_FREEBSD, + OS_TYPE_OTHER + } + + private static OSType getOSType() { + String osName = System.getProperty("os.name"); + if (osName.startsWith("Windows")) { + return OSType.OS_TYPE_WIN; + } else if (osName.contains("SunOS") || osName.contains("Solaris")) { + return OSType.OS_TYPE_SOLARIS; + } else if (osName.contains("Mac")) { + return OSType.OS_TYPE_MAC; + } else if (osName.contains("FreeBSD")) { + return OSType.OS_TYPE_FREEBSD; + } else if (osName.startsWith("Linux")) { + return OSType.OS_TYPE_LINUX; + } else { + // Some other form of Unix + return OSType.OS_TYPE_OTHER; + } + } Review comment: I would suggest to use `OperatingSystem.readOSFromSystemProperties` instead. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
