• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google LLC
3  *
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
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.google.cloud.testing;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.file.Path;
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 /** Utility class that executes system commands on both Windows and Unix. */
26 class CommandWrapper {
27 
28   private final List<String> prefix;
29   private List<String> command;
30   private String nullFilename;
31   private boolean redirectOutputToNull;
32   private boolean redirectErrorStream;
33   private boolean redirectErrorInherit;
34   private Path directory;
35 
CommandWrapper()36   private CommandWrapper() {
37     this.prefix = new ArrayList<>();
38     if (BaseEmulatorHelper.isWindows()) {
39       this.prefix.add("cmd");
40       this.prefix.add("/C");
41       this.nullFilename = "NUL:";
42     } else {
43       this.prefix.add("bash");
44       this.nullFilename = "/dev/null";
45     }
46   }
47 
setCommand(List<String> command)48   CommandWrapper setCommand(List<String> command) {
49     this.command = new ArrayList<>(command.size() + this.prefix.size());
50     this.command.addAll(prefix);
51     this.command.addAll(command);
52     return this;
53   }
54 
setRedirectOutputToNull()55   CommandWrapper setRedirectOutputToNull() {
56     this.redirectOutputToNull = true;
57     return this;
58   }
59 
setRedirectErrorStream()60   CommandWrapper setRedirectErrorStream() {
61     this.redirectErrorStream = true;
62     return this;
63   }
64 
setRedirectErrorInherit()65   CommandWrapper setRedirectErrorInherit() {
66     this.redirectErrorInherit = true;
67     return this;
68   }
69 
setDirectory(Path directory)70   CommandWrapper setDirectory(Path directory) {
71     this.directory = directory;
72     return this;
73   }
74 
getBuilder()75   ProcessBuilder getBuilder() {
76     ProcessBuilder builder = new ProcessBuilder(command);
77     if (redirectOutputToNull) {
78       builder.redirectOutput(new File(nullFilename));
79     }
80     if (directory != null) {
81       builder.directory(directory.toFile());
82     }
83     if (redirectErrorStream) {
84       builder.redirectErrorStream(true);
85     }
86     if (redirectErrorInherit) {
87       builder.redirectError(ProcessBuilder.Redirect.INHERIT);
88     }
89     return builder;
90   }
91 
start()92   public Process start() throws IOException {
93     return getBuilder().start();
94   }
95 
create()96   static CommandWrapper create() {
97     return new CommandWrapper();
98   }
99 }
100