indodax-cli 0.1.4

A command-line interface for the Indodax cryptocurrency exchange
Documentation
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SecretValue(String);

impl SecretValue {
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl fmt::Display for SecretValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0.is_empty() {
            write!(f, "")
        } else {
            write!(f, "********")
        }
    }
}

impl From<String> for SecretValue {
    fn from(s: String) -> Self {
        SecretValue(s)
    }
}

impl From<&str> for SecretValue {
    fn from(s: &str) -> Self {
        SecretValue(s.to_string())
    }
}


#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IndodaxConfig {
    pub api_key: Option<SecretValue>,
    pub api_secret: Option<SecretValue>,
    pub callback_url: Option<String>,
    pub paper_balances: Option<serde_json::Value>,
}


#[derive(Debug, Clone)]
pub struct ResolvedCredentials {
    pub api_key: SecretValue,
    pub api_secret: SecretValue,
}

impl IndodaxConfig {
    fn get_base_dir() -> PathBuf {
        match dirs::config_dir() {
            Some(dir) => dir,
            None => {
                let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
                cwd
            }
        }
    }

    pub fn config_path() -> PathBuf {
        Self::get_base_dir().join("indodax").join("config.toml")
    }

    pub fn config_dir() -> PathBuf {
        Self::get_base_dir().join("indodax")
    }

    pub fn load() -> Result<Self, anyhow::Error> {
        let path = Self::config_path();
        if !path.exists() {
            return Ok(Self::default());
        }
        let content = fs::read_to_string(&path)?;
        let config: IndodaxConfig = toml::from_str(&content)?;
        Ok(config)
    }

    pub fn save(&self) -> Result<(), anyhow::Error> {
        let dir = Self::config_dir();
        if dirs::config_dir().is_none() {
            eprintln!(
                "Warning: Could not determine user config directory. Falling back to current directory: {}",
                dir.parent().unwrap_or(&dir).display()
            );
        }
        fs::create_dir_all(&dir)?;
        let path = Self::config_path();
        let content = toml::to_string_pretty(self)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            let mut file = std::fs::OpenOptions::new()
                .write(true)
                .create(true)
                .truncate(true)
                .mode(0o600)
                .open(&path)?;
            use std::io::Write;
            file.write_all(content.as_bytes())?;
        }
        #[cfg(not(unix))]
        {
            fs::write(&path, content)?;
        }
        Ok(())
    }

    pub fn resolve_credentials(
        &self,
        cli_key: Option<String>,
        cli_secret: Option<String>,
    ) -> Result<Option<ResolvedCredentials>, anyhow::Error> {
        let api_key = if let Some(ref key) = cli_key {
            let trimmed = key.trim();
            if trimmed.is_empty() {
                None
            } else {
                Some(SecretValue::new(trimmed.to_string()))
            }
        } else {
            std::env::var("INDODAX_API_KEY")
                .ok()
                .map(|k| k.trim().to_string())
                .filter(|k| !k.is_empty())
                .map(SecretValue::new)
                .or_else(|| self.api_key.clone())
        };

        let api_secret = if let Some(ref secret) = cli_secret {
            let trimmed = secret.trim();
            if trimmed.is_empty() {
                None
            } else {
                Some(SecretValue::new(trimmed.to_string()))
            }
        } else {
            std::env::var("INDODAX_API_SECRET")
                .ok()
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .map(SecretValue::new)
                .or_else(|| self.api_secret.clone())
        };

        match (api_key, api_secret) {
            (Some(key), Some(secret)) => Ok(Some(ResolvedCredentials {
                api_key: key,
                api_secret: secret,
            })),
        _ => Ok(None),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use serial_test::serial;

    #[test]
    fn test_secret_value_new() {
        let sv = SecretValue::new("test_secret");
        assert_eq!(sv.as_str(), "test_secret");
    }

    #[test]
    fn test_secret_value_as_str() {
        let sv = SecretValue::new("mykey");
        assert_eq!(sv.as_str(), "mykey");
    }

    #[test]
    fn test_secret_value_is_empty() {
        let sv_empty = SecretValue::new("");
        assert!(sv_empty.is_empty());
        
        let sv_non_empty = SecretValue::new("value");
        assert!(!sv_non_empty.is_empty());
    }

    #[test]
    fn test_secret_value_display_masked() {
        let sv = SecretValue::new("secret123");
        let display = format!("{}", sv);
        assert_eq!(display, "********");
    }

    #[test]
    fn test_secret_value_display_empty() {
        let sv = SecretValue::new("");
        let display = format!("{}", sv);
        assert_eq!(display, "");
    }

    #[test]
    fn test_secret_value_serialize_raw() {
        let sv = SecretValue::new("serialize_me");
        let serialized = serde_json::to_string(&sv).unwrap();
        assert!(serialized.contains("serialize_me"));
    }

    #[test]
    fn test_secret_value_serialize_empty() {
        let sv = SecretValue::new("");
        let serialized = serde_json::to_string(&sv).unwrap();
        assert_eq!(serialized, "\"\"");
    }

    #[test]
    fn test_secret_value_deserialize() {
        // JSON string format
        let json_str = "\"deserialize_me\"";
        let sv: SecretValue = serde_json::from_str(json_str).unwrap();
        assert_eq!(sv.as_str(), "deserialize_me");
    }

    #[test]
    fn test_secret_value_from_string() {
        let s = String::from("from_string");
        let sv: SecretValue = s.into();
        assert_eq!(sv.as_str(), "from_string");
    }

    #[test]
    fn test_secret_value_from_str() {
        let sv: SecretValue = "from_str".into();
        assert_eq!(sv.as_str(), "from_str");
    }

    #[test]
    fn test_secret_value_equality() {
        let sv1 = SecretValue::new("same");
        let sv2 = SecretValue::new("same");
        let sv3 = SecretValue::new("different");
        assert_eq!(sv1, sv2);
        assert_ne!(sv1, sv3);
    }

    #[test]
    fn test_indodax_config_default() {
        let config = IndodaxConfig::default();
        assert!(config.api_key.is_none());
        assert!(config.api_secret.is_none());
        assert!(config.callback_url.is_none());
        assert!(config.paper_balances.is_none());
    }

    #[test]
    #[serial]
    fn test_indodax_config_save_and_load() {
        let config = IndodaxConfig {
            api_key: Some(SecretValue::new("test_key")),
            api_secret: Some(SecretValue::new("test_secret")),
            callback_url: Some("http://callback.test".into()),
            paper_balances: None,
        };
        
        let config_path = IndodaxConfig::config_path();
        config.save().unwrap();
        assert!(config_path.exists());
        
        let loaded = IndodaxConfig::load().unwrap();
        assert_eq!(loaded.api_key.as_ref().unwrap().as_str(), "test_key");
        assert_eq!(loaded.api_secret.as_ref().unwrap().as_str(), "test_secret");
        assert_eq!(loaded.callback_url.as_ref().unwrap(), "http://callback.test");
        
        // Clean up
        fs::remove_file(&config_path).ok();
    }

    #[test]
    #[serial]
    fn test_indodax_config_load_no_file() {
        // Remove any existing config file to ensure clean state
        let config_path = IndodaxConfig::config_path();
        if config_path.exists() {
            fs::remove_file(&config_path).ok();
        }

        // Just test that load() works when no config file exists
        let config = IndodaxConfig::load().unwrap();
        assert!(config.api_key.is_none());
        assert!(config.api_secret.is_none());
    }

    #[test]
    #[serial]
    fn test_indodax_config_config_path() {
        let path = IndodaxConfig::config_path();
        assert!(path.to_string_lossy().contains("indodax"));
        assert!(path.to_string_lossy().contains("config.toml"));
    }

    #[test]
    #[serial]
    fn test_indodax_config_config_dir() {
        let dir = IndodaxConfig::config_dir();
        assert!(dir.to_string_lossy().len() > 0);
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_cli_override() {
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        let config = IndodaxConfig {
            api_key: Some(SecretValue::new("config_key")),
            api_secret: Some(SecretValue::new("config_secret")),
            callback_url: None,
            paper_balances: None,
        };
        
        let result = config.resolve_credentials(
            Some("cli_key".into()),
            Some("cli_secret".into()),
        ).unwrap();
        
        assert!(result.is_some());
        let creds = result.unwrap();
        assert_eq!(creds.api_key.as_str(), "cli_key");
        assert_eq!(creds.api_secret.as_str(), "cli_secret");
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_env_variable() {
        // Clean up any existing env vars first
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        env::set_var("INDODAX_API_KEY", "env_key");
        env::set_var("INDODAX_API_SECRET", "env_secret");
        
        let config = IndodaxConfig::default();
        
        let result = config.resolve_credentials(None, None).unwrap();
        assert!(result.is_some());
        let creds = result.unwrap();
        assert_eq!(creds.api_key.as_str(), "env_key");
        assert_eq!(creds.api_secret.as_str(), "env_secret");
        
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_env_overrides_config() {
        // Clean up any existing env vars first
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        env::set_var("INDODAX_API_KEY", "env_key");
        env::set_var("INDODAX_API_SECRET", "env_secret");
        
        let config = IndodaxConfig {
            api_key: Some(SecretValue::new("config_key")),
            api_secret: Some(SecretValue::new("config_secret")),
            callback_url: None,
            paper_balances: None,
        };
        
        let result = config.resolve_credentials(None, None).unwrap();
        assert!(result.is_some());
        let creds = result.unwrap();
        assert_eq!(creds.api_key.as_str(), "env_key");
        assert_eq!(creds.api_secret.as_str(), "env_secret");
        
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_empty_cli() {
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        let config = IndodaxConfig::default();
        
        let result = config.resolve_credentials(
            Some("".into()),
            Some("".into()),
        ).unwrap();
        
        assert!(result.is_none());
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_empty_env_var() {
        // Clean up any existing env vars first
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        env::set_var("INDODAX_API_KEY", "");
        env::set_var("INDODAX_API_SECRET", "");
        
        let config = IndodaxConfig::default();
        
        let result = config.resolve_credentials(None, None).unwrap();
        assert!(result.is_none());
        
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_no_credentials() {
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        let config = IndodaxConfig::default();
        
        let result = config.resolve_credentials(None, None).unwrap();
        assert!(result.is_none());
    }

    #[test]
    #[serial]
    fn test_resolve_credentials_partial_none() {
        env::remove_var("INDODAX_API_KEY");
        env::remove_var("INDODAX_API_SECRET");
        
        let config = IndodaxConfig {
            api_key: Some(SecretValue::new("key_only")),
            api_secret: None,
            callback_url: None,
            paper_balances: None,
        };
        
        let result = config.resolve_credentials(None, None).unwrap();
        assert!(result.is_none());
    }
}