ensureTokenBackend returns the session token storage backend, creating it if necessary. This must be called after flags are parsed so we can respect the value of the --use-keyring flag.
()
| 879 | // This must be called after flags are parsed so we can respect the value of the --use-keyring |
| 880 | // flag. |
| 881 | func (r *RootCmd) ensureTokenBackend() sessionstore.Backend { |
| 882 | if r.tokenBackend == nil { |
| 883 | // Checking for the --global-config directory being set is a bit wonky but necessary |
| 884 | // to allow extensions that invoke the CLI with this flag (e.g. VS code) to continue |
| 885 | // working without modification. In the future we should modify these extensions to |
| 886 | // either access the credential in the keyring (like Coder Desktop) or some other |
| 887 | // approach that doesn't rely on the session token being stored on disk. |
| 888 | assumeExtensionInUse := r.globalConfig != config.DefaultDir() && !r.useKeyringWithGlobalConfig |
| 889 | keyringSupported := runtime.GOOS == "windows" || runtime.GOOS == "darwin" |
| 890 | if r.useKeyring && !assumeExtensionInUse && keyringSupported { |
| 891 | serviceName := sessionstore.DefaultServiceName |
| 892 | if r.keyringServiceName != "" { |
| 893 | serviceName = r.keyringServiceName |
| 894 | } |
| 895 | r.tokenBackend = sessionstore.NewKeyringWithService(serviceName) |
| 896 | } else { |
| 897 | r.tokenBackend = sessionstore.NewFile(r.createConfig) |
| 898 | } |
| 899 | } |
| 900 | return r.tokenBackend |
| 901 | } |
| 902 | |
| 903 | // WithKeyringServiceName sets a custom keyring service name for testing purposes. |
| 904 | // This allows tests to use isolated keyring storage while still exercising the |
no test coverage detected