-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (95 loc) · 4.37 KB
/
Program.cs
File metadata and controls
117 lines (95 loc) · 4.37 KB
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
using Apify.Commands;
using Apify.Models;
using Apify.Services;
using NuGet.Versioning;
using System.CommandLine;
using System.Net;
using System.Reflection;
namespace Apify
{
//TODO: if variable has no dot it consider as environment variable. {{baseUrl}} same as {{env.baseUrl}}
public class Program
{
public async static Task<int> Main(string[] args)
{
var version = Assembly.GetEntryAssembly()?
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion ?? "1.0.0";
version = version.TrimStart('v');
var currentVersion = NuGetVersion.Parse(version);
await CheckForUpdate(currentVersion);
var rootCommand = new RootCommand
{
Description = "Apify - A robust and powerful CLI tool for testing APIs and a mock server."
};
rootCommand.AddGlobalOption(RootOption.DebugOption);
// Add subcommands
rootCommand.AddCommand(new CallCommand());
rootCommand.AddCommand(new AboutCommand());
rootCommand.AddCommand(new InitCommand());
rootCommand.AddCommand(new CreateRequestCommand());
rootCommand.AddCommand(new CreateMockCommand());
rootCommand.AddCommand(new TestsCommand());
rootCommand.AddCommand(new MockServerCommand());
rootCommand.AddCommand(new ListEnvsCommand());
rootCommand.AddCommand(new ImportPostmanCommand());
if (args.Length == 0)
{
// Show help if no arguments are provided
return await rootCommand.InvokeAsync(["--help"]);
}
// Parse and execute
return await rootCommand.InvokeAsync(args);
}
public async static Task<NuGetVersion?> GetLatestGitHubVersion()
{
CacheService cacheService = new CacheService();
VersionCache? cachedVersion = cacheService.GetVersionCache();
if (cachedVersion != null && cachedVersion.LastUpdated > DateTime.UtcNow.AddHours(-2))
{
return NuGetVersion.Parse(cachedVersion.LatestVersion);
}
var handler = new HttpClientHandler
{
AllowAutoRedirect = false // or false to disable
};
try
{
using var client = new HttpClient(handler);
client.Timeout = TimeSpan.FromSeconds(5); // or whatever you want
client.DefaultRequestHeaders.UserAgent.ParseAdd("Apify CLI/1.0.0");
var response = await client.GetAsync($"https://github.com/nahid/apify/releases/latest");
if (response.StatusCode != HttpStatusCode.Found && response.StatusCode != HttpStatusCode.MovedPermanently)
{
throw new Exception($"Unexpected status code: {response.StatusCode}");
}
var location = response.Headers.Location?.ToString();
if (!string.IsNullOrEmpty(location))
{
var lastSegment = location.Split('/').Last(); // e.g. v1.0.0-rc3
var tag = lastSegment.TrimStart('v'); // return 1.0.0-rc3
cacheService.UpdateVersion(tag);
return NuGetVersion.Parse(tag);
}
}
catch (Exception)
{
return null;
}
return null;
}
public async static Task CheckForUpdate(NuGetVersion currentVersion)
{
var latestVersion = await GetLatestGitHubVersion();
if (latestVersion != null && latestVersion > currentVersion)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("============================================================");
Console.WriteLine($"🚀 New version available: {latestVersion}");
Console.WriteLine("👉 Visit: https://github.com/nahid/apify/releases/latest");
Console.WriteLine("============================================================\n");
Console.ResetColor();
}
}
}
}