/hudson-core/src/main/java/hudson/cli/UpdateJobCommand.java

http://github.com/hudson/hudson · Java · 84 lines · 45 code · 10 blank · 29 comment · 6 complexity · 10e83ad34d0b9f6463ad1dbdaa800726 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.cli;
  25. import hudson.model.Hudson;
  26. import hudson.Extension;
  27. import hudson.model.Item;
  28. import hudson.model.Items;
  29. import hudson.model.Job;
  30. import hudson.model.TopLevelItem;
  31. import hudson.util.IOUtils;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import org.kohsuke.args4j.Argument;
  35. /**
  36. * Updates or creates a job by reading stdin as a configuration XML file.
  37. *
  38. * @author Henrik Lynggaard Hansen
  39. */
  40. @Extension
  41. public class UpdateJobCommand extends CLICommand {
  42. @Override
  43. public String getShortDescription() {
  44. return "Updates and potentionally creates a job by reading stdin as a configuration XML file.";
  45. }
  46. @Argument(metaVar = "NAME", usage = "Name of the job to update", required = true)
  47. public String name;
  48. @Argument(metaVar = "CREATE", usage = "Create the job if needed", index = 1, required = true)
  49. public Boolean create;
  50. protected int run() throws Exception {
  51. Hudson h = Hudson.getInstance();
  52. TopLevelItem item = h.getItem(name);
  53. if (item == null && !create) {
  54. stderr.println("Job '" + name + "' does not exist and create is set to false");
  55. return -1;
  56. }
  57. if (item == null) {
  58. h.checkPermission(Item.CREATE);
  59. h.createProjectFromXML(name, stdin);
  60. } else {
  61. try {
  62. h.checkPermission(Job.CONFIGURE);
  63. File rootDirOfJob = new File(new File(h.getRootDir(), "jobs"), name);
  64. // place it as config.xml
  65. File configXml = Items.getConfigFile(rootDirOfJob).getFile();
  66. IOUtils.copy(stdin, configXml);
  67. item = h.reloadProjectFromDisk(configXml.getParentFile());
  68. } catch (IOException e) {
  69. throw e;
  70. }
  71. }
  72. return 0;
  73. }
  74. }