-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
143 lines (124 loc) · 4.04 KB
/
Program.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace CalcEngineTutorialSetup
{
class Program
{
private static List<IFixture> fixtures;
static void Main(string[] args)
{
parseArgs(args);
if (Context.Password == null)
{
getPassword();
}
using (DBConnection dbConnection = new DBConnection(Context.Hostname, Context.Username, Context.Password))
{
dbConnection.ConnectOrThrow();
Context.Driver = dbConnection.RTDBDriver;
Console.WriteLine("Connection successful");
fixtures = new List<IFixture>()
{
new VariablesFixture(),
new EquipmentModelFixture(),
};
if (Context.Cleanup)
{
Cleanup();
Console.WriteLine("Cleanup successful");
}
else
{
Setup();
Console.WriteLine("Setup successful");
}
}
}
static private void Setup()
{
foreach (var fixture in fixtures)
{
fixture.Setup();
}
}
static private void Cleanup()
{
foreach (var fixture in fixtures)
{
fixture.Cleanup();
}
}
static private void parseArgs(string[] args)
{
if (args[0] == "cleanup")
{
Context.Cleanup = true;
}
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "-h" && i < args.Length - 1)
{
Context.Hostname = args[i + 1];
}
if (args[i] == "-u" && i < args.Length - 1)
{
Context.Username = args[i + 1];
}
if (args[i] == "-p" && i < args.Length - 1)
{
Context.Password = new NetworkCredential("", args[i + 1]).SecurePassword;
}
if (args[i] == "-s" && i < args.Length - 1)
{
Context.NumberOfSites = uint.Parse(args[i + 1]);
}
if (args[i] == "-g" && i < args.Length - 1)
{
Context.Group = args[i + 1];
}
}
if (Context.Hostname == null)
{
throw new System.ArgumentException("Define hostname using -h <hostname>");
}
if (Context.Username == null)
{
throw new System.ArgumentException("Define username using -u <username>");
}
}
static private void getPassword()
{
Console.Write($"Password for {Context.Username}@{Context.Hostname}: ");
// From https://stackoverflow.com/a/3404522/2876520
var password = new SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.RemoveAt(password.Length - 1);
Console.Write("\b \b");
}
}
else if (i.KeyChar != '\u0000') // KeyChar == '\u0000' if the key pressed does not correspond to a printable character, e.g. F1, Pause-Break, etc
{
password.AppendChar(i.KeyChar);
Console.Write("*");
}
}
Context.Password = password;
}
}
}