Skip to content

Commit c489108

Browse files
committed
Moved all source cmd's to the WinGetArguments class
1 parent a47ebb6 commit c489108

3 files changed

Lines changed: 283 additions & 62 deletions

File tree

src/WGet.NET/Components/WinGetSourceManager.cs

Lines changed: 104 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// Created by basicx-StrgV //
33
// https://github.com/basicx-StrgV/ //
44
//--------------------------------------------------//
5+
using System.Collections.Generic;
56
using System.Threading;
67
using System.Threading.Tasks;
7-
using System.Collections.Generic;
8-
using WGetNET.Models;
9-
using WGetNET.Helper;
108
using WGetNET.Components.Internal;
9+
using WGetNET.Helper;
10+
using WGetNET.Models;
1111

1212
namespace WGetNET
1313
{
@@ -16,13 +16,6 @@ namespace WGetNET
1616
/// </summary>
1717
public class WinGetSourceManager : WinGet
1818
{
19-
private const string _sourceAddCmd = "source add -n {0} -a {1} --accept-source-agreements";
20-
private const string _sourceAddWithTypeCmd = "source add -n {0} -a {1} -t {2} --accept-source-agreements";
21-
private const string _sourceUpdateCmd = "source update";
22-
private const string _sourceExportCmd = "source export";
23-
private const string _sourceResetCmd = "source reset --force";
24-
private const string _sourceRemoveCmd = "source remove -n {0}";
25-
2619
/// <summary>
2720
/// Initializes a new instance of the <see cref="WGetNET.WinGetSourceManager"/> class.
2821
/// </summary>
@@ -43,7 +36,11 @@ public WinGetSourceManager()
4336
/// </exception>
4437
public List<WinGetSource> GetInstalledSources()
4538
{
46-
ProcessResult result = Execute(_sourceExportCmd);
39+
ProcessResult result =
40+
Execute(
41+
WinGetArguments
42+
.SourceExport()
43+
.ToString());
4744

4845
return ProcessOutputReader.ToSourceList(result.Output);
4946
}
@@ -68,9 +65,12 @@ public List<WinGetSource> GetInstalledSources(string sourceName)
6865
{
6966
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
7067

71-
string cmd = $"{_sourceExportCmd} -n {sourceName}";
72-
73-
ProcessResult result = Execute(cmd);
68+
ProcessResult result =
69+
Execute(
70+
WinGetArguments
71+
.SourceExport()
72+
.Name(sourceName)
73+
.ToString());
7474

7575
return ProcessOutputReader.ToSourceList(result.Output);
7676
}
@@ -90,7 +90,12 @@ public List<WinGetSource> GetInstalledSources(string sourceName)
9090
/// </exception>
9191
public async Task<List<WinGetSource>> GetInstalledSourcesAsync(CancellationToken cancellationToken = default)
9292
{
93-
ProcessResult result = await ExecuteAsync(_sourceExportCmd, false, cancellationToken);
93+
ProcessResult result =
94+
await ExecuteAsync(
95+
WinGetArguments
96+
.SourceExport()
97+
.ToString(),
98+
false, cancellationToken);
9499

95100
// Return empty list if the task was cancled
96101
if (cancellationToken.IsCancellationRequested)
@@ -125,9 +130,13 @@ public async Task<List<WinGetSource>> GetInstalledSourcesAsync(string sourceName
125130
{
126131
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
127132

128-
string cmd = $"{_sourceExportCmd} -n {sourceName}";
129-
130-
ProcessResult result = await ExecuteAsync(cmd, false, cancellationToken);
133+
ProcessResult result =
134+
await ExecuteAsync(
135+
WinGetArguments
136+
.SourceExport()
137+
.Name(sourceName)
138+
.ToString(),
139+
false, cancellationToken);
131140

132141
// Return empty list if the task was cancled
133142
if (cancellationToken.IsCancellationRequested)
@@ -169,9 +178,15 @@ public bool AddSource(string name, string arg)
169178
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
170179
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
171180

172-
string cmd = string.Format(_sourceAddCmd, name, arg);
173-
174-
ProcessResult result = Execute(cmd, true);
181+
ProcessResult result =
182+
Execute(
183+
WinGetArguments
184+
.SourceAdd()
185+
.Name(name)
186+
.Arg(arg)
187+
.AcceptSourceAgreements()
188+
.ToString(),
189+
true);
175190

176191
return result.Success;
177192
}
@@ -209,9 +224,16 @@ public bool AddSource(string name, string arg, string type)
209224
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
210225
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(type, "type");
211226

212-
string cmd = string.Format(_sourceAddWithTypeCmd, name, arg, type);
213-
214-
ProcessResult result = Execute(cmd, true);
227+
ProcessResult result =
228+
Execute(
229+
WinGetArguments
230+
.SourceAdd()
231+
.Name(name)
232+
.Arg(arg)
233+
.Type(type)
234+
.AcceptSourceAgreements()
235+
.ToString(),
236+
true);
215237

216238
return result.Success;
217239
}
@@ -316,9 +338,15 @@ public async Task<bool> AddSourceAsync(string name, string arg, CancellationToke
316338
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
317339
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
318340

319-
string cmd = string.Format(_sourceAddCmd, name, arg);
320-
321-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
341+
ProcessResult result =
342+
await ExecuteAsync(
343+
WinGetArguments
344+
.SourceAdd()
345+
.Name(name)
346+
.Arg(arg)
347+
.AcceptSourceAgreements()
348+
.ToString(),
349+
true, cancellationToken);
322350

323351
return result.Success;
324352
}
@@ -360,9 +388,16 @@ public async Task<bool> AddSourceAsync(string name, string arg, string type, Can
360388
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
361389
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(type, "type");
362390

363-
string cmd = string.Format(_sourceAddWithTypeCmd, name, arg, type);
364-
365-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
391+
ProcessResult result =
392+
await ExecuteAsync(
393+
WinGetArguments
394+
.SourceAdd()
395+
.Name(name)
396+
.Arg(arg)
397+
.Type(type)
398+
.AcceptSourceAgreements()
399+
.ToString(),
400+
true, cancellationToken);
366401

367402
return result.Success;
368403
}
@@ -466,7 +501,11 @@ public async Task<bool> AddSourceAsync(IEnumerable<WinGetSource> sources, Cancel
466501
/// </exception>
467502
public bool UpdateSources()
468503
{
469-
ProcessResult result = Execute(_sourceUpdateCmd);
504+
ProcessResult result =
505+
Execute(
506+
WinGetArguments
507+
.SourceUpdate()
508+
.ToString());
470509

471510
return result.Success;
472511
}
@@ -489,7 +528,12 @@ public bool UpdateSources()
489528
/// </exception>
490529
public async Task<bool> UpdateSourcesAsync(CancellationToken cancellationToken = default)
491530
{
492-
ProcessResult result = await ExecuteAsync(_sourceUpdateCmd, false, cancellationToken);
531+
ProcessResult result =
532+
await ExecuteAsync(
533+
WinGetArguments
534+
.SourceUpdate()
535+
.ToString(),
536+
false, cancellationToken);
493537

494538
return result.Success;
495539
}
@@ -925,7 +969,13 @@ public async Task<bool> ImportSourcesFromJsonAsync(string jsonString, Cancellati
925969
/// </exception>
926970
public bool ResetSources()
927971
{
928-
ProcessResult result = Execute(_sourceResetCmd, true);
972+
ProcessResult result =
973+
Execute(
974+
WinGetArguments
975+
.SourceReset()
976+
.Force()
977+
.ToString(),
978+
true);
929979

930980
return result.Success;
931981
}
@@ -951,7 +1001,13 @@ public bool ResetSources()
9511001
/// </exception>
9521002
public async Task<bool> ResetSourcesAsync(CancellationToken cancellationToken = default)
9531003
{
954-
ProcessResult result = await ExecuteAsync(_sourceResetCmd, true, cancellationToken);
1004+
ProcessResult result =
1005+
await ExecuteAsync(
1006+
WinGetArguments
1007+
.SourceReset()
1008+
.Force()
1009+
.ToString(),
1010+
true, cancellationToken);
9551011

9561012
return result.Success;
9571013
}
@@ -983,9 +1039,13 @@ public bool RemoveSources(string name)
9831039
{
9841040
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
9851041

986-
string cmd = string.Format(_sourceRemoveCmd, name);
987-
988-
ProcessResult result = Execute(cmd, true);
1042+
ProcessResult result =
1043+
Execute(
1044+
WinGetArguments
1045+
.SourceRemove()
1046+
.Name(name)
1047+
.ToString(),
1048+
true);
9891049

9901050
return result.Success;
9911051
}
@@ -1047,9 +1107,13 @@ public async Task<bool> RemoveSourcesAsync(string name, CancellationToken cancel
10471107
{
10481108
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
10491109

1050-
string cmd = string.Format(_sourceRemoveCmd, name);
1051-
1052-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
1110+
ProcessResult result =
1111+
await ExecuteAsync(
1112+
WinGetArguments
1113+
.SourceRemove()
1114+
.Name(name)
1115+
.ToString(),
1116+
true, cancellationToken);
10531117

10541118
return result.Success;
10551119
}

src/WGet.NET/Data/WinGetArguments.cs

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ internal enum WinGetAction
2727
PinList,
2828
PinAdd,
2929
PinRemove,
30-
PinReset
30+
PinReset,
31+
SourceAdd,
32+
SourceRemove,
33+
SourceUpdate,
34+
SourceReset,
35+
SourceExport
3136
}
3237

3338
/// <summary>
@@ -258,6 +263,61 @@ public static WinGetArguments PinReset()
258263
return new WinGetArguments("pin reset", WinGetAction.PinReset);
259264
}
260265

266+
/// <summary>
267+
/// Creates a new winget arguments object with "source add" as the base cmd.
268+
/// </summary>
269+
/// <returns>
270+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
271+
/// </returns>
272+
public static WinGetArguments SourceAdd()
273+
{
274+
return new WinGetArguments("source add", WinGetAction.SourceAdd);
275+
}
276+
277+
/// <summary>
278+
/// Creates a new winget arguments object with "source remove" as the base cmd.
279+
/// </summary>
280+
/// <returns>
281+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
282+
/// </returns>
283+
public static WinGetArguments SourceRemove()
284+
{
285+
return new WinGetArguments("source remove", WinGetAction.SourceRemove);
286+
}
287+
288+
/// <summary>
289+
/// Creates a new winget arguments object with "source update" as the base cmd.
290+
/// </summary>
291+
/// <returns>
292+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
293+
/// </returns>
294+
public static WinGetArguments SourceUpdate()
295+
{
296+
return new WinGetArguments("source update", WinGetAction.SourceUpdate);
297+
}
298+
299+
/// <summary>
300+
/// Creates a new winget arguments object with "source reset" as the base cmd.
301+
/// </summary>
302+
/// <returns>
303+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
304+
/// </returns>
305+
public static WinGetArguments SourceReset()
306+
{
307+
return new WinGetArguments("source reset", WinGetAction.SourceReset);
308+
}
309+
310+
/// <summary>
311+
/// Creates a new winget arguments object with "source export" as the base cmd.
312+
/// </summary>
313+
/// <returns>
314+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
315+
/// </returns>
316+
public static WinGetArguments SourceExport()
317+
{
318+
return new WinGetArguments("source export", WinGetAction.SourceExport);
319+
}
320+
261321
//---Flags-------------------------------------------------------------------------------------
262322
/// <summary>
263323
/// Adds a query to the arguments.
@@ -388,6 +448,51 @@ public WinGetArguments Disable(string query)
388448
return this;
389449
}
390450

451+
/// <summary>
452+
/// Adds name data to the arguments.
453+
/// </summary>
454+
/// <param name="name">
455+
/// A <see cref="System.String"/> containing the name to add to the arguments.
456+
/// </param>
457+
/// <returns>
458+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
459+
/// </returns>
460+
public WinGetArguments Name(string name)
461+
{
462+
_arguments += $" --name \"{name}\"";
463+
return this;
464+
}
465+
466+
/// <summary>
467+
/// Adds arg (Source argument) data to the arguments.
468+
/// </summary>
469+
/// <param name="arg">
470+
/// A <see cref="System.String"/> containing the arg (Source argument) to add to the arguments.
471+
/// </param>
472+
/// <returns>
473+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
474+
/// </returns>
475+
public WinGetArguments Arg(string arg)
476+
{
477+
_arguments += $" --arg \"{arg}\"";
478+
return this;
479+
}
480+
481+
/// <summary>
482+
/// Adds type data to the arguments.
483+
/// </summary>
484+
/// <param name="type">
485+
/// A <see cref="System.String"/> containing the type data to add to the arguments.
486+
/// </param>
487+
/// <returns>
488+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
489+
/// </returns>
490+
public WinGetArguments Type(string type)
491+
{
492+
_arguments += $" --type \"{type}\"";
493+
return this;
494+
}
495+
391496
/// <summary>
392497
/// Adds a version query to the arguments.
393498
/// </summary>

0 commit comments

Comments
 (0)