1
- use colored:: Colorize ;
2
1
use spinners:: Spinner ;
3
- use std:: io:: prelude:: * ;
4
- use std:: io:: { Seek , Write } ;
5
2
use std:: iter:: Iterator ;
6
- use zip:: result:: ZipError ;
7
- use zip:: write:: FileOptions ;
8
-
9
- use std:: fs:: File ;
10
- use std:: path:: { Path , PathBuf } ;
11
- use walkdir:: { DirEntry , WalkDir } ;
12
- #[ tracing:: instrument]
13
- fn get_zip_file_path ( ) -> PathBuf {
14
- let mut dst_file = std:: env:: temp_dir ( ) ;
15
- dst_file. push ( "discloud.zip" ) ;
16
- dst_file
17
- }
3
+ use crate :: zip_directory:: commit as commit_zip;
4
+ use crate :: zip_directory:: { get_zip_file_path, zip_dir_to_file} ;
18
5
#[ tracing:: instrument]
19
6
pub fn commit ( teams : bool ) {
20
7
let token = super :: expect_token ( ) ;
@@ -28,123 +15,14 @@ pub fn commit(teams: bool) {
28
15
29
16
let src_dir = "." ;
30
17
let dst_file = get_zip_file_path ( ) ;
31
- match zip_dir_to_file ( src_dir, dst_file. to_str ( ) . unwrap ( ) , METHOD_DEFLATED ) {
18
+ match zip_dir_to_file ( src_dir, dst_file. to_str ( ) . unwrap ( ) ) {
32
19
Ok ( _) => { }
33
20
Err ( e) => super :: err ( & format ! ( "Failed to zip: {:?}" , e) ) ,
34
21
}
35
22
let mut spinner = Spinner :: new ( spinners:: Spinners :: Earth , "Committing app..." . to_string ( ) ) ;
36
- let msg = match upload_zip ( token, app_id, teams) {
23
+ let msg = match commit_zip ( token, app_id, teams) {
37
24
Ok ( ( ) ) => if !teams { super :: format_log ( "Your app was updated successfully!" ) } else { super :: format_log ( "Your buddy's app was updated!" ) } ,
38
25
Err ( err) => super :: format_err ( & err) ,
39
26
} ;
40
27
spinner. stop_with_message ( msg) ;
41
28
}
42
-
43
- const METHOD_DEFLATED : zip:: CompressionMethod = zip:: CompressionMethod :: Deflated ;
44
-
45
- fn zip_dir < T > (
46
- it : & mut dyn Iterator < Item = DirEntry > ,
47
- prefix : & str ,
48
- writer : T ,
49
- method : zip:: CompressionMethod ,
50
- ) -> zip:: result:: ZipResult < ( ) >
51
- where
52
- T : Write + Seek ,
53
- {
54
- let mut zip = zip:: ZipWriter :: new ( writer) ;
55
- let options = FileOptions :: default ( )
56
- . compression_method ( method)
57
- . unix_permissions ( 0o755 ) ;
58
-
59
- let mut buffer = Vec :: new ( ) ;
60
- for entry in it {
61
- let path = entry. path ( ) ;
62
- let name = path. strip_prefix ( Path :: new ( prefix) ) . unwrap ( ) ;
63
- if path. is_file ( ) {
64
- print ! ( "⌛ Zipping file: {}\r " , name. to_str( ) . unwrap( ) ) ;
65
- zip. start_file ( name. to_str ( ) . unwrap ( ) , options) ?;
66
- let mut f = File :: open ( path) ?;
67
-
68
- f. read_to_end ( & mut buffer) ?;
69
- zip. write_all ( & buffer) ?;
70
- buffer. clear ( ) ;
71
- println ! ( "{}" , "✔" . green( ) . bold( ) ) ;
72
- } else if !name. as_os_str ( ) . is_empty ( ) {
73
- zip. add_directory ( name. to_str ( ) . unwrap ( ) , options) ?;
74
- }
75
- }
76
- zip. finish ( ) ?;
77
- Result :: Ok ( ( ) )
78
- }
79
-
80
- #[ tracing:: instrument]
81
- fn zip_dir_to_file (
82
- src_dir : & str ,
83
- dst_file : & str ,
84
- method : zip:: CompressionMethod ,
85
- ) -> zip:: result:: ZipResult < ( ) > {
86
- if !Path :: new ( src_dir) . is_dir ( ) {
87
- return Err ( ZipError :: FileNotFound ) ;
88
- }
89
- let writer = File :: create ( dst_file) . unwrap ( ) ;
90
-
91
- let walkdir = WalkDir :: new ( src_dir) ;
92
- let it = walkdir. into_iter ( ) ;
93
-
94
- zip_dir (
95
- & mut it. filter_map ( |e| {
96
- if let Ok ( e) = e {
97
- let components = e. path ( ) . components ( ) . collect :: < Vec < _ > > ( ) ;
98
- if components. len ( ) < 2 {
99
- Some ( e)
100
- } else {
101
- match components[ 1 ] . as_os_str ( ) . to_str ( ) . unwrap ( ) {
102
- "target" | ".git" | "build" | "out" | "node_modules" | ".gitignore" => None ,
103
- _ => Some ( e) ,
104
- }
105
- }
106
- } else {
107
- None
108
- }
109
- } ) ,
110
- src_dir,
111
- writer,
112
- method,
113
- ) ?;
114
-
115
- Ok ( ( ) )
116
- }
117
- #[ tracing:: instrument]
118
- fn upload_zip ( token : String , app_id : String , teams : bool ) -> Result < ( ) , String > {
119
- let file_path = get_zip_file_path ( ) ;
120
- let file_path = file_path. to_str ( ) . unwrap ( ) ;
121
- let client = reqwest:: blocking:: Client :: builder ( )
122
- . timeout ( None )
123
- . build ( )
124
- . unwrap ( ) ;
125
- let form = reqwest:: blocking:: multipart:: Form :: new ( ) . file ( "file" , file_path) ;
126
- match form {
127
- Err ( err) => Err ( format ! ( "Couldn't open zip file: {}" , err) ) ,
128
- Ok ( form) => {
129
- let req = client
130
- . put ( crate :: api_url!( format!( "/{}/{}/commit" , if teams { "team" } else { "app" } , app_id) ) )
131
- . multipart ( form)
132
- . header ( "api-token" , token) ;
133
- let res = req. send ( ) ;
134
- match res {
135
- Err ( err) => Err ( err. to_string ( ) ) ,
136
- Ok ( res) => {
137
- if res. status ( ) . is_success ( ) {
138
- Ok ( ( ) )
139
- } else {
140
- Err ( format ! (
141
- "Commit failed: API returned {} http code: {}" ,
142
- res. status( ) . as_u16( ) ,
143
- res. text( ) . unwrap( )
144
- ) )
145
- }
146
- }
147
- }
148
- }
149
- }
150
- }
0 commit comments