After adding a second subcommand to the Cobra console app, I got the error (no value) used as value
. Looking up the error, it says that TooManyValues occur. As if I'm attempting to return 2 values instead of one. Which is simple enough, but I'm not sure how that applies to the code I've written. Should I not be adding the functions to the same file as the cobra console code?
Error:
go build -o azGoCLI.exe
# azGoCLI/cmd
cmd\blob.go:40:25: DeleteContainer(args[0], args[1]) used as value
Much thanks in advance!
package cmd
import (
"context"
"log"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(mainCmd)
mainCmd.AddCommand(createContainer)
mainCmd.AddCommand(deleteContainer)
}
var mainCmd = &cobra.Command{
Use: "blob",
Short: "...",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
var createContainer = &cobra.Command{
Use: "create-container [storageAccount] [containerName]",
Short: "...",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return CreateContainer(args[0], args[1])
},
}
var deleteContainer = &cobra.Command{
Use: "delete-container [storageAccount] [containerName]",
Short: "...",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteContainer(args[0], args[1])
},
}
func CreateContainer(storageaccount, container string) error {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("Authentication failure: % v", err)
}
ctx := context.Background()
containerClient, err := azblob.NewContainerClient("https://" storageaccount ".blob.core.windows.net/" container, cred, nil)
_, err = containerClient.Create(ctx, nil)
if err != nil {
log.Fatal(err)
}
return nil
}
func DeleteContainer(storageaccount, container string) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("Authentication failure: % v", err)
}
ctx := context.Background()
containerClient, err := azblob.NewContainerClient("https://" storageaccount ".blob.core.windows.net/" container, cred, nil)
_, err = containerClient.Delete(ctx, nil)
if err != nil {
log.Fatalf("Failure: % v", err)
}
}
CodePudding user response:
As you can see, your function
func DeleteContainer(storageaccount, container string) {
doesn't return anything. However you are returninng it here:
var deleteContainer = &cobra.Command{
Use: "delete-container [storageAccount] [containerName]",
Short: "...",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteContainer(args[0], args[1])
},
}
in RunE
as if it returned an error
. That is why it's throwing an error.'
Solving this will depend on your use case though as to how you want to handle the error in case DeleteContainer
errors out
CodePudding user response:
Solution: Add a return value for the DeleteContainer function and a return statement.
func DeleteContainer(storageaccount, container string) error {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("Authentication failure: % v", err)
}
ctx := context.Background()
containerClient, err := azblob.NewContainerClient("https://" storageaccount ".blob.core.windows.net/" container, cred, nil)
_, err = containerClient.Delete(ctx, nil)
if err != nil {
log.Fatalf("Failure: % v", err)
}
return nil
}