Skip to content

Commit 9ef978d

Browse files
committed
logging + export/import feature
1 parent 82a7cdc commit 9ef978d

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/Generator.Client.CommandLine/ConsoleApplication.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ public async Task<int> Rename(
106106
}
107107
}
108108

109+
[ApplicationMetadata(Description = "Saves the local configuration to the specified location.")]
110+
public async Task<int> Export(
111+
[Argument(
112+
Description = "export path")]
113+
string newPath)
114+
{
115+
if (!ConsoleUtils.TryGetManager(null, out var manager))
116+
{
117+
Log.Error($"Failed to obtain local storage manager.");
118+
return 1;
119+
}
120+
121+
try
122+
{
123+
var configurations = await manager.LoadStorageContentAsync();
124+
if (!await manager.SaveConfigurationsAsync(configurations, newPath))
125+
{
126+
Log.Error($"Failed to save configurations to path [{newPath}].");
127+
return 3;
128+
}
129+
130+
return 0;
131+
}
132+
catch (Exception e)
133+
{
134+
Log.Error(e.Message);
135+
return 2;
136+
}
137+
}
138+
109139
[SubCommand]
110140
[ApplicationMetadata(Description = "Get actions.")]
111141
public class Get
@@ -163,6 +193,39 @@ public async Task<int> Json(
163193
}
164194
}
165195
}
196+
197+
[SubCommand]
198+
[ApplicationMetadata(Description = "Import actions.")]
199+
public class Import
200+
{
201+
[ApplicationMetadata(Description = "Updates the local storage from a given path.")]
202+
public async Task<int> FromFile(
203+
[Argument(
204+
Description = "Path to storage file.")]
205+
string storagePath)
206+
{
207+
if (!ConsoleUtils.TryGetManager(null, out var currentManager))
208+
{
209+
Log.Error("Failed to obtain local manager instance.");
210+
return 1;
211+
}
212+
213+
if (!ConsoleUtils.TryGetManager(storagePath, out var remoteManager))
214+
{
215+
Log.Error($"Failed to obtain remote manager instance using path [{storagePath}].");
216+
return 2;
217+
}
218+
219+
var remoteContents = await remoteManager.LoadStorageContentAsync();
220+
if (!await currentManager.SaveConfigurationsAsync(remoteContents))
221+
{
222+
Log.Error($"Failed to update local manager instance.");
223+
return 3;
224+
}
225+
226+
return 0;
227+
}
228+
}
166229
}
167230
}
168231
}

src/Generator.Client.CommandLine/ConsoleUtils.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System;
1+
// Copyright 2020 Andreas Müller
2+
// This file is a part of Amusoft.VisualStudio.TemplateGenerator and is licensed under Apache 2.0
3+
// See https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator/blob/master/LICENSE for details
4+
5+
using System;
6+
using System.IO;
27
using Generator.Shared.Template;
38
using NLog;
49

@@ -12,7 +17,16 @@ public static bool TryGetManager(string storage, out ConfigurationManager config
1217
{
1318
try
1419
{
15-
configurationManager = storage == null ? ConfigurationManager.Default() : ConfigurationManager.FromPath(storage);
20+
if (!string.IsNullOrEmpty(storage) && !File.Exists(storage))
21+
{
22+
Log.Error($"File [{storage}] does not exist.");
23+
configurationManager = null;
24+
return false;
25+
}
26+
27+
configurationManager = storage == null
28+
? ConfigurationManager.Default()
29+
: ConfigurationManager.FromPath(storage);
1630
return true;
1731
}
1832
catch (Exception e)

0 commit comments

Comments
 (0)