Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use sources from config file that was set via flag? #2034

Open
dearchap opened this issue Dec 25, 2024 Discussed in #2032 · 2 comments · May be fixed by #2033
Open

How to use sources from config file that was set via flag? #2034

dearchap opened this issue Dec 25, 2024 Discussed in #2032 · 2 comments · May be fixed by #2033
Assignees
Labels
area/v3 relates to / is being considered for v3

Comments

@dearchap
Copy link
Contributor

Discussed in #2032

Originally posted by plandem December 23, 2024
Hi

Sadly, I cant find how to implement this simple setup:

sources := cli.NewValueSourceChain()
app := &cli.Command{
		EnableShellCompletion:  true,
		UseShortOptionHandling: true,
		Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
			config := cmd.String("config")
			if len(config) > 0 {
				configFile := fmt.Sprintf("%s.yaml", cmd.String("config"))
				if _, err := os.Stat(configFile); os.IsNotExist(err) {
					return ctx, fmt.Errorf("could not load config file: %s", configFile)
				}

				sources.Append(cli.Files(configFile))
				return ctx, nil
			}

			return ctx, nil
		},
		OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error {
			os.Exit(1)
			return err
		},
		ShellComplete: func(ctx context.Context, cmd *cli.Command) {
			fmt.Fprintf(cmd.Root().Writer, "check\nnotify\nvalidate\n")
		},
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "config",
				Aliases: []string{"c"},
				Usage:   "Load configuration from `FILE`",
			},
		},
		Commands: []*cli.Command{
			{
				Name:   "notify",
				Usage:  "Notify Telegram about updates",
				Flags:  []cli.Flag{
					&cli.StringFlag{
						Name:     "token",
						Required: true,
						Usage:    "`TOKEN` of Telegram's bot",
						//Sources:  altsrc.YAML("notify.telegram-chat", ???), // how to use sources/files from 'Before'?
					},
					&cli.StringFlag{
						Name:     "chat",
						Required: true,
						Usage:    "`ID` of Telegram's chat to send updates",
						//Sources:  altsrc.YAML("notify.telegram-chat", ???),  // how to use sources/files from 'Before'?
					},
				}
				Action: notify.Telegram,
			},

Setup is quite typical, tbh - provide custom config file and use values from it. But from docs, it looks like it may work only with hardcoded names? Any direction? Ofc, I could hack around to get config's file outside from the cli package, but I feel that could miss something and there is a built-in way to achieve it.

Can anybody point me to what direction should I look?

P.S.: v3 of cli

@dearchap dearchap added the area/v3 relates to / is being considered for v3 label Dec 25, 2024
@dearchap dearchap self-assigned this Dec 25, 2024
@dearchap
Copy link
Contributor Author

@plandem I have a PoC PR which might help resolve your issue. You may have to customize the value source to read the flag value and load files. I have to think more on this.

@plandem
Copy link

plandem commented Dec 26, 2024

@plandem I have a PoC PR which might help resolve your issue. You may have to customize the value source to read the flag value and load files. I have to think more on this.

not sure how to use it :( since its PoC, do you need some feedback or what is the next step to make it possible (i mean original topic of discussion)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants