aboutsummaryrefslogtreecommitdiff
path: root/src/GpgSigner.cs
blob: 76943f9f30c9839d5c97444d4d9296664e2d74b5 (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
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Tools.Build.Executor.Constants;

namespace VNLib.Tools.Build.Executor
{
    public sealed class GpgSigner(bool enabled, string? defaultKey)
    {
        public bool IsEnabled { get; } = enabled;

        public async Task SignFileAsync(FileInfo file)
        {
            if (!IsEnabled)
            {
                return;
            }

            List<string> args = [ "--detach-sign" ];

            if (!string.IsNullOrWhiteSpace(defaultKey))
            {
                //Set the preferred key
                args.Add("--default-key");
                args.Add(defaultKey);
            }

            //Add input file
            args.Add(file.FullName);

            //Delete an original file 
            string sigFile = $"{file.FullName}.sig";
            if (File.Exists(sigFile))
            {
                File.Delete(sigFile);
            }

            int result = await Utils.RunProcessAsync("gpg", null, args.ToArray());

            switch (result)
            {
                case 2:
                case 0:
                    break;
                default:
                    throw new Exception($"Failed to sign file {file.FullName}");
            }
        }
    }
}