summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Services/ArgumentsService.cs
blob: 2ec6022fe6a85a06f2e54098289a1f944e945540 (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
using PKISharp.WACS.Configuration;
using System;
using System.Threading.Tasks;

namespace PKISharp.WACS.Services
{
    public class ArgumentsService : IArgumentsService
    {
        private readonly ILogService _log;
        private readonly ArgumentsParser _parser;
        private MainArguments? _mainArguments;

        public MainArguments MainArguments
        {
            get
            {
                if (_mainArguments == null)
                {
                    _mainArguments = _parser.GetArguments<MainArguments>();
                    if (_mainArguments == null)
                    {
                        _mainArguments = new MainArguments();
                    }
                }
                return _mainArguments;
            }
        }

        public ArgumentsService(ILogService log, ArgumentsParser parser)
        {
            _log = log;
            _parser = parser;
        }

        public T? GetArguments<T>() where T : class, new() => _parser.GetArguments<T>();

        public async Task<string?> TryGetArgument(string? providedValue, IInputService input, string what, bool secret = false) => await TryGetArgument(providedValue, input, new[] { what }, secret);

        public async Task<string?> TryGetArgument(string? providedValue, IInputService input, string[] what, bool secret = false)
        {
            if (!string.IsNullOrWhiteSpace(providedValue))
            {
                return providedValue;
            }

            if (secret)
            {
                return await input.ReadPassword(what[0]);
            }

            var raw = await input.RequestString(what);
            if (string.IsNullOrWhiteSpace(raw))
            {
                return null;
            }
            else
            {
                return raw;
            }
        }

        public string TryGetRequiredArgument(string optionName, string? providedValue)
        {
            if (string.IsNullOrWhiteSpace(providedValue))
            {
                _log.Error("Option --{optionName} not provided", optionName.ToLower());
                throw new Exception($"Option --{optionName.ToLower()} not provided");
            }
            return providedValue;
        }

        public void ShowHelp() => _parser.ShowArguments();

        public bool Valid => _parser.Validate();
        public bool Active => _parser.Active();

        public void ShowCommandLine() => _parser.ShowCommandLine();

        /// <summary>
        /// Is the command (e.g. --cancel or --renew)
        /// filtered for specific renewals
        /// </summary>
        /// <returns></returns>
        public bool HasFilter()
        {
            if (MainArguments == null)
            {
                return false;
            }
            return
                !string.IsNullOrEmpty(MainArguments.Id) ||
                !string.IsNullOrEmpty(MainArguments.FriendlyName);
        }
    }
}