2022-02-25 05:26:40 +00:00
package main
import (
"fmt"
"path/filepath"
"time"
"github.com/cheggaaa/pb/v3"
2022-10-17 19:40:51 +00:00
"github.com/rs/zerolog/log"
2022-02-25 05:26:40 +00:00
"github.com/urfave/cli/v2"
"go.arsenm.dev/itd/api"
)
func fwUpgrade ( c * cli . Context ) error {
2022-10-16 20:17:12 +00:00
resources := c . String ( "resources" )
if resources != "" {
absRes , err := filepath . Abs ( resources )
if err != nil {
return err
}
err = resLoad ( c . Context , [ ] string { absRes } )
if err != nil {
2022-10-17 19:40:51 +00:00
log . Error ( ) . Msg ( "Resource loading has returned an error. This can happen if your current version of InfiniTime doesn't support BLE FS. Try updating without resource loading, and then load them after using the `itctl res load` command." )
2022-10-16 20:17:12 +00:00
return err
}
}
2022-02-25 05:26:40 +00:00
start := time . Now ( )
var upgType api . UpgradeType
var files [ ] string
// Get relevant data struct
if c . String ( "archive" ) != "" {
// Get archive data struct
2022-04-23 00:12:30 +00:00
upgType = api . UpgradeTypeArchive
2022-02-25 05:26:40 +00:00
files = [ ] string { c . String ( "archive" ) }
} else if c . String ( "init-packet" ) != "" && c . String ( "firmware" ) != "" {
// Get files data struct
2022-04-23 00:12:30 +00:00
upgType = api . UpgradeTypeFiles
2022-02-25 05:26:40 +00:00
files = [ ] string { c . String ( "init-packet" ) , c . String ( "firmware" ) }
} else {
return cli . Exit ( "Upgrade command requires either archive or init packet and firmware." , 1 )
}
2022-05-01 22:22:28 +00:00
progress , err := client . FirmwareUpgrade ( c . Context , upgType , abs ( files ) ... )
2022-02-25 05:26:40 +00:00
if err != nil {
return err
}
// Create progress bar template
barTmpl := ` {{ counters . }} B {{ bar . "|" "-" ( cycle . ) " " "|" }} {{ percent . }} {{ rtime . "%s" }} `
// Start full bar at 0 total
bar := pb . ProgressBarTemplate ( barTmpl ) . Start ( 0 )
// Create new scanner of connection
for event := range progress {
// Set total bytes in progress bar
bar . SetTotal ( event . Total )
// Set amount of bytes received in progress bar
2022-04-23 00:12:30 +00:00
bar . SetCurrent ( int64 ( event . Received ) )
2022-02-25 05:26:40 +00:00
// If transfer finished, break
2022-04-23 00:12:30 +00:00
if int64 ( event . Sent ) == event . Total {
2022-02-25 05:26:40 +00:00
break
}
}
// Finish progress bar
bar . Finish ( )
fmt . Printf ( "Transferred %d B in %s.\n" , bar . Total ( ) , time . Since ( start ) )
fmt . Println ( "Remember to validate the new firmware in the InfiniTime settings." )
return nil
}
func fwVersion ( c * cli . Context ) error {
2022-05-01 22:22:28 +00:00
version , err := client . Version ( c . Context )
2022-02-25 05:26:40 +00:00
if err != nil {
return err
}
fmt . Println ( version )
return nil
}
func abs ( paths [ ] string ) [ ] string {
for index , path := range paths {
newPath , err := filepath . Abs ( path )
if err != nil {
continue
}
paths [ index ] = newPath
}
return paths
}