Skip to content

Commit d62d222

Browse files
committed
upload folder
1 parent 02a7280 commit d62d222

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+115
-7788
lines changed

Deployf.Cli.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5+
<AssemblyName>df</AssemblyName>
56
<TargetFramework>net6.0</TargetFramework>
67
<ImplicitUsings>enable</ImplicitUsings>
78

@@ -10,12 +11,12 @@
1011
<PackageOutputPath>./nupkg</PackageOutputPath>
1112

1213
<NoWarn>NU1701</NoWarn>
14+
15+
<Version>0.1.0</Version>
1316
</PropertyGroup>
1417

1518
<ItemGroup>
1619
<PackageReference Include="CliFx" Version="2.2.2" />
17-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
18-
<PackageReference Include="RestSharp.Net2" version="1.1.11" />
1920
</ItemGroup>
2021

2122
</Project>

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Deployf Cli
2+
3+
4+
## how to build an exe
5+
```
6+
dotnet publish -c Release -r win-x64 -o publish -p:PublishSingleFile=true --self-contained -p:SelfContained=true -p:PublishTrimmed=true
7+
```

SetAppIdCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using CliFx;
22
using CliFx.Attributes;
33
using CliFx.Infrastructure;
4-
using IO.Swagger.Api;
54

65
[Command("set-app-id", Description = "set context to use passed app id")]
76
public class SetAppIdCommand : ApplicationBaseCommand, ICommand

UploadFileCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using CliFx;
22
using CliFx.Attributes;
33
using CliFx.Infrastructure;
4-
using System.Net.Http.Headers;
54

65
[Command("upload", Description = "uplaoad file to the container")]
76
public class UploadFileCommand : ApplicationBaseCommand, ICommand

UploadFolderCommand.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using CliFx;
2+
using CliFx.Attributes;
3+
using CliFx.Exceptions;
4+
using CliFx.Infrastructure;
5+
using System.Diagnostics;
6+
using System.IO.Compression;
7+
8+
[Command("upload-folder", Description = "uplaoad folder to the container")]
9+
public class UploadFolderCommand : ApplicationBaseCommand, ICommand
10+
{
11+
[CommandParameter(0)]
12+
public string SourceFolder { get; set; }
13+
14+
[CommandParameter(1)]
15+
public string AppPath { get; set; }
16+
17+
public async ValueTask ExecuteAsync(IConsole console)
18+
{
19+
var absolutePath = Path.GetFullPath(SourceFolder);
20+
if(!Directory.Exists(absolutePath))
21+
{
22+
throw new CommandException("Directory does not exist");
23+
}
24+
25+
using var tempPath = new TempFSEntry(false);
26+
27+
ZipFile.CreateFromDirectory(SourceFolder, tempPath, CompressionLevel.SmallestSize, false);
28+
29+
using var zip = File.OpenRead(tempPath);
30+
31+
var request = CreateRequest(HttpMethod.Put, $"api/application/{AppId}/exec");
32+
var content = new MultipartFormDataContent();
33+
request.Content = content;
34+
content.Add(new StringContent("bsdtar"), "command");
35+
content.Add(new StringContent("-xf"), "command");
36+
content.Add(new StringContent("-"), "command");
37+
content.Add(new StringContent("-C"), "command");
38+
content.Add(new StringContent(AppPath), "command");
39+
content.Add(new StreamContent(zip), "file", "stdin");
40+
41+
var response = await new HttpClient().SendAsync(request);
42+
43+
response.EnsureSuccessStatusCode();
44+
45+
await response.Content.ReadAsStream().CopyToAsync(console.Output.BaseStream);
46+
}
47+
}
48+
49+
public class TempFSEntry : IDisposable
50+
{
51+
public readonly string EntryPath;
52+
53+
private bool _destroyed;
54+
private bool _isFolder;
55+
56+
public TempFSEntry(bool isFolder = true)
57+
{
58+
var basePath = Path.Combine(Path.GetTempPath(), "deployf");
59+
60+
if(!Directory.Exists(basePath))
61+
{
62+
Directory.CreateDirectory(basePath);
63+
}
64+
65+
EntryPath = Path.Combine(basePath, Path.GetRandomFileName());
66+
_isFolder = isFolder;
67+
if (isFolder)
68+
{
69+
Directory.CreateDirectory(EntryPath);
70+
}
71+
Debug.WriteLine($"Allocate temp fs entry at {EntryPath}");
72+
}
73+
74+
~TempFSEntry()
75+
{
76+
Dispose();
77+
}
78+
79+
public void Dispose()
80+
{
81+
if (_destroyed)
82+
{
83+
return;
84+
}
85+
86+
_destroyed = true;
87+
88+
if (_isFolder && Directory.Exists(EntryPath))
89+
{
90+
Debug.WriteLine($"Dispose temp folder at {EntryPath}");
91+
Directory.Delete(EntryPath, true);
92+
}
93+
94+
if (!_isFolder && File.Exists(EntryPath))
95+
{
96+
Debug.WriteLine($"Dispose temp file at {EntryPath}");
97+
File.Delete(EntryPath);
98+
}
99+
}
100+
101+
public static implicit operator string(TempFSEntry instance)
102+
{
103+
return instance.EntryPath;
104+
}
105+
}

0 commit comments

Comments
 (0)