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

caddycmd.Commands() function doesn't work #6819

Open
tozh opened this issue Jan 30, 2025 · 4 comments
Open

caddycmd.Commands() function doesn't work #6819

tozh opened this issue Jan 30, 2025 · 4 comments

Comments

@tozh
Copy link

tozh commented Jan 30, 2025

In https://github.com/caddyserver/caddy/blob/9996d6a70ba76a94dfc90548f25fbac0ce9da497/cmd/commands.go#L80C1-L86C40. There is a function Commands() to get a list of commands initialised by RegisterCommand.

// Commands returns a list of commands initialised by
// RegisterCommand
func Commands() map[string]Command {
	return commands
}

var commands = make(map[string]Command)

But actually, the RegisterCommand function doesn't set the command into the commands map. It only reads from the commands map. So the funcion Commands() always return an empty map.

I wonder if the Commands() function still works.

By the way, I want to use the Commands() function get the caddy run command. Then I need to overwrite the original caddy run function to redirect its output to somewhere to remove its stdout/stderr from OS.Stdout/os.Stderr. Its output interferes with the go test output. Do you have any recommendations to achieve this?

@tozh
Copy link
Author

tozh commented Jan 30, 2025

More git history found:
function Commands() was introduced in #4316.
And it didn't work since #4565.

@mholt
Copy link
Member

mholt commented Jan 30, 2025

Hmm, @mohammed90 maybe we should look into this when we have a sec.

@tozh

Then I need to overwrite the original caddy run function to redirect its output to somewhere to remove its stdout/stderr from OS.Stdout/os.Stderr.

That seems like less of a program concern and more of a shell concern. I would just append this to your command (bash): >/dev/null 2>&1

@tozh
Copy link
Author

tozh commented Jan 31, 2025

Hi @mholt ,

Thanks for your reply.

Unfortunately, I don't run caddy in bash. I am trying to compile and run caddy in go test, just like the way at https://github.com/caddyserver/caddy/blob/master/caddytest/caddytest.go#L268

os.Args = []string{"caddy", "run", "--config", f.Name(), "--adapter", "caddyfile"}

go func() {
	caddycmd.Main()
}()

@mholt
Copy link
Member

mholt commented Jan 31, 2025

Regardless of which shell or execution environment, I feel like redirecting stdout/stderr is the concern of the shell or execution environment, rather than the program being executed. It's also going to be much simpler that way.

We'll look into the empty map thing though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants