MCPcopy Index your code
hub / github.com/coder/coder / TryInitClient

Method TryInitClient

cli/root.go:729–800  ·  view source on GitHub ↗

TryInitClient is similar to InitClient but doesn't error when credentials are missing. This allows commands to run without requiring authentication, but still use auth if available.

(inv *serpent.Invocation)

Source from the content-addressed store, hash-verified

727// TryInitClient is similar to InitClient but doesn't error when credentials are missing.
728// This allows commands to run without requiring authentication, but still use auth if available.
729func (r *RootCmd) TryInitClient(inv *serpent.Invocation) (*codersdk.Client, error) {
730 conf := r.createConfig()
731 // Read the client URL stored on disk.
732 if r.clientURL == nil || r.clientURL.String() == "" {
733 rawURL, err := conf.URL().Read()
734 // If the configuration files are absent, just continue without URL
735 if err != nil {
736 // Continue with a nil or empty URL
737 if !os.IsNotExist(err) {
738 return nil, err
739 }
740 } else {
741 r.clientURL, err = url.Parse(strings.TrimSpace(rawURL))
742 if err != nil {
743 return nil, err
744 }
745 }
746 }
747 if r.token == "" {
748 tok, err := r.ensureTokenBackend().Read(r.clientURL)
749 // Even if there isn't a token, we don't care.
750 // Some API routes can be unauthenticated.
751 if err != nil && !xerrors.Is(err, os.ErrNotExist) {
752 if xerrors.Is(err, sessionstore.ErrNotImplemented) {
753 return nil, errKeyringNotSupported
754 }
755 return nil, err
756 }
757 if tok != "" {
758 r.token = tok
759 }
760 }
761
762 // Only configure the client if we have a URL
763 if r.clientURL != nil && r.clientURL.String() != "" {
764 // Load TLS config from files if specified
765 if err := r.ensureTLSConfig(); err != nil {
766 return nil, err
767 }
768
769 // Configure HTTP client with transport wrappers
770 httpClient, err := r.createHTTPClient(inv.Context(), r.clientURL, inv)
771 if err != nil {
772 return nil, err
773 }
774
775 clientOpts := []codersdk.ClientOption{
776 codersdk.WithSessionToken(r.token),
777 codersdk.WithHTTPClient(httpClient),
778 }
779
780 if r.disableDirect {
781 clientOpts = append(clientOpts, codersdk.WithDisableDirectConnections())
782 }
783
784 if r.tlsConfig != nil {
785 clientOpts = append(clientOpts, codersdk.WithDERPTLSConfig(r.tlsConfig))
786 }

Callers 2

mcpServerMethod · 0.95

Calls 15

createConfigMethod · 0.95
ensureTokenBackendMethod · 0.95
ensureTLSConfigMethod · 0.95
createHTTPClientMethod · 0.95
WithSessionTokenFunction · 0.92
WithHTTPClientFunction · 0.92
WithDERPTLSConfigFunction · 0.92
WithPlainLoggerFunction · 0.92
WithLogBodiesFunction · 0.92
NewFunction · 0.92
ReadMethod · 0.65

Tested by

no test coverage detected