ArgumentListBuilder.java 15.6 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
5
 * Alan Harder, Yahoo! Inc.
K
kohsuke 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
K
kohsuke 已提交
25 26
package hudson.util;

27
import hudson.Launcher;
K
kohsuke 已提交
28 29
import hudson.Util;

K
kohsuke 已提交
30 31
import java.util.ArrayList;
import java.util.List;
K
kohsuke 已提交
32
import java.util.Arrays;
K
kohsuke 已提交
33
import java.util.Map;
K
kohsuke 已提交
34 35
import java.util.BitSet;
import java.util.Properties;
K
kohsuke 已提交
36
import java.util.Map.Entry;
37
import java.io.Serializable;
K
kohsuke 已提交
38 39
import java.io.File;
import java.io.IOException;
40
import java.util.Set;
41
import javax.annotation.Nonnull;
K
kohsuke 已提交
42 43 44 45 46 47

/**
 * Used to build up arguments for a process invocation.
 *
 * @author Kohsuke Kawaguchi
 */
K
Kohsuke Kawaguchi 已提交
48
public class ArgumentListBuilder implements Serializable, Cloneable {
49
    private final List<String> args = new ArrayList<>();
50 51 52
    /**
     * Bit mask indicating arguments that shouldn't be echoed-back (e.g., password)
     */
K
kohsuke 已提交
53
    private BitSet mask = new BitSet();
K
kohsuke 已提交
54

K
kohsuke 已提交
55 56 57 58 59 60 61
    public ArgumentListBuilder() {
    }

    public ArgumentListBuilder(String... args) {
        add(args);
    }

62
    public ArgumentListBuilder add(Object a) {
63 64 65 66 67 68 69 70
        return add(a.toString(), false);
    }

    /**
     * @since 1.378
     */
    public ArgumentListBuilder add(Object a, boolean mask) {
        return add(a.toString(), mask);
71 72
    }

K
kohsuke 已提交
73
    public ArgumentListBuilder add(File f) {
74
        return add(f.getAbsolutePath(), false);
K
kohsuke 已提交
75 76
    }

K
kohsuke 已提交
77
    public ArgumentListBuilder add(String a) {
78 79 80 81
        return add(a,false);
    }

    /**
82 83 84 85 86 87
     * Optionally hide this part of the command line from being printed to the log.
     * @param a a command argument
     * @param mask true to suppress in output, false to print normally
     * @return this
     * @see hudson.Launcher.ProcStarter#masks(boolean[])
     * @see Launcher#maskedPrintCommandLine(List, boolean[], FilePath)
88 89 90 91 92 93 94
     * @since 1.378
     */
    public ArgumentListBuilder add(String a, boolean mask) {
        if(a!=null) {
            if(mask) {
                this.mask.set(args.size());
            }
95
            args.add(a);
96
        }
K
kohsuke 已提交
97 98
        return this;
    }
99
    
K
kohsuke 已提交
100
    public ArgumentListBuilder prepend(String... args) {
K
kohsuke 已提交
101 102 103 104 105 106
        // left-shift the mask
        BitSet nm = new BitSet(this.args.size()+args.length);
        for(int i=0; i<this.args.size(); i++)
            nm.set(i+args.length, mask.get(i));
        mask = nm;

K
kohsuke 已提交
107 108 109 110
        this.args.addAll(0, Arrays.asList(args));
        return this;
    }

K
kohsuke 已提交
111 112 113 114 115
    /**
     * Adds an argument by quoting it.
     * This is necessary only in a rare circumstance,
     * such as when adding argument for ssh and rsh.
     *
K
kohsuke 已提交
116
     * Normal process invocations don't need it, because each
K
kohsuke 已提交
117 118 119
     * argument is treated as its own string and never merged into one. 
     */
    public ArgumentListBuilder addQuoted(String a) {
120 121 122 123 124 125 126 127
        return add('"'+a+'"', false);
    }

    /**
     * @since 1.378
     */
    public ArgumentListBuilder addQuoted(String a, boolean mask) {
        return add('"'+a+'"', mask);
K
kohsuke 已提交
128 129 130 131 132 133 134 135
    }

    public ArgumentListBuilder add(String... args) {
        for (String arg : args) {
            add(arg);
        }
        return this;
    }
136 137
    
    /**
138
     * @since 2.72
139 140 141 142 143 144 145
     */
    public ArgumentListBuilder add(@Nonnull Iterable<String> args) {
        for (String arg : args) {
            add(arg);
        }
        return this;
    }
K
kohsuke 已提交
146 147 148 149 150

    /**
     * Decomposes the given token into multiple arguments by splitting via whitespace.
     */
    public ArgumentListBuilder addTokenized(String s) {
K
kohsuke 已提交
151
        if(s==null) return this;
K
kohsuke 已提交
152
        add(Util.tokenize(s));
K
kohsuke 已提交
153 154 155
        return this;
    }

156 157 158 159 160 161 162 163 164
    /**
     * @since 1.378
     */
    public ArgumentListBuilder addKeyValuePair(String prefix, String key, String value, boolean mask) {
        if(key==null) return this;
        add(((prefix==null)?"-D":prefix)+key+'='+value, mask);
        return this;
    }

K
kohsuke 已提交
165
    /**
166
     * Adds key value pairs as "-Dkey=value -Dkey=value ..."
K
kohsuke 已提交
167
     *
168
     * {@code -D} portion is configurable as the 'prefix' parameter.
K
kohsuke 已提交
169 170 171 172
     * @since 1.114
     */
    public ArgumentListBuilder addKeyValuePairs(String prefix, Map<String,String> props) {
        for (Entry<String,String> e : props.entrySet())
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            addKeyValuePair(prefix, e.getKey(), e.getValue(), false);
        return this;
    }

    /**
     * Adds key value pairs as "-Dkey=value -Dkey=value ..." with masking.
     *
     * @param prefix
     *      Configures the -D portion of the example. Defaults to -D if null.
     * @param props
     *      The map of key/value pairs to add
     * @param propsToMask
     *      Set containing key names to mark as masked in the argument list. Key
     *      names that do not exist in the set will be added unmasked.
     * @since 1.378
     */
    public ArgumentListBuilder addKeyValuePairs(String prefix, Map<String,String> props, Set<String> propsToMask) {
        for (Entry<String,String> e : props.entrySet()) {
191
            addKeyValuePair(prefix, e.getKey(), e.getValue(), (propsToMask != null) && propsToMask.contains(e.getKey()));
192
        }
K
kohsuke 已提交
193 194 195
        return this;
    }

196 197 198 199
    /**
     * Adds key value pairs as "-Dkey=value -Dkey=value ..." by parsing a given string using {@link Properties}.
     *
     * @param prefix
200
     *      The '-D' portion of the example. Defaults to -D if null.
201 202 203 204
     * @param properties
     *      The persisted form of {@link Properties}. For example, "abc=def\nghi=jkl". Can be null, in which
     *      case this method becomes no-op.
     * @param vr
205
     *      {@link VariableResolver} to resolve variables in properties string.
206 207
     * @since 1.262
     */
208 209
    public ArgumentListBuilder addKeyValuePairsFromPropertyString(String prefix, String properties, VariableResolver<String> vr) throws IOException {
        return addKeyValuePairsFromPropertyString(prefix, properties, vr, null);
210 211 212 213 214 215 216 217 218 219 220
    }

    /**
     * Adds key value pairs as "-Dkey=value -Dkey=value ..." by parsing a given string using {@link Properties} with masking.
     *
     * @param prefix
     *      The '-D' portion of the example. Defaults to -D if null.
     * @param properties
     *      The persisted form of {@link Properties}. For example, "abc=def\nghi=jkl". Can be null, in which
     *      case this method becomes no-op.
     * @param vr
221
     *      {@link VariableResolver} to resolve variables in properties string.
222 223 224 225 226
     * @param propsToMask
     *      Set containing key names to mark as masked in the argument list. Key
     *      names that do not exist in the set will be added unmasked.
     * @since 1.378
     */
227
    public ArgumentListBuilder addKeyValuePairsFromPropertyString(String prefix, String properties, VariableResolver<String> vr, Set<String> propsToMask) throws IOException {
228 229
        if(properties==null)    return this;

230 231
        properties = Util.replaceMacro(properties, propertiesGeneratingResolver(vr));

232
        for (Entry<Object,Object> entry : Util.loadProperties(properties).entrySet()) {
233
            addKeyValuePair(prefix, (String)entry.getKey(), entry.getValue().toString(), (propsToMask != null) && propsToMask.contains(entry.getKey()));
K
kohsuke 已提交
234 235 236 237
        }
        return this;
    }

238 239 240 241 242 243 244 245 246
    /**
     * Creates a resolver generating values to be safely placed in properties string.
     *
     * {@link Properties#load} generally removes single backslashes from input and that
     * is not desirable for outcomes of macro substitution as the values can
     * contain them but user has no way to escape them.
     *
     * @param original Resolution will be delegated to this resolver. Resolved
     *                 values will be escaped afterwards.
247
     * @see <a href="https://jenkins-ci.org/issue/10539">JENKINS-10539</a>
248 249 250 251 252 253 254 255 256 257 258 259 260 261
     */
    private static VariableResolver<String> propertiesGeneratingResolver(final VariableResolver<String> original) {

        return new VariableResolver<String>() {

            public String resolve(String name) {
                final String value = original.resolve(name);
                if (value == null) return null;
                // Substitute one backslash with two
                return value.replaceAll("\\\\", "\\\\\\\\");
            }
        };
    }

K
kohsuke 已提交
262
    public String[] toCommandArray() {
263
        return args.toArray(new String[0]);
K
kohsuke 已提交
264
    }
K
kohsuke 已提交
265
    
266
    @Override
K
kohsuke 已提交
267 268 269
    public ArgumentListBuilder clone() {
        ArgumentListBuilder r = new ArgumentListBuilder();
        r.args.addAll(this.args);
270
        r.mask = (BitSet) this.mask.clone();
K
kohsuke 已提交
271 272
        return r;
    }
K
kohsuke 已提交
273

K
kohsuke 已提交
274 275 276 277 278
    /**
     * Re-initializes the arguments list.
     */
    public void clear() {
        args.clear();
279
        mask.clear();
K
kohsuke 已提交
280 281
    }

K
kohsuke 已提交
282 283 284
    public List<String> toList() {
        return args;
    }
285

286 287 288 289
    /**
     * Just adds quotes around args containing spaces, but no other special characters,
     * so this method should generally be used only for informational/logging purposes.
     */
290 291 292 293 294 295 296 297 298 299 300 301
    public String toStringWithQuote() {
        StringBuilder buf = new StringBuilder();
        for (String arg : args) {
            if(buf.length()>0)  buf.append(' ');

            if(arg.indexOf(' ')>=0 || arg.length()==0)
                buf.append('"').append(arg).append('"');
            else
                buf.append(arg);
        }
        return buf.toString();
    }
302

303
    /**
J
Jesse Glick 已提交
304
     * Wrap command in a {@code CMD.EXE} call so we can return the exit code ({@code ERRORLEVEL}).
305
     * This method takes care of escaping special characters in the command, which
J
Jesse Glick 已提交
306
     * is needed since the command is now passed as a string to the {@code CMD.EXE} shell.
307 308
     * This is done as follows:
     * Wrap arguments in double quotes if they contain any of:
J
Jesse Glick 已提交
309 310 311 312
     *   {@code space *?,;^&<>|"}
     *   and if {@code escapeVars} is true, {@code %} followed by a letter.
     * <p> When testing from command prompt, these characters also need to be
     * prepended with a ^ character: {@code ^&<>|}—however, invoking {@code cmd.exe} from
313
     * Jenkins does not seem to require this extra escaping so it is not added by
314
     * this method.
J
Jesse Glick 已提交
315
     * <p> A {@code "} is prepended with another {@code "} character.  Note: Windows has issues
316
     * escaping some combinations of quotes and spaces.  Quotes should be avoided.
J
Jesse Glick 已提交
317
     * <p> If {@code escapeVars} is true, a {@code %} followed by a letter has that letter wrapped
318
     * in double quotes, to avoid possible variable expansion.
J
Jesse Glick 已提交
319 320 321 322
     * ie, {@code %foo%} becomes {@code "%"f"oo%"}.  The second {@code %} does not need special handling
     * because it is not followed by a letter. <p>
     * Example: {@code "-Dfoo=*abc?def;ghi^jkl&mno<pqr>stu|vwx""yz%"e"nd"}
     * @param escapeVars True to escape {@code %VAR%} references; false to leave these alone
323
     *                   so they may be expanded when the command is run
J
Jesse Glick 已提交
324
     * @return new {@link ArgumentListBuilder} that runs given command through {@code cmd.exe /C}
A
alanharder 已提交
325
     * @since 1.386
326
     */
327
    public ArgumentListBuilder toWindowsCommand(boolean escapeVars) {
328
    	ArgumentListBuilder windowsCommand = new ArgumentListBuilder().add("cmd.exe", "/C");
329
        boolean quoted, percent;
330
        for (int i = 0; i < args.size(); i++) {
331 332
            StringBuilder quotedArgs = new StringBuilder();
            String arg = args.get(i);
333
            quoted = percent = false;
334 335
            for (int j = 0; j < arg.length(); j++) {
                char c = arg.charAt(j);
336
                if (!quoted && (c == ' ' || c == '*' || c == '?' || c == ',' || c == ';')) {
337
                    quoted = startQuoting(quotedArgs, arg, j);
338 339
                }
                else if (c == '^' || c == '&' || c == '<' || c == '>' || c == '|') {
340
                    if (!quoted) quoted = startQuoting(quotedArgs, arg, j);
341
                    // quotedArgs.append('^'); See note in javadoc above
342 343
                }
                else if (c == '"') {
344
                    if (!quoted) quoted = startQuoting(quotedArgs, arg, j);
345 346
                    quotedArgs.append('"');
                }
347 348
                else if (percent && escapeVars
                         && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) {
349
                    if (!quoted) quoted = startQuoting(quotedArgs, arg, j);
350 351 352 353 354 355
                    quotedArgs.append('"').append(c);
                    c = '"';
                }
                percent = (c == '%');
                if (quoted) quotedArgs.append(c);
            }
356 357 358 359 360 361 362
            if (i == 0) {
                if (quoted) {
                    quotedArgs.insert(0, '"'); 
                } else {
                    quotedArgs.append('"');
                }
            }
363
            if (quoted) quotedArgs.append('"'); else quotedArgs.append(arg);
364 365
            
            windowsCommand.add(quotedArgs, mask.get(i));
366 367 368 369 370 371
        }
        // (comment copied from old code in hudson.tasks.Ant)
        // on Windows, executing batch file can't return the correct error code,
        // so we need to wrap it into cmd.exe.
        // double %% is needed because we want ERRORLEVEL to be expanded after
        // batch file executed, not before. This alone shows how broken Windows is...
372 373
        windowsCommand.add("&&").add("exit").add("%%ERRORLEVEL%%\"");
        return windowsCommand;
374 375
    }

376 377 378 379 380 381 382 383
    /**
     * Calls toWindowsCommand(false)
     * @see #toWindowsCommand(boolean)
     */
    public ArgumentListBuilder toWindowsCommand() {
        return toWindowsCommand(false);
    }

384
    private static boolean startQuoting(StringBuilder buf, String arg, int atIndex) {
385
        buf.append('"').append(arg, 0, atIndex);
386 387 388
        return true;
    }

K
kohsuke 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    /**
     * Returns true if there are any masked arguments.
     * @return true if there are any masked arguments; false otherwise
     */
    public boolean hasMaskedArguments() {
        return mask.length()>0;
    }

    /**
     * Returns an array of booleans where the masked arguments are marked as true
     * @return an array of booleans.
     */
    public boolean[] toMaskArray() {
        boolean[] mask = new boolean[args.size()];
        for( int i=0; i<mask.length; i++)
            mask[i] = this.mask.get(i);
        return mask;
    }

    /**
     * Add a masked argument
     * @param string the argument
     */
412 413
    public void addMasked(String string) {
        add(string, true);
414 415 416 417
    }

    public ArgumentListBuilder addMasked(Secret s) {
        return add(Secret.toString(s),true);
K
kohsuke 已提交
418 419
    }

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    /**
     * Debug/error message friendly output.
     */
    public String toString() {
        StringBuilder buf = new StringBuilder();
        for (int i=0; i<args.size(); i++) {
            String arg = args.get(i);
            if (mask.get(i))
                arg = "******";

            if(buf.length()>0)  buf.append(' ');

            if(arg.indexOf(' ')>=0 || arg.length()==0)
                buf.append('"').append(arg).append('"');
            else
                buf.append(arg);
        }
        return buf.toString();
    }

440
    private static final long serialVersionUID = 1L;
K
kohsuke 已提交
441
}