forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
65 lines (55 loc) · 1.94 KB
/
Config.cs
File metadata and controls
65 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
public class Config : Command
{
public Config(string repository)
{
if (string.IsNullOrEmpty(repository))
{
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
else
{
WorkingDirectory = repository;
Context = repository;
_isLocal = true;
}
}
public async Task<Dictionary<string, string>> ReadAllAsync(string file = null)
{
Args = string.IsNullOrEmpty(file) ? "config -l" : $"config -l -f {file}";
var output = await ReadToEndAsync().ConfigureAwait(false);
var rs = new Dictionary<string, string>();
if (output.IsSuccess)
{
var lines = output.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var parts = line.Split('=', 2);
if (parts.Length == 2)
rs[parts[0]] = parts[1];
}
}
return rs;
}
public async Task<string> GetAsync(string key)
{
Args = $"config {key}";
var rs = await ReadToEndAsync().ConfigureAwait(false);
return rs.StdOut.Trim();
}
public async Task<bool> SetAsync(string key, string value, bool allowEmpty = false)
{
var scope = _isLocal ? "--local" : "--global";
if (!allowEmpty && string.IsNullOrWhiteSpace(value))
Args = $"config {scope} --unset {key}";
else
Args = $"config {scope} {key} {value.Quoted()}";
return await ExecAsync().ConfigureAwait(false);
}
private bool _isLocal = false;
}
}