mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
* chore: rename `AgentConn` to `WorkspaceAgentConn` The codersdk was becoming bloated with consts for the workspace agent that made no sense to a reader. `Tailnet*` is an example of these consts. * chore: remove `Get` prefix from *Client functions * chore: remove `BypassRatelimits` option in `codersdk.Client` It feels wrong to have this as a direct option because it's so infrequently needed by API callers. It's better to directly modify headers in the two places that we actually use it. * Merge `appearance.go` and `buildinfo.go` into `deployment.go` * Merge `experiments.go` and `features.go` into `deployment.go` * Fix `make gen` referencing old type names * Merge `error.go` into `client.go` `codersdk.Response` lived in `error.go`, which is wrong. * chore: refactor workspace agent functions into agentsdk It was odd conflating the codersdk that clients should use with functions that only the agent should use. This separates them into two SDKs that are closely coupled, but separate. * Merge `insights.go` into `deployment.go` * Merge `organizationmember.go` into `organizations.go` * Merge `quota.go` into `workspaces.go` * Rename `sse.go` to `serversentevents.go` * Rename `codersdk.WorkspaceAppHostResponse` to `codersdk.AppHostResponse` * Format `.vscode/settings.json` * Fix outdated naming in `api.ts` * Fix app host response * Fix unsupported type * Fix imported type
84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/cli/cliui"
|
|
"github.com/coder/coder/coderd/gitauth"
|
|
"github.com/coder/coder/codersdk"
|
|
"github.com/coder/retry"
|
|
)
|
|
|
|
// gitAskpass is used by the Coder agent to automatically authenticate
|
|
// with Git providers based on a hostname.
|
|
func gitAskpass() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "gitaskpass",
|
|
Hidden: true,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
ctx := cmd.Context()
|
|
|
|
ctx, stop := signal.NotifyContext(ctx, InterruptSignals...)
|
|
defer stop()
|
|
|
|
user, host, err := gitauth.ParseAskpass(args[0])
|
|
if err != nil {
|
|
return xerrors.Errorf("parse host: %w", err)
|
|
}
|
|
|
|
client, err := createAgentClient(cmd)
|
|
if err != nil {
|
|
return xerrors.Errorf("create agent client: %w", err)
|
|
}
|
|
|
|
token, err := client.GitAuth(ctx, host, false)
|
|
if err != nil {
|
|
var apiError *codersdk.Error
|
|
if errors.As(err, &apiError) && apiError.StatusCode() == http.StatusNotFound {
|
|
// This prevents the "Run 'coder --help' for usage"
|
|
// message from occurring.
|
|
cmd.Printf("%s\n", apiError.Message)
|
|
return cliui.Canceled
|
|
}
|
|
return xerrors.Errorf("get git token: %w", err)
|
|
}
|
|
if token.URL != "" {
|
|
if err := openURL(cmd, token.URL); err == nil {
|
|
cmd.Printf("Your browser has been opened to authenticate with Git:\n\n\t%s\n\n", token.URL)
|
|
} else {
|
|
cmd.Printf("Open the following URL to authenticate with Git:\n\n\t%s\n\n", token.URL)
|
|
}
|
|
|
|
for r := retry.New(250*time.Millisecond, 10*time.Second); r.Wait(ctx); {
|
|
token, err = client.GitAuth(ctx, host, true)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
cmd.Printf("You've been authenticated with Git!\n")
|
|
break
|
|
}
|
|
}
|
|
|
|
if token.Password != "" {
|
|
if user == "" {
|
|
fmt.Fprintln(cmd.OutOrStdout(), token.Username)
|
|
} else {
|
|
fmt.Fprintln(cmd.OutOrStdout(), token.Password)
|
|
}
|
|
} else {
|
|
fmt.Fprintln(cmd.OutOrStdout(), token.Username)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|