github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/ota/upload.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  	"context"
    22  	"fmt"
    23  	"os"
    24  
    25  	"github.com/arduino/arduino-cli/cli/errorcodes"
    26  	"github.com/arduino/arduino-cli/cli/feedback"
    27  	"github.com/arduino/arduino-cloud-cli/command/ota"
    28  	"github.com/arduino/arduino-cloud-cli/config"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  type uploadFlags struct {
    34  	deviceID         string
    35  	file             string
    36  	deferred         bool
    37  	doNotApplyHeader bool
    38  }
    39  
    40  func initUploadCommand() *cobra.Command {
    41  	flags := &uploadFlags{}
    42  	uploadCommand := &cobra.Command{
    43  		Use:   "upload",
    44  		Short: "OTA upload",
    45  		Long:  "OTA upload on a device of Arduino IoT Cloud",
    46  		Run: func(cmd *cobra.Command, args []string) {
    47  			if err := runUploadCommand(flags); err != nil {
    48  				feedback.Errorf("Error during ota upload: %v", err)
    49  				os.Exit(errorcodes.ErrGeneric)
    50  			}
    51  		},
    52  	}
    53  	uploadCommand.Flags().StringVarP(&flags.deviceID, "device-id", "d", "", "Device ID")
    54  	uploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.bin) to be uploaded")
    55  	uploadCommand.Flags().BoolVar(&flags.deferred, "deferred", false, "Perform a deferred OTA. It can take up to 1 week.")
    56  	uploadCommand.Flags().BoolVar(&flags.doNotApplyHeader, "no-header", false, "Do not apply header and compression to binary file before upload")
    57  	uploadCommand.MarkFlagRequired("device-id")
    58  	uploadCommand.MarkFlagRequired("file")
    59  	return uploadCommand
    60  }
    61  
    62  func runUploadCommand(flags *uploadFlags) error {
    63  	logrus.Infof("Uploading binary %s to device %s", flags.file, flags.deviceID)
    64  
    65  	cred, err := config.RetrieveCredentials()
    66  	if err != nil {
    67  		return fmt.Errorf("retrieving credentials: %w", err)
    68  	}
    69  
    70  	params := &ota.UploadParams{
    71  		DeviceID:         flags.deviceID,
    72  		File:             flags.file,
    73  		Deferred:         flags.deferred,
    74  		DoNotApplyHeader: flags.doNotApplyHeader,
    75  	}
    76  	err = ota.Upload(context.TODO(), params, cred)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	logrus.Info("Upload successfully started")
    82  	return nil
    83  }