Skip to content

Commit aebcda3

Browse files
committed
Add --existing option to set subcommand to only set a value if that key is already present
1 parent 61a6750 commit aebcda3

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ dotnet json set <file> <key> <value> [-o|--output <output file>]
6161

6262
**Options:**
6363
- _-o|--output file_ Write the result to a custom output file instead of using the input file
64+
- _-e|--existing_ Only set the value if the key already exists in the json file, otherwise do nothing
6465

6566
### remove
6667

dotnet-json.Tests/Commands/SetCommandTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@ public async Task SavesNewValueToFile(string json, string key, string value)
4343
result.Should().Be(value);
4444
}
4545

46+
[Fact]
47+
public async Task Existing_DoesNotChangeFileIfKeyDoesNotExist()
48+
{
49+
var json = @"{""key1"":""value""}";
50+
var filename = Path.Join(_tmpDir, "test.json");
51+
await File.WriteAllTextAsync(filename, json);
52+
53+
var (exitCode, output) = await RunCommand(filename, "key2", "newvalue", "--compressed", "--existing");
54+
55+
exitCode.Should().Be(0);
56+
57+
var contents = await File.ReadAllTextAsync(filename);
58+
contents.Should().Be(json);
59+
}
60+
61+
[Fact]
62+
public async Task Existing_UpdatesValueIfKeyDoesExist()
63+
{
64+
var json = @"{""key1"":""value"",""key2"":""value""}";
65+
var filename = Path.Join(_tmpDir, "test.json");
66+
await File.WriteAllTextAsync(filename, json);
67+
68+
var (exitCode, output) = await RunCommand(filename, "key2", "newvalue", "--compressed", "--existing");
69+
70+
exitCode.Should().Be(0);
71+
72+
var contents = await File.ReadAllTextAsync(filename);
73+
contents.Should().Be(@"{""key1"":""value"",""key2"":""newvalue""}");
74+
}
75+
4676
private async Task<(int exitCode, IConsole console)> RunCommand(params string[] arguments)
4777
{
4878
var command = new SetCommand();

dotnet-json/Commands/MergeCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class MergeCommand : CommandBase
1313
public MergeCommand() : base("merge", "merge two or more json files into one")
1414
{
1515
AddArgument(Files);
16+
AddOption(Compressed);
1617

1718
Handler = this;
1819
}

dotnet-json/Commands/RemoveCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public RemoveCommand()
2222

2323
AddAlias("rm");
2424

25+
AddOption(Compressed);
26+
2527
Handler = this;
2628
}
2729

dotnet-json/Commands/SetCommand.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ public class SetCommand : CommandBase, ICommandHandler
1212

1313
private Argument<string> Value = new Argument<string>("value", "The value to set") { Arity = ArgumentArity.ExactlyOne };
1414

15+
private Option<bool> Existing = new Option<bool>(new[] { "-e", "--existing" }, "Only set the value if the key already exists in the json file, otherwise do nothing");
16+
1517
public SetCommand() : base("set", "set a value in a json file")
1618
{
1719
AddArgument(Key);
1820
AddArgument(Value);
21+
AddOption(Existing);
22+
AddOption(Compressed);
1923

2024
Handler = this;
2125
}
@@ -24,12 +28,16 @@ protected override async Task<int> ExecuteAsync()
2428
{
2529
var key = GetParameterValue(Key) ?? throw new ArgumentException("Missing argument <key>");
2630
var value = GetParameterValue(Value) ?? throw new ArgumentException("Missing argument <value>");
31+
var existing = Context!.ParseResult.GetValueForOption(Existing);
2732

2833
JsonDocument document;
2934

3035
await using (var inputStream = GetInputStream())
3136
document = JsonDocument.ReadFromStream(inputStream);
3237

38+
if (existing && document[key] == null)
39+
return 0;
40+
3341
document[key] = value;
3442

3543
await using (var outputStream = GetOutputStream())

dotnet-json/dotnet-json.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<ToolCommandName>dotnet-json</ToolCommandName>
1515
<PackageOutputPath>./nupkg</PackageOutputPath>
1616

17-
<Version>1.0.2</Version>
17+
<Version>1.1.0</Version>
1818
<PackageId>dotnet-json</PackageId>
1919
<Authors>sleeuwen</Authors>
2020
<RepositoryUrl>https://github.com/sleeuwen/dotnet-json</RepositoryUrl>

0 commit comments

Comments
 (0)