summaryrefslogtreecommitdiff
path: root/writer/Program.cs
blob: ec8706b88bc811324cc36b2f8c9f019e459130ab (plain) (blame)
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
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System;
using System.IO;

namespace writer
{

    class Program
    {
        static void Send(JToken settingsToken)
        {
            var settings = settingsToken.ToObject<DriverSettings>();

            var errors = DriverInterop.GetSettingsErrors(settings);
            if (errors.Empty())
            {
                DriverInterop.Write(settings);
                return;
            }

            Console.Write("Bad settings: \n\n{0}", errors);
        }

        static void Main(string[] args)
        {
            try
            {
                VersionHelper.ValidateAndGetDriverVersion(typeof(Program).Assembly.GetName().Version);
            }
            catch (VersionException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            if (args.Length != 1 || args[0].Equals("help"))
            {
                Console.WriteLine("USAGE: {0} <file>", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("Settings file not found at {0}", args[0]);
                return;
            }

            try
            {
                var JO = JObject.Parse(File.ReadAllText(args[0]));

                if (JO.ContainsKey(DriverSettings.Key))
                {
                    Send(JO[DriverSettings.Key]);
                    return;
                }

                Send(JO);
            }
            catch (JsonException e)
            {
                Console.WriteLine("Settings invalid:\n{0}", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }
        }
    }
}