Skip to content

Move Flag upwards #1202

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Move Flag upwards #1202

wants to merge 1 commit into from

Conversation

KevyVo
Copy link
Contributor

@KevyVo KevyVo commented May 22, 2025

Description

Linked Issues

Checklist

  • I have performed a self-review of my code
  • I have added appropriate tests
  • I have updated the Defang CLI docs and/or README to reflect my changes, if necessary

@KevyVo KevyVo linked an issue May 22, 2025 that may be closed by this pull request
@KevyVo KevyVo self-assigned this May 22, 2025
@@ -75,6 +78,28 @@ const (
PromptYes Prompt = true
)

func NewAuthServer(handler http.Handler, port int) *httptest.Server {
// create a listener with the desired port.
l, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work in Docker? I'd expect we'd have to use 0.0.0.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have to use 0.0.0.0, this is just me playing with things, I am not exactly sure what the best approach, I just push onto draft PR so I don't lose the code.

@@ -75,6 +78,28 @@ const (
PromptYes Prompt = true
)

func NewAuthServer(handler http.Handler, port int) *httptest.Server {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint16 instead of int?

@@ -17,7 +17,7 @@ func Token(ctx context.Context, client client.FabricClient, tenant types.TenantN
return ErrDryRun
}

code, err := auth.StartAuthCodeFlow(ctx, true)
code, err := auth.StartAuthCodeFlow(ctx, auth.PromptYes, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this command should probably honor any --auth-server flag as well.

log.Fatal(err)
}

ts := httptest.NewUnstartedServer(handler)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there still value in using httptest as opposed to using the vanilla http package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use httptest because its underlying server.Start() method independently manages the server goroutine lifecycle. It utilizes wait groups and wraps the connection state-tracking hook to monitor which connections are idle. Plus it non-blocking. I am just exploring which server function may be better.

// NewUnstartedServer creates a listener. Close that listener and replace
// with the one we created.
ts.Listener.Close()
ts.Listener = l
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels pretty hacky. There must be a clean way to tell http to use a given port or listener.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe still WIP, but I agree. just trying things out.

@@ -390,7 +400,7 @@ var RootCmd = &cobra.Command{
term.ResetWarnings() // clear any previous warnings so we don't show them again

defer func() { track.Cmd(nil, "Login", P("reason", err)) }()
if err = cli.InteractiveLogin(cmd.Context(), client, getCluster()); err != nil {
if err = cli.InteractiveLoginPrompt(cmd.Context(), client, getCluster(), port); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now opens the browser without user interaction. Not a change we want, I think.

port, _ := cmd.Flags().GetInt("auth-server")
if port != 0 {
go func() {
if err := cli.InteractiveLoginWithDocker(cmd.Context(), getCluster(), port); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only the MCP server needs this to be long running. Regular CLI can use the existing flow (Start/stop server when needed) but honoring the --auth-server port that was given.

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

Successfully merging this pull request may close these issues.

Move the Auth flag upwards and make so that docker cli can be used
2 participants