Mercurial > jhg
annotate src/org/tmatesoft/hg/core/HgUpdateConfigCommand.java @ 669:b1a3a056d7e6
Pull: sample command-line utility
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 11 Jul 2013 20:49:33 +0200 |
parents | 0205a5c4566b |
children |
rev | line source |
---|---|
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012 TMate Software Ltd |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.core; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import java.io.File; |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
20 import java.io.FileInputStream; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
21 import java.io.FileOutputStream; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.IOException; |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
23 import java.util.LinkedList; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import java.util.List; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import java.util.Map; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 import org.tmatesoft.hg.internal.ConfigFile; |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
28 import org.tmatesoft.hg.internal.ConfigFileParser; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 import org.tmatesoft.hg.internal.Internals; |
382
82336b7c54f4
Per-repository UpdateConfigCommand completed. Access to system properties through SessionContext to ease alternation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
30 import org.tmatesoft.hg.repo.HgInternals; |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
31 import org.tmatesoft.hg.repo.HgInvalidStateException; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 import org.tmatesoft.hg.repo.HgRepository; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 /** |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
35 * Command to alter Mercurial configuration settings at various levels (system-wide, user-wide, repository-wide). |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 * |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 * @author Artem Tikhomirov |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 * @author TMate Software Ltd. |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 */ |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 public final class HgUpdateConfigCommand extends HgAbstractCommand<HgUpdateConfigCommand> { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
42 private final SessionContext sessionCtx; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 private final File configFile; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
45 private final List<Operation> changes = new LinkedList<Operation>(); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
46 private boolean ignoreMissingKeys = false; |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
427
diff
changeset
|
47 |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
427
diff
changeset
|
48 private HgUpdateConfigCommand(SessionContext sessionContext, File configurationFile) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
427
diff
changeset
|
49 sessionCtx = sessionContext; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 configFile = configurationFile; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 public static HgUpdateConfigCommand forRepository(HgRepository hgRepo) { |
493
ba36f66c32b4
Refactor to keep knowledge about repository control files and their location in respect to .hg/ in a single place (facilitate future adoption of shared repositories)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
490
diff
changeset
|
54 return new HgUpdateConfigCommand(hgRepo.getSessionContext(), HgInternals.getImplementationRepo(hgRepo).getFileFromRepoDir("hgrc")); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 |
382
82336b7c54f4
Per-repository UpdateConfigCommand completed. Access to system properties through SessionContext to ease alternation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
57 public static HgUpdateConfigCommand forUser(SessionContext ctx) { |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
427
diff
changeset
|
58 return new HgUpdateConfigCommand(ctx, Internals.getUserConfigurationFileToWrite(ctx)); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 |
382
82336b7c54f4
Per-repository UpdateConfigCommand completed. Access to system properties through SessionContext to ease alternation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
61 public static HgUpdateConfigCommand forInstallation(SessionContext ctx) { |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
427
diff
changeset
|
62 return new HgUpdateConfigCommand(ctx, Internals.getInstallationConfigurationFileToWrite(ctx)); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 } |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
64 |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 /** |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
66 * Remove an entry altogether. If no entry with the key found, {@link #execute()} fails. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
67 * |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
68 * @param section identifies section to alter, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
69 * @param key identifies entry within section, not <code>null</code> or otherwise ill-formed name |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 * @return <code>this</code> for convenience |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
71 * @throws IllegalArgumentException if arguments are <code>null</code> or empty |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 */ |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
73 public HgUpdateConfigCommand remove(String section, String key) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
74 checkSection(section); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
75 checkKey(key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
76 changes.add(Operation.deleteEntry(section, key)); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 return this; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 /** |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
81 * Delete single attribute in a multi-valued property. If specified value not found among values of |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
82 * the identified entry, {@link #execute()} fails. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
83 * |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
84 * @param section identifies section to alter, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
85 * @param key identifies entry within section, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
86 * @param value one of the values to remove, not <code>null</code> or an empty value |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 * @return <code>this</code> for convenience |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
88 * @throws IllegalArgumentException if arguments are <code>null</code> or empty |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 */ |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
90 public HgUpdateConfigCommand remove(String section, String key, String value) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
91 checkSection(section); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
92 checkKey(key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
93 changes.add(Operation.deleteValue(section, key, value)); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 return this; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 /** |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
98 * Set single-valued property or update multi-valued with a single value |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
99 * |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
100 * @param section identifies section to alter, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
101 * @param key identifies entry within section, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
102 * @param value new value, may be <code>null</code> |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 * @return <code>this</code> for convenience |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
104 * @throws IllegalArgumentException if arguments are <code>null</code> or empty |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 */ |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
106 public HgUpdateConfigCommand put(String section, String key, String value) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
107 checkSection(section); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
108 checkKey(key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
109 changes.add(Operation.setValue(section, key, value)); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
110 return this; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 |
427
31a89587eb04
FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
113 /** |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
114 * Add value to a multi-valued entry. If specified entry not found, {@link #execute()} fails. |
427
31a89587eb04
FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
115 * |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
116 * @param section identifies section to alter, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
117 * @param key identifies entry within section, not <code>null</code> or otherwise ill-formed name |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
118 * @param value new value to add, not <code>null</code> or an empty value |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
119 * @return <code>this</code> for convenience |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
120 * @throws IllegalArgumentException if arguments are <code>null</code> or empty |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
121 */ |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
122 public HgUpdateConfigCommand add(String section, String key, String value) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
123 checkSection(section); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
124 checkKey(key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
125 changes.add(Operation.addValue(section, key, value)); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
126 return this; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
127 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
128 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
129 /** |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
130 * Tells whether {@link #execute()} shall fail with exception if keys selected for modification were not found. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
131 * If <code>true</code>, missing keys would be silently ignored. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
132 * When <code>false</code>(<em>default</em>), exception would be raised. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
133 * |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
134 * @param ignoreMissing pass <code>true</code> to ignore any incorrect keys |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
135 * @return <code>this</code> for convenience |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
136 */ |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
137 public HgUpdateConfigCommand ignoreMissing(boolean ignoreMissing) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
138 ignoreMissingKeys = ignoreMissing; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
139 return this; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
140 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
141 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
142 /** |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
143 * Perform configuration file update. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
144 * |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
145 * @throws HgMissingConfigElementException if attempt to alter an entry failed to find one, and missing keys are not ignored |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
146 * @throws HgIOException when configuration file read/write attemt has failed |
427
31a89587eb04
FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
147 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state |
31a89587eb04
FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
148 */ |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
149 public void execute() throws HgMissingConfigElementException, HgIOException, HgException { |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 try { |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
151 ConfigFile cfgRead = new ConfigFile(sessionCtx); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
152 cfgRead.addLocation(configFile); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
153 ConfigFileParser cfgWrite = new ConfigFileParser(); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
154 FileInputStream fis = new FileInputStream(configFile); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
155 cfgWrite.parse(fis); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
156 fis.close(); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
157 for (Operation op : changes) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
158 if (!ignoreMissingKeys && !cfgRead.hasSection(op.section)) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
159 throw new HgMissingConfigElementException("Bad section name", op.section, op.key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
160 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
161 Map<String, String> sect = cfgRead.getSection(op.section); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
162 if (!ignoreMissingKeys && !sect.containsKey(op.key)) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
163 throw new HgMissingConfigElementException("Bad key name", op.section, op.key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
164 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
165 String oldValue = sect.get(op.key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
166 if (oldValue == null) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
167 oldValue = ""; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
168 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
169 switch (op.kind) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
170 case AddValue: { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
171 String separator = ", "; // XXX shall parse and find out separator kind in use |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
172 String newValue = oldValue + separator + op.value; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
173 if (sect.containsKey(op.key)) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
174 cfgWrite.change(op.section, op.key, newValue); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
175 } else { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
176 cfgWrite.add(op.section, op.key, newValue); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
177 } |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
178 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
179 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
180 case DelValue: { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
181 if (!ignoreMissingKeys && (oldValue.length() == 0 || !oldValue.contains(op.value))) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
182 throw new HgMissingConfigElementException(String.format("Bad value '%s' to delete from '%s'", op.value, oldValue), op.section, op.key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
183 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
184 int start = oldValue.indexOf(op.value); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
185 if (start == -1) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
186 // nothing to change |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
187 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
188 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
189 int commaPos = -1; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
190 for (int i = start-1; i >=0; i--) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
191 if (oldValue.charAt(i) == ',') { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
192 commaPos = i; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
193 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
194 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
195 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
196 for (int i = start + op.value.length(); commaPos == -1 && i < oldValue.length(); i++) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
197 if (oldValue.charAt(i) == ',') { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
198 commaPos = i; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
199 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
200 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
201 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
202 String newValue; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
203 if (commaPos >= 0) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
204 if (commaPos < start) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
205 // from preceding comma up to end of value |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
206 newValue = oldValue.substring(0, commaPos) + oldValue.substring(start + op.value.length()); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
207 } else { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
208 // from value start up to and including subsequent comma |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
209 newValue = oldValue.substring(0, start) + oldValue.substring(commaPos+1); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
210 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
211 } else { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
212 // found no separator, just remove the value |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
213 // extra whitespaces (if space, not a comma is a separator) won't hurt |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
214 newValue = oldValue.substring(0, start) + oldValue.substring(start + op.value.length()); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
215 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
216 cfgWrite.change(op.section, op.key, newValue); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
217 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
218 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
219 case SetValue: { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
220 if (sect.containsKey(op.key)) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
221 cfgWrite.change(op.section, op.key, op.value); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
222 } else { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
223 cfgWrite.add(op.section, op.key, op.value); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
224 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
225 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
226 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
227 case DelEntry: { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
228 cfgWrite.delete(op.section, op.key); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
229 break; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
230 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
231 default: throw new HgInvalidStateException(String.format("Unknown change %s", op.kind)); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
232 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
233 } |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
234 FileOutputStream fos = new FileOutputStream(configFile); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
235 cfgWrite.update(fos); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
236 fos.close(); |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
237 } catch (IOException ex) { |
427
31a89587eb04
FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
238 String m = String.format("Failed to update configuration file %s", configFile); |
31a89587eb04
FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
239 throw new HgBadArgumentException(m, ex); // TODO [post-1.0] better exception, it's not bad argument case |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
240 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
241 } |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
242 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
243 private static void checkSection(String section) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
244 if (section == null || section.trim().length() == 0) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
245 throw new IllegalArgumentException(String.format("Section name can't be empty: %s", section)); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
246 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
247 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
248 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
249 private static void checkKey(String key) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
250 if (key == null || key.trim().length() == 0) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
251 throw new IllegalArgumentException(String.format("Entry key can't be empty: %s", key)); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
252 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
253 } |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
254 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
255 |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
256 private static class Operation { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
257 private enum OpKind { AddValue, SetValue, DelValue, DelEntry }; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
258 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
259 public final OpKind kind; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
260 public final String section; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
261 public final String key; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
262 public final String value; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
263 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
264 private Operation(OpKind t, String s, String k, String v) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
265 kind = t; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
266 section = s; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
267 key = k; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
268 value = v; |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
269 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
270 |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
271 public static Operation deleteEntry(String section, String key) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
272 return new Operation(OpKind.DelEntry, section, key, null); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
273 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
274 public static Operation deleteValue(String section, String key, String value) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
275 if (value == null || value.trim().length() == 0) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
276 throw new IllegalArgumentException(String.format("Can't remove empty value '%s'", value)); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
277 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
278 return new Operation(OpKind.DelValue, section, key, value); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
279 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
280 public static Operation addValue(String section, String key, String value) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
281 if (value == null || value.trim().length() == 0) { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
282 throw new IllegalArgumentException(String.format("Can't add empty value '%s'", value)); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
283 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
284 return new Operation(OpKind.AddValue, section, key, value); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
285 } |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
286 public static Operation setValue(String section, String key, String value) throws IllegalArgumentException { |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
287 return new Operation(OpKind.SetValue, section, key, value); |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
493
diff
changeset
|
288 } |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
289 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
290 } |