@@ -7,20 +7,22 @@ import (
77 "encoding/hex"
88 "fmt"
99 "io"
10- "log/slog"
1110
1211 "github.com/arduino/go-paths-helper"
1312 "github.com/bcmi-labs/orchestrator/cmd/feedback"
13+ "github.com/bcmi-labs/orchestrator/cmd/i18n"
1414 "github.com/codeclysm/extract/v4"
1515)
1616
17- // TODO: add more fields to download other image versions
1817type Manifest struct {
19- Latest struct {
20- Version string `json:"version"`
21- Url string `json:"url"`
22- Sha256 string `json:"sha256"`
23- } `json:"latest"`
18+ Latest Release `json:"latest"`
19+ Releases []Release `json:"releases"`
20+ }
21+
22+ type Release struct {
23+ Version string `json:"version"`
24+ Url string `json:"url"`
25+ Sha256 string `json:"sha256"`
2426}
2527
2628// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
@@ -59,6 +61,11 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
5961 return nil , fmt .Errorf ("error downloading the image: %v" , err )
6062 }
6163
64+ // Download not confirmed
65+ if tmpZip == nil {
66+ return nil , nil
67+ }
68+
6269 err = ExtractImage (tmpZip , tmpZip .Parent ())
6370 if err != nil {
6471 return nil , fmt .Errorf ("error extracting the image: %v" , err )
@@ -71,39 +78,43 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
7178func DownloadImage (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , string , error ) {
7279 var err error
7380
74- slog . Info ( "Checking for Debian image releases" )
81+ feedback . Print ( i18n . Tr ( "Checking for Debian image releases" ) )
7582 manifest , err := client .GetInfoManifest ()
7683 if err != nil {
7784 return nil , "" , err
7885 }
7986
80- if targetVersion == "latest" {
81- targetVersion = manifest .Latest .Version
87+ var rel * Release
88+ if targetVersion == "latest" || targetVersion == manifest .Latest .Version {
89+ rel = & manifest .Latest
90+ } else {
91+ for _ , r := range manifest .Releases {
92+ if targetVersion == r .Version {
93+ rel = & r
94+ break
95+ }
96+ }
97+ }
98+
99+ if rel == nil {
100+ return nil , "" , fmt .Errorf ("could not find Debian image %s" , targetVersion )
82101 }
83102
84103 if ! forceYes {
85- res , err := upgradeConfirmCb (targetVersion )
104+ res , err := upgradeConfirmCb (rel . Version )
86105 if err != nil {
87106 return nil , "" , err
88107 }
89108 if ! res {
90- slog . Info ( "Download not confirmed by user, exiting" )
109+ feedback . Print ( i18n . Tr ( "Download not confirmed by user, exiting" ) )
91110 return nil , "" , nil
92111 }
93112 }
94113
95- // Download the Debian image
96- var download io.ReadCloser
97- var size int64
98- if targetVersion == manifest .Latest .Version {
99- slog .Info ("Downloading Debian image" , "version" , manifest .Latest .Version )
100- download , size , err = client .FetchZip (manifest .Latest .Url )
101- if err != nil {
102- return nil , "" , fmt .Errorf ("could not fetch Debian image: %w" , err )
103- }
104- } else {
105- // TODO: check the json for the specific version and download it
106- return nil , "" , nil
114+ feedback .Print (i18n .Tr ("Downloading Debian image version %s" , rel .Version ))
115+ download , size , err := client .FetchZip (rel .Url )
116+ if err != nil {
117+ return nil , "" , fmt .Errorf ("could not fetch Debian image: %w" , err )
107118 }
108119 defer download .Close ()
109120
@@ -128,20 +139,20 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
128139 }
129140
130141 // Check the hash
131- if sha256Byte , err := hex .DecodeString (manifest . Latest .Sha256 ); err != nil {
142+ if sha256Byte , err := hex .DecodeString (rel .Sha256 ); err != nil {
132143 return nil , "" , fmt .Errorf ("could not convert sha256 from hex to bytes: %w" , err )
133144 } else if s := checksum .Sum (nil ); ! bytes .Equal (s , sha256Byte ) {
134145 return nil , "" , fmt .Errorf ("bad hash: %x (expected %x)" , s , sha256Byte )
135146 }
136147
137- slog . Info ( "Download of Debian image completed" , "path" , temp )
148+ feedback . Print ( i18n . Tr ( "Download of Debian image completed" ) )
138149
139- return tmpZip , targetVersion , nil
150+ return tmpZip , rel . Version , nil
140151}
141152
142153func ExtractImage (archive , temp * paths.Path ) error {
143154 // Unzip the Debian image
144- slog . Info ( "Unzipping Debian image" , "tmpDir" , temp )
155+ feedback . Print ( i18n . Tr ( "Unzipping Debian image" ) )
145156 tmpZipFile , err := archive .Open ()
146157 if err != nil {
147158 return fmt .Errorf ("could not open archive: %w" , err )
0 commit comments