github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/ota/decode.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  	"os"
    22  
    23  	"github.com/arduino/arduino-cli/cli/errorcodes"
    24  	"github.com/arduino/arduino-cli/cli/feedback"
    25  	"github.com/arduino/arduino-cloud-cli/command/ota"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  type decodeHeaderFlags struct {
    30  	file string
    31  }
    32  
    33  func initDecodeHeaderCommand() *cobra.Command {
    34  	flags := &decodeHeaderFlags{}
    35  	uploadCommand := &cobra.Command{
    36  		Use:   "header-decode",
    37  		Short: "OTA firmware header decoder",
    38  		Long:  "decode OTA firmware header of the given binary file",
    39  		Run: func(cmd *cobra.Command, args []string) {
    40  			if err := runDecodeHeaderCommand(flags); err != nil {
    41  				feedback.Errorf("Error during firmware decoding: %v", err)
    42  				os.Exit(errorcodes.ErrGeneric)
    43  			}
    44  		},
    45  	}
    46  	uploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.ota)")
    47  	uploadCommand.MarkFlagRequired("file")
    48  	return uploadCommand
    49  }
    50  
    51  func runDecodeHeaderCommand(flags *decodeHeaderFlags) error {
    52  	params := &ota.ReadHeaderParams{
    53  		File: flags.file,
    54  	}
    55  	if err := ota.ReadHeader(params); err != nil {
    56  		return err
    57  	}
    58  	return nil
    59  }