github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/ota/cancel.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/arduino/arduino-cloud-cli/config"
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  type cancelFlags struct {
    32  	otaID string
    33  }
    34  
    35  func initOtaCancelCommand() *cobra.Command {
    36  	flags := &cancelFlags{}
    37  	uploadCommand := &cobra.Command{
    38  		Use:   "cancel",
    39  		Short: "OTA cancel",
    40  		Long:  "Cancel OTA by OTA ID",
    41  		Run: func(cmd *cobra.Command, args []string) {
    42  			if err := runOtaCancelCommand(flags); err != nil {
    43  				feedback.Errorf("Error during ota cancel: %v", err)
    44  				os.Exit(errorcodes.ErrGeneric)
    45  			}
    46  		},
    47  	}
    48  	uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID")
    49  
    50  	return uploadCommand
    51  }
    52  
    53  func runOtaCancelCommand(flags *cancelFlags) error {
    54  	if flags.otaID == "" {
    55  		return fmt.Errorf("required flag \"ota-id\" not set")
    56  	}
    57  
    58  	cred, err := config.RetrieveCredentials()
    59  	if err != nil {
    60  		return fmt.Errorf("retrieving credentials: %w", err)
    61  	}
    62  
    63  	return ota.CancelOta(flags.otaID, cred)
    64  }