将多个函数添加到Cobra包中会导致编译错误:[ (no value) used as value ]。

huangapple go评论88阅读模式
英文:

Adding multiple functions to Cobra packages causes [ (no value) used as value ] compile error

问题

在将第二个子命令添加到Cobra控制台应用程序后,我遇到了错误(no value) used as value。查找该错误,它表示发生了TooManyValues错误。就好像我试图返回两个值而不是一个值一样。这很简单,但我不确定它如何适用于我编写的代码。我是否不应该将函数添加到与Cobra控制台代码相同的文件中?

错误信息:

go build -o azGoCLI.exe
# azGoCLI/cmd
cmd\blob.go:40:25: DeleteContainer(args[0], args[1]) used as value

非常感谢!

英文:

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)
	}
}

答案1

得分: 0

你可以看到,你的函数

func DeleteContainer(storageaccount, container string) {

没有返回任何内容。然而你在这里返回了它:

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])
    },
}

RunE 中,就好像它返回了一个 error。这就是为什么它会抛出一个错误的原因。

解决这个问题将取决于你的使用情况,如果 DeleteContainer 出错,你想如何处理这个错误。

英文:

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

答案2

得分: 0

解决方案:为DeleteContainer函数添加一个返回值和一个返回语句。

func DeleteContainer(storageaccount, container string) error {
	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatalf("身份验证失败:%+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("失败:%+v", err)
	}
	return nil
}
英文:

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
}

huangapple
  • 本文由 发表于 2021年12月3日 05:14:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/70206537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定