Skip to content

Commit

Permalink
Add ability to pass templates and exclusions via command line
Browse files Browse the repository at this point in the history
So that you don't need to construct a custom file if you just want to
pass a template or tag exclusion.

version: Unreleased
tag: Added
  • Loading branch information
Ben Zumhagen committed Nov 5, 2019
1 parent 50302ef commit b0d9854
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions dotnet-gitchanges/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using System.Linq;
using System.Reflection;
using System.Text;
using CommandLine;
using Gitchanges.Caches;
using Gitchanges.Configuration;
using Gitchanges.Readers;
using LibGit2Sharp;
using Microsoft.Extensions.Configuration;
using Stubble.Core.Builders;
using CommandLine;

namespace Gitchanges
{
Expand All @@ -20,6 +20,10 @@ public class Options
{
[Option('s', "settings", Required = false, HelpText = "Path to custom settings file.")]
public string CustomSettingsPath { get; set; }
[Option('t', "template", Required = false, HelpText = "Path to custom template file. Overrides value specified in custom settings file.")]
public string CustomTemplatePath { get; set; }
[Option('e', "exclude", Required = false, HelpText = "Comma separated tags to exclude. Overrides value specified in custom settings file.")]
public string TagsToExclude { get; set; }
}

static void Main(string[] args)
Expand All @@ -28,10 +32,18 @@ static void Main(string[] args)
Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
if (!string.IsNullOrEmpty(options.CustomSettingsPath))
{
var additionalSettings = new List<KeyValuePair<string, string>>();

if (!string.IsNullOrEmpty(options.CustomSettingsPath))
configBuilder.AddJsonFile(options.CustomSettingsPath);
}

if (!string.IsNullOrEmpty(options.CustomTemplatePath))
additionalSettings.Add(new KeyValuePair<string, string>("Template", options.CustomTemplatePath));

if (!string.IsNullOrEmpty(options.TagsToExclude))
additionalSettings.Add(new KeyValuePair<string, string>("TagsToExclude", options.TagsToExclude));

configBuilder.AddInMemoryCollection(additionalSettings);
});

var config = TryOrExit(() => configBuilder.Build(), "Failed to build configuration");
Expand All @@ -41,7 +53,7 @@ static void Main(string[] args)
var repo = TryOrExit(() => new Repository("."), "Failed to initialize repository");
var cache = new ChangeCache();
var reader = new GitReader(repo, patterns);
var tagsToExclude = (config.GetSection("TagsToExclude").Get<HashSet<string>>() ?? new HashSet<string>()).Select(tag => tag.ToLower()).ToHashSet();
var tagsToExclude = (config.GetSection("TagsToExclude").Value ?? "").Split(",").Select(tag => tag.ToLower()).ToHashSet();

foreach (var change in reader.Changes())
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet-gitchanges/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"Tag": "tag:(.*)[\n]?"
},
"Template": "",
"TagsToExclude": []
"TagsToExclude": ""
}

0 comments on commit b0d9854

Please sign in to comment.