github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/ota/encode.go (about) 1 // This file is part of arduino-cloud-cli. 2 // 3 // Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published 7 // by the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package ota 19 20 import ( 21 "fmt" 22 "os" 23 24 "github.com/arduino/arduino-cli/cli/errorcodes" 25 "github.com/arduino/arduino-cli/cli/feedback" 26 "github.com/arduino/arduino-cloud-cli/command/ota" 27 "github.com/spf13/cobra" 28 ) 29 30 type encodeBinaryFlags struct { 31 FQBN string 32 file string 33 } 34 35 func initEncodeBinaryCommand() *cobra.Command { 36 flags := &encodeBinaryFlags{} 37 uploadCommand := &cobra.Command{ 38 Use: "header-encode", 39 Short: "OTA firmware encoder", 40 Long: "encode header firmware to make it compatible with Arduino OTA", 41 Run: func(cmd *cobra.Command, args []string) { 42 if err := runEncodeCommand(flags); err != nil { 43 feedback.Errorf("Error during firmware encoding: %v", err) 44 os.Exit(errorcodes.ErrGeneric) 45 } 46 }, 47 } 48 uploadCommand.Flags().StringVarP(&flags.FQBN, "fqbn", "b", "", "Device fqbn") 49 uploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.bin) to be encoded") 50 uploadCommand.MarkFlagRequired("fqbn") 51 uploadCommand.MarkFlagRequired("file") 52 return uploadCommand 53 } 54 55 func runEncodeCommand(flags *encodeBinaryFlags) error { 56 params := &ota.EncodeParams{ 57 FQBN: flags.FQBN, 58 File: flags.file, 59 } 60 otafile, err := ota.Encode(params) 61 if err != nil { 62 return err 63 } 64 65 feedback.Print(fmt.Sprintf("Encode successfully performed. File: %s", *otafile)) 66 67 return nil 68 }