summaryrefslogtreecommitdiff
path: root/Program.cs
blob: db48b83b09d7099c951d9fadfc902205cd90c8fb (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
using HyperBooru.ApiClient;
using System.CommandLine;

namespace HyperBooru.CommandLine;

public static class Program {
    public static readonly Option<string> UrlOption = new("--url") {
        Description = "API base URL to connect to",
        Required    = true
    };

    public static readonly Option<string> UsernameOption = new("--username") {
        Description = "Username to connect with",
        Required    = true
    };

    public static readonly Option<string> PasswordOption = new("--password") {
        Description = "Password to connect with",
        Required    = true
    };

    public static readonly Option<FileInfo> FileOption = new("--file") {
        Description = "File to upload",
        Required    = true
    };

    public static readonly Option<bool> SkipCertCheckOption = new("--skip-cert-check") {
        Description = "Don't validate server CA certificate"
    };

    public static readonly Option[] Options = [
        UrlOption,
        UsernameOption,
        PasswordOption,
        FileOption,
        SkipCertCheckOption
    ];

    public static void Main(string[] args) {
        var rootCommand = new RootCommand();
        foreach(var o in Options)
            rootCommand.Options.Add(o);

        var parseResult = rootCommand.Parse(args);
        if(parseResult.Errors.Count != 0) {
            foreach(var error in parseResult.Errors)
                Console.Error.WriteLine(error.Message);
            Environment.Exit(1);
        }

        var session = new HBSession(
            new Uri(parseResult.GetValue(UrlOption)!),
            parseResult.GetValue(SkipCertCheckOption));

        session.LoginAsync(
            parseResult.GetValue(UsernameOption)!,
            parseResult.GetValue(PasswordOption)!)
            .GetAwaiter()
            .GetResult();

        try {
            var media = session.Media.UploadAsync(
                parseResult.GetValue(FileOption)!.FullName, null, false).GetAwaiter().GetResult();

            Console.WriteLine(media.MediaId);
        } catch(Exception e) {
            Console.Error.WriteLine($"An error occurred: {e.Message}");
            Environment.Exit(1);
        }
    }
}