-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.rs
More file actions
1300 lines (1140 loc) · 49.6 KB
/
init.rs
File metadata and controls
1300 lines (1140 loc) · 49.6 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use anyhow::{Context, Result};
use std::fs;
use std::include_str;
use std::path::Path;
const GITHUB_API_BASE: &str = "https://api.github.com";
const REPO_OWNER: &str = "avocado-linux";
const REPO_NAME: &str = "avocado-os";
const REPO_BRANCH: &str = "main";
const REFERENCES_PATH: &str = "references";
/// GitHub API response structure for directory contents
#[derive(serde::Deserialize, Debug)]
struct GitHubContent {
path: String,
#[serde(rename = "type")]
item_type: String,
download_url: Option<String>,
/// The SHA of the content (for files, dirs, and submodules)
sha: Option<String>,
/// For submodules: the git URL of the submodule repository (may not always be present)
submodule_git_url: Option<String>,
/// The size of the file (submodules have size 0)
size: Option<u64>,
/// The git URL - for submodules this points to the submodule repo's tree
git_url: Option<String>,
}
/// Command to initialize a new Avocado project with configuration files.
///
/// This command creates a new `avocado.yaml` configuration file in the specified
/// directory with default settings for the Avocado build system.
pub struct InitCommand {
/// Target architecture (e.g., "qemux86-64")
target: Option<String>,
/// Directory to initialize (defaults to current directory)
directory: Option<String>,
/// Reference example to download from avocado-os repository
reference: Option<String>,
/// Branch to fetch reference from (defaults to "main")
reference_branch: Option<String>,
/// Specific commit SHA to fetch reference from
reference_commit: Option<String>,
/// Repository to fetch reference from (format: "owner/repo", defaults to "avocado-linux/avocado-os")
reference_repo: Option<String>,
}
impl InitCommand {
/// Creates a new InitCommand instance.
///
/// # Arguments
/// * `target` - Optional target architecture string
/// * `directory` - Optional directory path to initialize
/// * `reference` - Optional reference example name to download
/// * `reference_branch` - Optional branch to fetch reference from
/// * `reference_commit` - Optional specific commit SHA to fetch reference from
/// * `reference_repo` - Optional repository to fetch reference from (format: "owner/repo")
#[allow(clippy::too_many_arguments)]
pub fn new(
target: Option<String>,
directory: Option<String>,
reference: Option<String>,
reference_branch: Option<String>,
reference_commit: Option<String>,
reference_repo: Option<String>,
) -> Self {
Self {
target,
directory,
reference,
reference_branch,
reference_commit,
reference_repo,
}
}
/// Detects the system architecture and returns the appropriate default target.
///
/// # Returns
/// * `"qemux86-64"` for x86_64 systems
/// * `"qemuarm64"` for aarch64 systems
/// * `"qemux86-64"` as fallback for unknown architectures
pub fn get_default_target() -> &'static str {
match std::env::consts::ARCH {
"x86_64" => "qemux86-64",
"aarch64" => "qemuarm64",
_ => "qemux86-64", // fallback to x86_64 for unknown architectures
}
}
/// Returns the repository owner from reference_repo or the default.
fn get_repo_owner(&self) -> &str {
self.reference_repo
.as_ref()
.and_then(|repo| repo.split('/').next())
.unwrap_or(REPO_OWNER)
}
/// Returns the repository name from reference_repo or the default.
fn get_repo_name(&self) -> &str {
self.reference_repo
.as_ref()
.and_then(|repo| repo.split('/').nth(1))
.unwrap_or(REPO_NAME)
}
/// Returns the git ref (commit, branch, or default branch) for API requests.
fn get_git_ref(&self) -> &str {
if let Some(commit) = &self.reference_commit {
commit.as_str()
} else if let Some(branch) = &self.reference_branch {
branch.as_str()
} else {
REPO_BRANCH
}
}
/// Returns the display string for the reference source (for error messages).
fn get_reference_source(&self) -> String {
let owner = self.get_repo_owner();
let name = self.get_repo_name();
let git_ref = self.get_git_ref();
format!("{owner}/{name}/{git_ref}/{REFERENCES_PATH}")
}
/// Loads the configuration template for the specified target.
///
/// # Arguments
/// * `target` - The target architecture string
///
/// # Returns
/// * The configuration template content as a string
fn load_config_template(target: &str) -> String {
// Try to load YAML config first, fall back to default with target substitution
let yaml_content = match target {
"reterminal" => Some(include_str!("../../configs/seeed/reterminal.yaml")),
"reterminal-dm" => Some(include_str!("../../configs/seeed/reterminal-dm.yaml")),
"jetson-orin-nano-devkit" => Some(include_str!(
"../../configs/nvidia/jetson-orin-nano-devkit.yaml"
)),
"raspberrypi4" => Some(include_str!(
"../../configs/raspberry-pi/raspberrypi-4-model-b.yaml"
)),
"raspberrypi5" => Some(include_str!(
"../../configs/raspberry-pi/raspberrypi-5.yaml"
)),
"icam-540" => Some(include_str!("../../configs/advantech/icam-540.yaml")),
_ => None,
};
if let Some(content) = yaml_content {
content.to_string()
} else {
// Use default YAML template and substitute the target
let default_template = include_str!("../../configs/default.yaml");
default_template.replace("{target}", target)
}
}
/// Checks if a reference exists in the repository.
///
/// # Arguments
/// * `reference_name` - The name of the reference to check
///
/// # Returns
/// * `Ok(true)` if the reference exists
/// * `Ok(false)` if the reference doesn't exist
/// * `Err` if there was an error checking
async fn reference_exists(&self, reference_name: &str) -> Result<bool> {
let owner = self.get_repo_owner();
let name = self.get_repo_name();
let git_ref = self.get_git_ref();
let url = format!(
"{GITHUB_API_BASE}/repos/{owner}/{name}/contents/{REFERENCES_PATH}/{reference_name}?ref={git_ref}"
);
let client = reqwest::Client::builder()
.user_agent("avocado-cli")
.build()?;
let response = client.get(&url).send().await?;
Ok(response.status().is_success())
}
/// Downloads a file from GitHub and saves it to the specified path.
///
/// # Arguments
/// * `download_url` - The URL to download the file from
/// * `dest_path` - The destination path to save the file
///
/// # Returns
/// * `Ok(())` if successful
/// * `Err` if there was an error downloading or saving the file
async fn download_file(download_url: &str, dest_path: &Path) -> Result<()> {
let client = reqwest::Client::builder()
.user_agent("avocado-cli")
.build()?;
let response = client
.get(download_url)
.send()
.await
.with_context(|| format!("Failed to download file from {download_url}"))?;
if !response.status().is_success() {
anyhow::bail!("Failed to download file: HTTP {}", response.status());
}
let content = response
.bytes()
.await
.with_context(|| "Failed to read response content")?;
// Create parent directories if they don't exist
if let Some(parent) = dest_path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("Failed to create directory '{}'", parent.display()))?;
}
fs::write(dest_path, content)
.with_context(|| format!("Failed to write file '{}'", dest_path.display()))?;
Ok(())
}
/// Downloads the avocado.yaml file from a reference and returns its content.
///
/// # Arguments
/// * `reference_name` - The name of the reference folder
///
/// # Returns
/// * `Ok(String)` with the file content if successful
/// * `Err` if the file doesn't exist or cannot be downloaded
async fn download_reference_config(&self, reference_name: &str) -> Result<String> {
let owner = self.get_repo_owner();
let name = self.get_repo_name();
let git_ref = self.get_git_ref();
let url = format!(
"{GITHUB_API_BASE}/repos/{owner}/{name}/contents/{REFERENCES_PATH}/{reference_name}/avocado.yaml?ref={git_ref}"
);
let client = reqwest::Client::builder()
.user_agent("avocado-cli")
.build()?;
let response = client.get(&url).send().await.with_context(|| {
format!("Failed to fetch avocado.yaml from reference '{reference_name}'")
})?;
if !response.status().is_success() {
anyhow::bail!("Reference '{reference_name}' does not contain an avocado.yaml file");
}
let content: GitHubContent = response
.json()
.await
.with_context(|| "Failed to parse GitHub API response")?;
if let Some(download_url) = content.download_url {
let file_response = client
.get(&download_url)
.send()
.await
.with_context(|| "Failed to download avocado.yaml")?;
let file_content = file_response
.text()
.await
.with_context(|| "Failed to read avocado.yaml content")?;
Ok(file_content)
} else {
anyhow::bail!("Could not get download URL for avocado.yaml");
}
}
/// Checks if a target is supported in the given TOML content.
///
/// # Arguments
/// * `yaml_content` - The content of the avocado.yaml file
/// * `target` - The target to check for
///
/// # Returns
/// * `Ok(true)` if the target is supported or if supported_targets contains "*"
/// * `Ok(false)` if the target is not supported
/// * `Err` if the YAML cannot be parsed or doesn't have supported_targets
fn is_target_supported(yaml_content: &str, target: &str) -> Result<bool> {
let config: serde_yaml::Value =
serde_yaml::from_str(yaml_content).with_context(|| "Failed to parse avocado.yaml")?;
let supported_targets_value = config.get("supported_targets").ok_or_else(|| {
anyhow::anyhow!("Reference avocado.yaml missing 'supported_targets' field")
})?;
// Handle supported_targets as a string (e.g., "*")
if let Some(s) = supported_targets_value.as_str() {
return Ok(s == "*");
}
// Handle supported_targets as an array
if let Some(array) = supported_targets_value.as_sequence() {
// Check if "*" is in supported_targets (means all targets supported)
let has_wildcard = array.iter().any(|v| v.as_str() == Some("*"));
if has_wildcard {
return Ok(true);
}
// Check if the specific target is in supported_targets
let is_supported = array.iter().any(|v| v.as_str() == Some(target));
return Ok(is_supported);
}
anyhow::bail!("supported_targets must be either a string or an array");
}
/// Updates the default_target in the avocado.yaml file.
///
/// # Arguments
/// * `yaml_path` - Path to the avocado.yaml file
/// * `new_target` - The new target to set as default
///
/// # Returns
/// * `Ok(())` if successful
/// * `Err` if the file cannot be read, parsed, or written
fn update_default_target(yaml_path: &Path, new_target: &str) -> Result<()> {
let content = fs::read_to_string(yaml_path)
.with_context(|| format!("Failed to read '{}'", yaml_path.display()))?;
// Parse as serde_yaml::Value to preserve structure
let mut config: serde_yaml::Value =
serde_yaml::from_str(&content).with_context(|| "Failed to parse avocado.yaml")?;
// Update the default_target field
if let Some(mapping) = config.as_mapping_mut() {
mapping.insert(
serde_yaml::Value::String("default_target".to_string()),
serde_yaml::Value::String(new_target.to_string()),
);
} else {
anyhow::bail!("avocado.yaml is not a valid YAML mapping");
}
// Write back to file
let updated_content =
serde_yaml::to_string(&config).with_context(|| "Failed to serialize updated config")?;
fs::write(yaml_path, updated_content).with_context(|| {
format!(
"Failed to write updated config to '{}'",
yaml_path.display()
)
})?;
Ok(())
}
/// Recursively downloads all contents from a GitHub directory.
///
/// # Arguments
/// * `reference_name` - The name of the reference folder
/// * `github_path` - The path within the repository (relative to references/)
/// * `local_base_path` - The base local path to download to
/// * `repo_owner` - The repository owner
/// * `repo_name` - The repository name
/// * `git_ref` - The git ref (branch/commit) to fetch from
///
/// # Returns
/// * `Ok(())` if successful
/// * `Err` if there was an error downloading the contents
fn download_reference_contents<'a>(
reference_name: &'a str,
github_path: &'a str,
local_base_path: &'a Path,
repo_owner: &'a str,
repo_name: &'a str,
git_ref: &'a str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
let url = format!(
"{GITHUB_API_BASE}/repos/{repo_owner}/{repo_name}/contents/{REFERENCES_PATH}/{github_path}?ref={git_ref}"
);
let client = reqwest::Client::builder()
.user_agent("avocado-cli")
.build()?;
let response = client
.get(&url)
.send()
.await
.with_context(|| format!("Failed to fetch contents from {url}"))?;
if !response.status().is_success() {
anyhow::bail!(
"Failed to fetch contents: HTTP {}. The reference '{reference_name}' may not exist.",
response.status()
);
}
let contents: Vec<GitHubContent> = response
.json()
.await
.with_context(|| "Failed to parse GitHub API response")?;
for item in contents {
let relative_path = item
.path
.strip_prefix(&format!("{REFERENCES_PATH}/"))
.unwrap_or(&item.path)
.strip_prefix(&format!("{reference_name}/"))
.unwrap_or(&item.path);
let local_path = local_base_path.join(relative_path);
// Check if this is a submodule (can appear as type "submodule" or "file")
if Self::is_submodule(&item, repo_owner, repo_name) {
// Try to get submodule info from submodule_git_url first, then fall back to git_url
let submodule_info = if let Some(ref submodule_url) = item.submodule_git_url {
item.sha.as_ref().and_then(|sha| {
Self::parse_github_url(submodule_url)
.map(|(owner, repo)| (owner, repo, sha.clone()))
})
} else {
// Parse from git_url (format: https://api.github.com/repos/{owner}/{repo}/git/trees/{sha})
item.git_url
.as_ref()
.and_then(|url| Self::parse_git_api_url(url))
};
if let Some((sub_owner, sub_repo, sha)) = submodule_info {
println!(" Downloading submodule {relative_path} from {sub_owner}/{sub_repo}@{sha}...");
fs::create_dir_all(&local_path).with_context(|| {
format!("Failed to create directory '{}'", local_path.display())
})?;
Self::download_submodule_contents(
&sub_owner,
&sub_repo,
&sha,
"",
&local_path,
)
.await?;
} else {
println!(
" Warning: Submodule '{relative_path}' missing required info, skipping"
);
}
} else {
match item.item_type.as_str() {
"file" => {
if let Some(ref download_url) = item.download_url {
println!(" Downloading {relative_path}...");
Self::download_file(download_url, &local_path).await?;
}
}
"dir" => {
fs::create_dir_all(&local_path).with_context(|| {
format!("Failed to create directory '{}'", local_path.display())
})?;
// Recursively download directory contents
let sub_path = item.path.replace(&format!("{REFERENCES_PATH}/"), "");
Self::download_reference_contents(
reference_name,
&sub_path,
local_base_path,
repo_owner,
repo_name,
git_ref,
)
.await?;
}
_ => {
// Skip other types (symlinks, etc.)
}
}
}
}
Ok(())
})
}
/// Parses a GitHub URL to extract owner and repo name.
///
/// Supports formats:
/// - https://github.com/owner/repo.git
/// - https://github.com/owner/repo
/// - git://github.com/owner/repo.git
/// - git@github.com:owner/repo.git
///
/// # Arguments
/// * `url` - The GitHub URL to parse
///
/// # Returns
/// * `Some((owner, repo))` if parsing succeeded
/// * `None` if the URL format is not recognized
fn parse_github_url(url: &str) -> Option<(String, String)> {
// Handle git@github.com:owner/repo.git format
if url.starts_with("git@github.com:") {
let path = url.strip_prefix("git@github.com:")?;
let path = path.strip_suffix(".git").unwrap_or(path);
let parts: Vec<&str> = path.split('/').collect();
if parts.len() >= 2 {
return Some((parts[0].to_string(), parts[1].to_string()));
}
return None;
}
// Handle https:// and git:// URLs
let url = url
.strip_prefix("https://github.com/")
.or_else(|| url.strip_prefix("git://github.com/"))?;
let url = url.strip_suffix(".git").unwrap_or(url);
let parts: Vec<&str> = url.split('/').collect();
if parts.len() >= 2 {
Some((parts[0].to_string(), parts[1].to_string()))
} else {
None
}
}
/// Parses a GitHub API git_url to extract owner, repo, and SHA.
///
/// The git_url for submodules has the format:
/// https://api.github.com/repos/{owner}/{repo}/git/trees/{sha}
///
/// # Arguments
/// * `git_url` - The GitHub API git URL
///
/// # Returns
/// * `Some((owner, repo, sha))` if parsing succeeded
/// * `None` if the URL format is not recognized
fn parse_git_api_url(git_url: &str) -> Option<(String, String, String)> {
// Format: https://api.github.com/repos/{owner}/{repo}/git/trees/{sha}
let url = git_url.strip_prefix("https://api.github.com/repos/")?;
let parts: Vec<&str> = url.split('/').collect();
// Expected: [owner, repo, "git", "trees", sha]
if parts.len() >= 5 && parts[2] == "git" && parts[3] == "trees" {
Some((
parts[0].to_string(),
parts[1].to_string(),
parts[4].to_string(),
))
} else {
None
}
}
/// Checks if a GitHubContent item is a submodule.
///
/// Submodules in GitHub's API can appear as either:
/// - type: "submodule" with submodule_git_url
/// - type: "file" with size: 0, download_url: null, and git_url pointing to another repo's tree
fn is_submodule(item: &GitHubContent, current_owner: &str, current_repo: &str) -> bool {
if item.item_type == "submodule" {
return true;
}
// Check for submodules that appear as files
if item.item_type == "file" && item.download_url.is_none() && item.size == Some(0) {
// Check if git_url points to a different repository
if let Some(ref git_url) = item.git_url {
if let Some((owner, repo, _)) = Self::parse_git_api_url(git_url) {
// If the git_url points to a different repo, it's a submodule
return owner != current_owner || repo != current_repo;
}
}
}
false
}
/// Recursively downloads all contents from a GitHub submodule at a specific commit.
///
/// # Arguments
/// * `repo_owner` - The submodule repository owner
/// * `repo_name` - The submodule repository name
/// * `git_ref` - The git ref (commit SHA) to fetch from
/// * `path` - The path within the submodule repository (empty for root)
/// * `local_path` - The local path to download to
///
/// # Returns
/// * `Ok(())` if successful
/// * `Err` if there was an error downloading the contents
fn download_submodule_contents<'a>(
repo_owner: &'a str,
repo_name: &'a str,
git_ref: &'a str,
path: &'a str,
local_path: &'a Path,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
let url = if path.is_empty() {
format!("{GITHUB_API_BASE}/repos/{repo_owner}/{repo_name}/contents?ref={git_ref}")
} else {
format!(
"{GITHUB_API_BASE}/repos/{repo_owner}/{repo_name}/contents/{path}?ref={git_ref}"
)
};
let client = reqwest::Client::builder()
.user_agent("avocado-cli")
.build()?;
let response = client
.get(&url)
.send()
.await
.with_context(|| format!("Failed to fetch submodule contents from {url}"))?;
if !response.status().is_success() {
anyhow::bail!(
"Failed to fetch submodule contents: HTTP {}",
response.status()
);
}
let contents: Vec<GitHubContent> = response
.json()
.await
.with_context(|| "Failed to parse GitHub API response for submodule")?;
for item in contents {
let item_name = item.path.rsplit('/').next().unwrap_or(&item.path);
let item_local_path = local_path.join(item_name);
// Check if this is a submodule (can appear as type "submodule" or "file")
if Self::is_submodule(&item, repo_owner, repo_name) {
// Try to get submodule info from submodule_git_url first, then fall back to git_url
let submodule_info = if let Some(ref submodule_url) = item.submodule_git_url {
item.sha.as_ref().and_then(|sha| {
Self::parse_github_url(submodule_url)
.map(|(owner, repo)| (owner, repo, sha.clone()))
})
} else {
// Parse from git_url (format: https://api.github.com/repos/{owner}/{repo}/git/trees/{sha})
item.git_url
.as_ref()
.and_then(|url| Self::parse_git_api_url(url))
};
if let Some((sub_owner, sub_repo, sha)) = submodule_info {
println!(" Downloading nested submodule {item_name} from {sub_owner}/{sub_repo}@{sha}...");
fs::create_dir_all(&item_local_path).with_context(|| {
format!("Failed to create directory '{}'", item_local_path.display())
})?;
Self::download_submodule_contents(
&sub_owner,
&sub_repo,
&sha,
"",
&item_local_path,
)
.await?;
}
} else {
match item.item_type.as_str() {
"file" => {
if let Some(ref download_url) = item.download_url {
Self::download_file(download_url, &item_local_path).await?;
}
}
"dir" => {
fs::create_dir_all(&item_local_path).with_context(|| {
format!(
"Failed to create directory '{}'",
item_local_path.display()
)
})?;
Self::download_submodule_contents(
repo_owner,
repo_name,
git_ref,
&item.path,
&item_local_path,
)
.await?;
}
_ => {
// Skip other types
}
}
}
}
Ok(())
})
}
/// Creates a .gitignore file with Avocado-specific entries.
///
/// # Arguments
/// * `directory` - The directory to create the .gitignore file in
///
/// # Returns
/// * `Ok(())` if successful
/// * `Err` if the file cannot be written
fn create_gitignore(directory: &str) -> Result<()> {
let gitignore_path = Path::new(directory).join(".gitignore");
// Don't overwrite existing .gitignore files
if gitignore_path.exists() {
// Read existing content
let existing_content = fs::read_to_string(&gitignore_path).with_context(|| {
format!("Failed to read existing '{}'", gitignore_path.display())
})?;
// Check if .avocado-state is already in the .gitignore
if !existing_content.contains(".avocado-state") {
// Append to existing .gitignore
let mut updated_content = existing_content;
if !updated_content.ends_with('\n') {
updated_content.push('\n');
}
updated_content.push_str("\n# Avocado state files\n.avocado-state\n");
fs::write(&gitignore_path, updated_content)
.with_context(|| format!("Failed to update '{}'", gitignore_path.display()))?;
println!("✓ Updated .gitignore to ignore .avocado-state files.");
}
return Ok(());
}
// Create new .gitignore with Avocado-specific entries
let gitignore_content = "# Avocado state files\n.avocado-state\n";
fs::write(&gitignore_path, gitignore_content).with_context(|| {
format!(
"Failed to write .gitignore file '{}'",
gitignore_path.display()
)
})?;
println!("✓ Created .gitignore file.");
Ok(())
}
/// Executes the init command, creating the avocado.yaml configuration file.
///
/// # Returns
/// * `Ok(())` if the initialization was successful
/// * `Err` if there was an error during initialization
///
/// # Errors
/// This function will return an error if:
/// * The target directory cannot be created
/// * The avocado.yaml file already exists
/// * The configuration file cannot be written
/// * The reference doesn't exist (when using --reference)
/// * There was an error downloading reference contents
pub async fn execute(&self) -> Result<()> {
let directory = self.directory.as_deref().unwrap_or(".");
// Validate mutually exclusive options
if self.reference_branch.is_some() && self.reference_commit.is_some() {
anyhow::bail!(
"Cannot specify both --reference-branch and --reference-commit. Please use only one."
);
}
// Validate and create directory if it doesn't exist
if !Path::new(directory).exists() {
fs::create_dir_all(directory)
.with_context(|| format!("Failed to create directory '{directory}'"))?;
}
// If reference is specified, download the reference project
if let Some(ref_name) = &self.reference {
let reference_source = self.get_reference_source();
println!("Initializing from reference '{ref_name}'...");
// Print source info if using non-default values
if self.reference_repo.is_some()
|| self.reference_branch.is_some()
|| self.reference_commit.is_some()
{
println!("Using source: {reference_source}");
}
// Check if reference exists
println!("Checking if reference '{ref_name}' exists...");
if !self.reference_exists(ref_name).await? {
anyhow::bail!(
"Reference '{ref_name}' not found in {reference_source}. \
Please check the available references at https://github.com/{}/{}/tree/{}",
self.get_repo_owner(),
self.get_repo_name(),
self.get_git_ref()
);
}
// If both reference and target are specified, validate target support
if let Some(target) = &self.target {
println!("Validating target '{target}' is supported by reference '{ref_name}'...");
// Download and parse the reference's avocado.yaml
let toml_content = self.download_reference_config(ref_name).await?;
// Check if target is supported
if !Self::is_target_supported(&toml_content, target)? {
anyhow::bail!(
"Target '{target}' is not supported by reference '{ref_name}'. \
Please check the reference's avocado.yaml for supported_targets."
);
}
println!("✓ Target '{target}' is supported by reference '{ref_name}'.");
}
// Download all contents from the reference
println!("Downloading reference contents...");
Self::download_reference_contents(
ref_name,
ref_name,
Path::new(directory),
self.get_repo_owner(),
self.get_repo_name(),
self.get_git_ref(),
)
.await?;
// If a target was specified, update the default_target in the downloaded avocado.yaml
if let Some(target) = &self.target {
let toml_path = Path::new(directory).join("avocado.yaml");
if toml_path.exists() {
println!("Updating default_target to '{target}'...");
Self::update_default_target(&toml_path, target)?;
println!("✓ Updated default_target to '{target}'.");
}
}
println!(
"✓ Successfully initialized project from reference '{ref_name}' in '{}'.",
Path::new(directory)
.canonicalize()
.unwrap_or_else(|_| Path::new(directory).to_path_buf())
.display()
);
} else {
// Original behavior: create avocado.yaml from template
let target = self
.target
.as_deref()
.unwrap_or_else(|| Self::get_default_target());
// Create the avocado.yaml file path
let toml_path = Path::new(directory).join("avocado.yaml");
// Check if configuration file already exists
if toml_path.exists() {
anyhow::bail!(
"Configuration file '{}' already exists.",
toml_path.display()
);
}
// Load the configuration template for the target
let config_content = Self::load_config_template(target);
// Write the configuration file
fs::write(&toml_path, config_content).with_context(|| {
format!(
"Failed to write configuration file '{}'",
toml_path.display()
)
})?;
println!(
"✓ Created config at {}.",
toml_path
.canonicalize()
.unwrap_or_else(|_| toml_path.to_path_buf())
.display()
);
}
// Create .gitignore file (for both reference and non-reference paths)
Self::create_gitignore(directory)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
#[tokio::test]
async fn test_init_default_target() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().to_str().unwrap();
let init_cmd = InitCommand::new(None, Some(temp_path.to_string()), None, None, None, None);
let result = init_cmd.execute().await;
assert!(result.is_ok());
let config_path = PathBuf::from(temp_path).join("avocado.yaml");
assert!(config_path.exists());
let content = fs::read_to_string(&config_path).unwrap();
let expected_target = InitCommand::get_default_target();
assert!(content.contains(&format!("default_target: \"{expected_target}\"")));
assert!(content.contains("distro:"));
assert!(content.contains("channel: apollo-edge"));
assert!(content.contains("version: 0.1.0"));
assert!(content.contains("runtimes:"));
assert!(content.contains("dev:"));
assert!(content.contains("packages:"));
assert!(content.contains("avocado-img-bootfiles:"));
assert!(content.contains("avocado-img-rootfs:"));
assert!(content.contains("avocado-img-initramfs:"));
assert!(content.contains("avocado-ext-dev:"));
assert!(content.contains("type: package"));
assert!(
content.contains("image: \"docker.io/avocadolinux/sdk:{{ config.distro.channel }}\"")
);
assert!(content.contains("extensions:"));
assert!(content.contains("app:"));
assert!(content.contains("- sysext"));
assert!(content.contains("- confext"));
assert!(content.contains("config:"));
assert!(content.contains("avocado-sdk-toolchain: \"{{ config.distro.version }}\""));
}
#[tokio::test]
async fn test_init_custom_target() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().to_str().unwrap();
let init_cmd = InitCommand::new(
Some("custom-arch".to_string()),
Some(temp_path.to_string()),
None,
None,
None,
None,
);
let result = init_cmd.execute().await;
assert!(result.is_ok());
let config_path = PathBuf::from(temp_path).join("avocado.yaml");
let content = fs::read_to_string(&config_path).unwrap();
assert!(content.contains("default_target: \"custom-arch\""));
}
#[tokio::test]
async fn test_init_file_already_exists() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().to_str().unwrap();
let config_path = PathBuf::from(temp_path).join("avocado.yaml");
// Create existing file
fs::write(&config_path, "existing content").unwrap();
let init_cmd = InitCommand::new(None, Some(temp_path.to_string()), None, None, None, None);
let result = init_cmd.execute().await;
assert!(result.is_err());
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("already exists"));
}
#[tokio::test]
async fn test_init_creates_directory() {
let temp_dir = TempDir::new().unwrap();
let new_dir_path = temp_dir.path().join("new_project");
let new_dir_str = new_dir_path.to_str().unwrap();
let init_cmd =
InitCommand::new(None, Some(new_dir_str.to_string()), None, None, None, None);
let result = init_cmd.execute().await;
assert!(result.is_ok());
assert!(new_dir_path.exists());
let config_path = new_dir_path.join("avocado.yaml");
assert!(config_path.exists());
}
#[tokio::test]
async fn test_init_creates_gitignore() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path().to_str().unwrap();
let init_cmd = InitCommand::new(None, Some(temp_path.to_string()), None, None, None, None);
let result = init_cmd.execute().await;
assert!(result.is_ok());