summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Configuration/BaseArgumentsProvider.cs
blob: df42949e101668527152230945be9a1d70522d8f (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Fclp;
using Fclp.Internals;
using PKISharp.WACS.Services;
using System.Collections.Generic;

namespace PKISharp.WACS.Configuration
{
    public abstract class BaseArgumentsProvider<T> : IArgumentsProvider<T> where T : class, new()
    {
        private readonly FluentCommandLineParser<T> _parser;
        public ILogService? Log { get; set; }

        public BaseArgumentsProvider()
        {
            _parser = new FluentCommandLineParser<T>
            {
                IsCaseSensitive = false
            };
            Configure(_parser);
        }

        public abstract string Name { get; }
        public abstract string Group { get; }
        public virtual string? Condition { get; }
        public virtual bool Default => false;
        public abstract void Configure(FluentCommandLineParser<T> parser);
        bool IArgumentsProvider.Active(object current)
        {
            if (current is T typed)
            {
                return IsActive(typed);
            }
            else
            {
                return false;
            }
        }

        protected virtual bool IsActive(T current)
        {
            foreach (var prop in current.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(bool) && (bool)prop.GetValue(current) == true)
                {
                    return true;
                }
                if (prop.PropertyType == typeof(string) && !string.IsNullOrEmpty((string)prop.GetValue(current)))
                {
                    return true;
                }
                if (prop.PropertyType == typeof(int) && (int)prop.GetValue(current) > 0)
                {
                    return true;
                }
                if (prop.PropertyType == typeof(int?) && (int?)prop.GetValue(current) != null)
                {
                    return true;
                }
                if (prop.PropertyType == typeof(long) && (long)prop.GetValue(current) > 0)
                {
                    return true;
                }
                if (prop.PropertyType == typeof(long?) && (long?)prop.GetValue(current) != null)
                {
                    return true;
                }
            }
            return false;
        }

        public virtual bool Validate(T current, MainArguments main)
        {
            if (main.Renew)
            {
                if (IsActive(current))
                {
                    Log?.Error($"Renewal {(string.IsNullOrEmpty(Group)?"":$"{Group} ")}parameters cannot be changed during a renewal. Recreate/overwrite the renewal or edit the .json file if you want to make changes.");
                    return false;
                }
            }
            return true;
        }

        bool IArgumentsProvider.Validate(object current, MainArguments main) => Validate((T)current, main);

        public IEnumerable<ICommandLineOption> Configuration => _parser.Options;

        public ICommandLineParserResult GetParseResult(string[] args) => _parser.Parse(args);

        public T? GetResult(string[] args)
        {
            var result = _parser.Parse(args);
            if (result.HasErrors)
            {
                Log?.Error(result.ErrorText);
                return null;
            }
            return _parser.Object;
        }
        object? IArgumentsProvider.GetResult(string[] args) => GetResult(args);

    }
}