1 /*
2 * LICENSE
3 *
4 * "THE BEER-WARE LICENSE" (Revision 43):
5 * "Sven Strittmatter" <weltraumschaf@googlemail.com> wrote this file.
6 * As long as you retain this notice you can do whatever you want with
7 * this stuff. If we meet some day, and you think this stuff is worth it,
8 * you can buy me a non alcohol-free beer in return.
9 *
10 * Copyright (C) 2012 "Sven Strittmatter" <weltraumschaf@googlemail.com>
11 */
12 package org.jenkinsci.plugins.darcs.cmd;
13
14 import hudson.util.ArgumentListBuilder;
15 import org.apache.commons.lang.Validate;
16
17 /**
18 * Builder for `darcs pull` command.
19 *
20 * Example: {@literal `darcs pull FROM [--repo=REPDIR] [--all] [--verbose]`}
21 *
22 * @author Sven Strittmatter <weltraumschaf@googlemail.com>
23 */
24 public class DarcsPullBuilder extends DarcsBaseCommandBuilder implements DarcsCommandCreator {
25
26 /**
27 * Darcs subcommand.
28 */
29 private static final String COMMAND = "pull";
30 /**
31 * From where to get.
32 */
33 private String from = "";
34 /**
35 * Where to pull in.
36 */
37 private String repoDir = "";
38 /**
39 * Whether to pull all patches.
40 */
41 private boolean all;
42 /**
43 * Whether to use verbose output.
44 */
45 private boolean verbose;
46
47 /**
48 * Initializes the {@link DarcsBaseCommandBuilder#command} with {@value #COMMAND}.
49 *
50 * @param darcsExe name of Darcs executable, e.g. "darcs" or "/usr/local/bin/darcs"
51 */
52 DarcsPullBuilder(final String darcsExe) {
53 super(darcsExe, COMMAND);
54 }
55
56 /**
57 * From where to pull into the repository.
58 *
59 * @param location file or URI
60 * @return the builder itself
61 * CHECKSTYLE:OFF
62 * @throws IllegalArgumentException if location is {@code null} or empty
63 * CHECKSTYLE:ON
64 */
65 public DarcsPullBuilder from(final String location) {
66 Validate.notEmpty(location);
67 from = location;
68 return this;
69 }
70
71 /**
72 * Directory of repository to pull in.
73 *
74 * @param directory path to repository
75 * @return the builder itself
76 * CHECKSTYLE:OFF
77 * @throws IllegalArgumentException if location is {@code null} or empty
78 * CHECKSTYLE:ON
79 */
80 public DarcsPullBuilder repoDir(final String directory) {
81 Validate.notEmpty(directory);
82 repoDir = directory;
83 return this;
84 }
85
86 /**
87 * Pull all patches.
88 *
89 * @return the builder itself
90 */
91 public DarcsPullBuilder all() {
92 all = true;
93 return this;
94 }
95
96 /**
97 * Use verbose output.
98 *
99 * @return the builder itself
100 */
101 public DarcsPullBuilder verbose() {
102 verbose = true;
103 return this;
104 }
105
106 @Override
107 public DarcsCommand create() {
108 Validate.notEmpty(from, "Set from where to pull the patches!");
109 final ArgumentListBuilder arguments = createArgumentList();
110 arguments.add(from);
111
112 if (repoDir.length() > 0) {
113 arguments.add(String.format("--repo=%s", repoDir));
114 }
115
116 if (all) {
117 arguments.add("--all");
118 }
119
120 if (verbose) {
121 arguments.add("--verbose");
122 }
123
124 return new DarcsCommand(arguments);
125 }
126
127 }