33// SPDX-License-Identifier: Apache-2.0
44
55use anyhow:: { bail, Result } ;
6- use move_command_line_common:: { env:: MOVE_HOME , movey:: MOVEY_URL } ;
6+ use clap:: Parser ;
7+ use move_command_line_common:: { env:: MOVE_HOME , movey_constants:: MOVEY_URL } ;
78use std:: { fs, fs:: File , io, path:: PathBuf } ;
89use toml_edit:: easy:: { map:: Map , Value } ;
910
10- pub const MOVEY_API_KEY_PATH : & str = "/movey_api_key.toml" ;
11-
12- pub struct TestMode {
13- pub test_path : String ,
14- }
15-
16- pub fn handle_movey_login_commands ( test_path : Option < String > ) -> Result < ( ) > {
17- println ! (
18- "Please paste the API Token found on {}/settings/tokens below" ,
19- MOVEY_URL
20- ) ;
21- let mut line = String :: new ( ) ;
22- loop {
23- match io:: stdin ( ) . read_line ( & mut line) {
24- Ok ( _) => {
25- if let Some ( '\n' ) = line. chars ( ) . next_back ( ) {
26- line. pop ( ) ;
11+ pub const MOVEY_CREDENTIAL_PATH : & str = "/movey_credential.toml" ;
12+
13+ #[ derive( Parser ) ]
14+ #[ clap( name = "movey-login" ) ]
15+ pub struct MoveyLogin ;
16+
17+ impl MoveyLogin {
18+ pub fn execute ( self ) -> Result < ( ) > {
19+ println ! (
20+ "Please paste the API Token found on {}/settings/tokens below" ,
21+ MOVEY_URL
22+ ) ;
23+ let mut line = String :: new ( ) ;
24+ loop {
25+ match io:: stdin ( ) . read_line ( & mut line) {
26+ Ok ( _) => {
27+ line = line. trim ( ) . to_string ( ) ;
28+ if !line. is_empty ( ) {
29+ break ;
30+ }
31+ println ! ( "Invalid API Token. Try again!" ) ;
2732 }
28- if let Some ( '\r' ) = line . chars ( ) . next_back ( ) {
29- line . pop ( ) ;
33+ Err ( err ) => {
34+ bail ! ( "Error reading file: {}" , err ) ;
3035 }
31- if !line. is_empty ( ) {
32- break ;
33- }
34- println ! ( "Invalid API Token. Try again!" ) ;
35- }
36- Err ( err) => {
37- bail ! ( "Error reading file: {}" , err) ;
3836 }
3937 }
38+ Self :: save_credential ( line, MOVE_HOME . clone ( ) ) ?;
39+ println ! ( "Token for Movey saved." ) ;
40+ Ok ( ( ) )
4041 }
41- let mut test_mode: Option < TestMode > = None ;
42- if let Some ( path) = test_path {
43- test_mode = Some ( TestMode { test_path : path } ) ;
44- }
45- save_credential ( line, test_mode) ?;
46- println ! ( "Token for Movey saved." ) ;
47- Ok ( ( ) )
48- }
4942
50- pub fn save_credential ( token : String , test_mode : Option < TestMode > ) -> Result < ( ) > {
51- let mut move_home;
52- if let Some ( test_mode) = test_mode {
53- move_home = std:: env:: var ( "TEST_MOVE_HOME" ) . unwrap ( ) ;
54- if !test_mode. test_path . is_empty ( ) {
55- move_home. push_str ( & test_mode. test_path ) ;
43+ pub fn save_credential ( token : String , move_home : String ) -> Result < ( ) > {
44+ fs:: create_dir_all ( & move_home) ?;
45+ let credential_path = move_home + MOVEY_CREDENTIAL_PATH ;
46+ let credential_file = PathBuf :: from ( & credential_path) ;
47+ if !credential_file. exists ( ) {
48+ let credential_file = File :: create ( & credential_path) ?;
49+ set_permissions ( & credential_file, 0o600 ) ?;
5650 }
57- } else {
58- move_home = MOVE_HOME . clone ( ) ;
59- }
60- fs:: create_dir_all ( & move_home) ?;
61- let credential_path = move_home + MOVEY_API_KEY_PATH ;
62- let credential_file = PathBuf :: from ( & credential_path) ;
63- if !credential_file. exists ( ) {
64- File :: create ( & credential_path) ?;
65- }
6651
67- let old_contents: String ;
68- match fs:: read_to_string ( & credential_path) {
69- Ok ( contents) => {
70- old_contents = contents;
52+ let old_contents: String ;
53+ match fs:: read_to_string ( & credential_path) {
54+ Ok ( contents) => {
55+ old_contents = contents;
56+ }
57+ Err ( error) => bail ! ( "Error reading input: {}" , error) ,
7158 }
72- Err ( error) => bail ! ( "Error reading input: {}" , error) ,
73- }
74- let mut toml: Value = old_contents
75- . parse ( )
76- . map_err ( |e| anyhow:: Error :: from ( e) . context ( "could not parse input as TOML" ) ) ?;
77-
78- if let Some ( registry) = toml. as_table_mut ( ) . unwrap ( ) . get_mut ( "registry" ) {
79- if let Some ( toml_token) = registry. as_table_mut ( ) . unwrap ( ) . get_mut ( "token" ) {
80- * toml_token = Value :: String ( token) ;
59+ let mut toml: Value = old_contents
60+ . parse ( )
61+ . map_err ( |e| anyhow:: Error :: from ( e) . context ( "could not parse input as TOML" ) ) ?;
62+
63+ // only update token key, keep the rest of the file intact
64+ if let Some ( registry) = toml. as_table_mut ( ) . unwrap ( ) . get_mut ( "registry" ) {
65+ if let Some ( toml_token) = registry. as_table_mut ( ) . unwrap ( ) . get_mut ( "token" ) {
66+ * toml_token = Value :: String ( token) ;
67+ } else {
68+ registry
69+ . as_table_mut ( )
70+ . unwrap ( )
71+ . insert ( String :: from ( "token" ) , Value :: String ( token) ) ;
72+ }
8173 } else {
82- registry
83- . as_table_mut ( )
74+ let mut value = Map :: new ( ) ;
75+ value. insert ( String :: from ( "token" ) , Value :: String ( token) ) ;
76+ toml. as_table_mut ( )
8477 . unwrap ( )
85- . insert ( String :: from ( "token " ) , Value :: String ( token ) ) ;
78+ . insert ( String :: from ( "registry " ) , Value :: Table ( value ) ) ;
8679 }
87- } else {
88- let mut value = Map :: new ( ) ;
89- value. insert ( String :: from ( "token" ) , Value :: String ( token) ) ;
90- toml. as_table_mut ( )
91- . unwrap ( )
92- . insert ( String :: from ( "registry" ) , Value :: Table ( value) ) ;
93- }
9480
95- let new_contents = toml. to_string ( ) ;
96- fs:: write ( credential_file, new_contents) . expect ( "Unable to write file" ) ;
97- let file = File :: open ( & credential_path) ?;
98- set_permissions ( & file, 0o600 ) ?;
99- Ok ( ( ) )
81+ let new_contents = toml. to_string ( ) ;
82+ fs:: write ( credential_file, new_contents) . expect ( "Unable to write file" ) ;
83+ Ok ( ( ) )
84+ }
10085}
10186
10287#[ cfg( unix) ]
@@ -123,13 +108,12 @@ mod tests {
123108 fn setup_move_home ( test_path : & str ) -> ( String , String ) {
124109 let cwd = env:: current_dir ( ) . unwrap ( ) ;
125110 let mut move_home: String = String :: from ( cwd. to_string_lossy ( ) ) ;
126- env:: set_var ( "TEST_MOVE_HOME" , & move_home) ;
127111 if !test_path. is_empty ( ) {
128112 move_home. push_str ( & test_path) ;
129113 } else {
130114 move_home. push_str ( "/test" ) ;
131115 }
132- let credential_path = move_home. clone ( ) + MOVEY_API_KEY_PATH ;
116+ let credential_path = move_home. clone ( ) + MOVEY_CREDENTIAL_PATH ;
133117 ( move_home, credential_path)
134118 }
135119
@@ -142,11 +126,7 @@ mod tests {
142126 let ( move_home, credential_path) =
143127 setup_move_home ( "/save_credential_works_if_no_credential_file_exists" ) ;
144128 let _ = fs:: remove_dir_all ( & move_home) ;
145-
146- let test_mode = Some ( TestMode {
147- test_path : String :: from ( "/save_credential_works_if_no_credential_file_exists" ) ,
148- } ) ;
149- save_credential ( String :: from ( "test_token" ) , test_mode) . unwrap ( ) ;
129+ MoveyLogin :: save_credential ( String :: from ( "test_token" ) , move_home. clone ( ) ) . unwrap ( ) ;
150130
151131 let contents = fs:: read_to_string ( & credential_path) . expect ( "Unable to read file" ) ;
152132 let mut toml: Value = contents. parse ( ) . unwrap ( ) ;
@@ -170,10 +150,7 @@ mod tests {
170150 let mut toml: Value = contents. parse ( ) . unwrap ( ) ;
171151 assert ! ( toml. as_table_mut( ) . unwrap( ) . get_mut( "registry" ) . is_none( ) ) ;
172152
173- let test_mode = Some ( TestMode {
174- test_path : String :: from ( "/save_credential_works_if_empty_credential_file_exists" ) ,
175- } ) ;
176- save_credential ( String :: from ( "test_token" ) , test_mode) . unwrap ( ) ;
153+ MoveyLogin :: save_credential ( String :: from ( "test_token" ) , move_home. clone ( ) ) . unwrap ( ) ;
177154
178155 let contents = fs:: read_to_string ( & credential_path) . expect ( "Unable to read file" ) ;
179156 let mut toml: Value = contents. parse ( ) . unwrap ( ) ;
@@ -204,10 +181,7 @@ mod tests {
204181 assert ! ( token. to_string( ) . contains( "old_test_token" ) ) ;
205182 assert ! ( !token. to_string( ) . contains( "new_world" ) ) ;
206183
207- let test_mode = Some ( TestMode {
208- test_path : String :: from ( "/save_credential_works_if_token_field_exists" ) ,
209- } ) ;
210- save_credential ( String :: from ( "new_world" ) , test_mode) . unwrap ( ) ;
184+ MoveyLogin :: save_credential ( String :: from ( "new_world" ) , move_home. clone ( ) ) . unwrap ( ) ;
211185
212186 let contents = fs:: read_to_string ( & credential_path) . expect ( "Unable to read file" ) ;
213187 let mut toml: Value = contents. parse ( ) . unwrap ( ) ;
@@ -239,10 +213,7 @@ mod tests {
239213 let token = registry. as_table_mut ( ) . unwrap ( ) . get_mut ( "token" ) . unwrap ( ) ;
240214 assert ! ( !token. to_string( ) . contains( "test_token" ) ) ;
241215
242- let test_mode = Some ( TestMode {
243- test_path : String :: from ( "/save_credential_works_if_empty_token_field_exists" ) ,
244- } ) ;
245- save_credential ( String :: from ( "test_token" ) , test_mode) . unwrap ( ) ;
216+ MoveyLogin :: save_credential ( String :: from ( "test_token" ) , move_home. clone ( ) ) . unwrap ( ) ;
246217
247218 let contents = fs:: read_to_string ( & credential_path) . expect ( "Unable to read file" ) ;
248219 let mut toml: Value = contents. parse ( ) . unwrap ( ) ;
0 commit comments