sourcegit/src/Commands/Config.cs

69 lines
2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
namespace SourceGit.Commands
{
public class Config : Command
{
public Config(string repository)
{
WorkingDirectory = repository;
Context = repository;
RaiseError = false;
}
public Dictionary<string, string> ListAll()
{
2024-06-18 19:21:36 -07:00
if (string.IsNullOrEmpty(WorkingDirectory))
Args = "config --global -l";
else
Args = "config -l";
2021-04-29 05:05:55 -07:00
var output = ReadToEnd();
var rs = new Dictionary<string, string>();
if (output.IsSuccess)
{
2024-07-14 09:30:31 -07:00
var lines = output.StdOut.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var idx = line.IndexOf('=', StringComparison.Ordinal);
if (idx != -1)
{
var key = line.Substring(0, idx).Trim();
var val = line.Substring(idx + 1).Trim();
2024-07-14 09:30:31 -07:00
rs[key] = val;
}
}
}
2021-04-29 05:05:55 -07:00
return rs;
2021-04-29 05:05:55 -07:00
}
public string Get(string key)
{
2021-04-29 05:05:55 -07:00
Args = $"config {key}";
return ReadToEnd().StdOut.Trim();
2021-04-29 05:05:55 -07:00
}
public bool Set(string key, string value, bool allowEmpty = false)
{
if (!allowEmpty && string.IsNullOrWhiteSpace(value))
{
if (string.IsNullOrEmpty(WorkingDirectory))
2021-04-29 05:05:55 -07:00
Args = $"config --global --unset {key}";
else
2021-04-29 05:05:55 -07:00
Args = $"config --unset {key}";
}
else
{
if (string.IsNullOrWhiteSpace(WorkingDirectory))
Args = $"config --global {key} \"{value}\"";
else
Args = $"config {key} \"{value}\"";
2021-04-29 05:05:55 -07:00
}
return Exec();
}
}
}