Context retrieved from cobra subcommand is empty

WBOY
Release: 2024-02-06 09:42:04
forward
826 people have browsed it

从 cobra 子命令检索的上下文为空

Question content

I want a global timeout (set in rootCmd), so I set it in rootCmd is set as follows

ctxInit := context.Background()
timeout := viper.GetInt("timeout")
ctx, cancel := context.WithTimeout(ctxInit, time.Duration(timeout)*time.Second)
defer cancel()
cmd.SetContext(ctx)
Copy after login

Then in the subcommand

ctx := rootCmd.Context()
Copy after login

But ctx is context.emptyCtx {}

Am I doing something wrong in setting/retrieving the context?

edit

My rootCmd Statement

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
    Use:              "my-cli",
    TraverseChildren: true,
    Short:            "cli",
    PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
        var err error
        logger, err = logging.InitialiseLogger(*logLevel, *logFormat)
        if err != nil {
            return err
        }
        if err := viper.BindPFlags(cmd.Flags()); err != nil {
            return fmt.Errorf("error binding flags to %s command: %w\n", cmd.Name(), err)
        }
        if err := cloneMethodValidator(cmd); err != nil {
            return err
        }
        if err := InitConfig(false); err != nil {
            logger.Fatal("ERROR initiating configuration:\n", err)
        }
        ctxInit := context.Background()
        timeout := viper.GetInt("timeout")
        ctx, cancel := context.WithTimeout(ctxInit, time.Duration(timeout)*time.Second)
        defer cancel()
        cmd.SetContext(ctx)
        return nil
    },
}
Copy after login


Correct answer


As @Peter mentioned, cmd and rootCmd are not the same. The Cobra documentation describes PersistentPreRun(E):

So cmd.SetContext(ctx) does not set the context of rootCmd, but sets the context of the subcommand.

Then in the subcommand you can use:

ctx := cmd.Context()
Copy after login

instead of rootCmd.Context().

The above is the detailed content of Context retrieved from cobra subcommand is empty. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!