-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProgram.cs
106 lines (89 loc) · 3.7 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
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
using GoogleSheetsWrapper.SampleClient.SampleModel;
using CsvHelper.Configuration;
using System.Globalization;
namespace GoogleSheetsWrapper.SampleClient
{
public class Program
{
public static void Main()
{
var config = BuildConfig();
// Get the Google Spreadsheet Config Values
var serviceAccount = config["GOOGLE_SERVICE_ACCOUNT"];
var documentId = config["GOOGLE_SPREADSHEET_ID"];
var jsonCredsPath = config["GOOGLE_JSON_CREDS_PATH"];
// In this case the json creds file is stored locally, but you can store this however you want to (Azure Key Vault, HSM, etc)
var jsonCredsContent = File.ReadAllText(jsonCredsPath);
// Create a new SheetHelper class
var sheetHelper = new SheetHelper<SampleRecord>(documentId, serviceAccount, "");
sheetHelper.Init(jsonCredsContent);
var csvConfig = new CsvConfiguration(CultureInfo.CurrentCulture)
{
Delimiter = ";",
};
var repoConfig = new BaseRepositoryConfiguration()
{
// Does the table have a header row?
HasHeaderRow = true,
// Are there any blank rows before the header row starts?
HeaderRowOffset = 0,
// Are there any blank rows before the first row in the data table starts?
DataTableRowOffset = 0,
};
var respository = new SampleRepository(sheetHelper, repoConfig);
var records = respository.GetAllRecords();
foreach (var record in records)
{
try
{
Foo(record.TaskName);
record.Result = true;
record.DateExecuted = DateTime.UtcNow;
var result = respository.SaveFields(
record,
r => r.Result,
r => r.DateExecuted);
}
catch (Exception ex)
{
record.Result = false;
record.ErrorMessage = ex.Message;
record.DateExecuted = DateTime.UtcNow;
var result = respository.SaveFields(
record,
r => r.Result,
r => r.ErrorMessage,
r => r.DateExecuted);
}
}
}
private static void Foo(string name)
{
// Do some operation based on the record
Console.WriteLine(name);
}
/// <summary>
/// Really simple method to build the config locally. This requires you to setup User Secrets locally with Visual Studio
/// </summary>
/// <returns></returns>
private static IConfigurationRoot BuildConfig()
{
var devEnvironmentVariable = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT");
var isDevelopment = string.IsNullOrEmpty(devEnvironmentVariable) ||
devEnvironmentVariable.Equals("development", StringComparison.OrdinalIgnoreCase);
var builder = new ConfigurationBuilder();
// tell the builder to look for the appsettings.json file
_ = builder
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
//only add secrets in development
if (isDevelopment)
{
_ = builder.AddUserSecrets<Program>();
}
return builder.Build();
}
}
}