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