Skip to content

Commit

Permalink
redirect from neovim, updates to health to go zoom zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Dec 14, 2023
1 parent 35bcd8e commit 9554203
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.2.7"
version = "1.0.0"
edition = "2021"

[package]
Expand Down
4 changes: 0 additions & 4 deletions lua/sg/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ M.download = function()
-- so we need to vim.wait for them.
vim.notify "[sg] Starting to download binaries..."

-- TODO: Windows
-- Check that we have curl
-- Check what to do to zip

local curl = system({ "curl", link, "-L", "-o", tarfile }):wait()
if curl.code ~= 0 then
error("Failed to execute downloading release" .. vim.inspect(curl))
Expand Down
17 changes: 12 additions & 5 deletions lua/sg/health.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- Just make sure request has started
require("sg.request").start()

local M = {}

local blocking = require("sg.utils").blocking
Expand Down Expand Up @@ -57,8 +60,8 @@ local report_env = function()

local creds = auth.get()
if not creds then
vim.health.error "No valid auth strategy detected. See `:help sg` for more info."
ok = false
vim.health.error "No valid auth strategy detected. See ':help sg' for more info."
return false
else
assert(creds, "must have valid credentials")

Expand All @@ -67,9 +70,9 @@ local report_env = function()
end

local err, info = blocking(require("sg.rpc").get_info)
info = info or {}
local expected_cargo_version = require "sg.private.cargo_version"
if err then
if err or not info then
ok = false
elseif expected_cargo_version ~= info.sg_nvim_version then
vim.health.error "Mismatched cargo and expected version. Update using :SourcegraphDownloadBinaries or :SourcegraphBuild"
vim.health.error(string.format("Exptected: %s | Found: %s", expected_cargo_version, info.sg_nvim_version))
Expand Down Expand Up @@ -144,8 +147,12 @@ local report_agent = function()
end

local report_cody_account = function()
if not require("sg.auth").get() then
vim.health.error "Cannot check Cody Status, not logged in"
return false
end
local err, user_info = blocking(require("sg.rpc").get_user_info)
if err then
if err or not user_info then
vim.health.error(string.format("Cody Auth Failed: %s", vim.inspect(err)))
return false
end
Expand Down
20 changes: 18 additions & 2 deletions plugin/sg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ vim.api.nvim_create_user_command("SourcegraphLogin", function(command)

if endpoint == "https://sourcegraph.com/" then
local port = 52068
-- TODO: Change to NEOVIM when that goes live
local editor = "JETBRAINS"
local editor = "NEOVIM"
local redirect = string.format("user/settings/tokens/new/callback?requestFrom=%s-%s", editor, port)

require("sg.rpc").dotcom_login(port, function(err, _)
Expand All @@ -84,6 +83,23 @@ end, {
bang = true,
})

---@command SourcegraphClear [[
--- Remove Sourcegraph Login information
---@command ]]
vim.api.nvim_create_user_command("SourcegraphClear", function()
return require("sg.rpc").get_auth({
clear = true,
}, function(err)
if err then
vim.notify(string.format("[cody] Failed to update auth: %s", vim.inspect(err)))
else
vim.notify "[cody] Cleared Sourcegraph Auth Information"
end
end)
end, {
desc = "Clears stored sourcegraph credentials",
})

---@command SourcegraphBuild [[
--- Rebuild the Sourcegraph crates and required dependencies (in case build failed during installation)
---@command ]]
Expand Down
2 changes: 1 addition & 1 deletion src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
std::sync::Mutex,
};

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct CodyCredentials {
pub endpoint: Option<String>,
pub token: Option<String>,
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub async fn get_file_contents(remote: &str, commit: &str, path: &str) -> Result
}

pub async fn get_sourcegraph_version() -> Result<SourcegraphVersion> {
auth::get_access_token().ok_or(anyhow::anyhow!("No user token. Login first"))?;

wrap_request!(sg_gql::sourcegraph_version, Variables {})
}

Expand Down Expand Up @@ -250,5 +252,9 @@ pub async fn get_search(query: String) -> Result<Vec<SearchResult>> {
}

pub async fn get_user_info() -> Result<UserInfo> {
wrap_request!(sg_gql::user, Variables {})
let token = auth::get_access_token();
match token {
Some(_) => wrap_request!(sg_gql::user, Variables {}),
None => Err(anyhow::anyhow!("No user information. Must log in first")),
}
}
23 changes: 16 additions & 7 deletions src/nvim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub enum RequestData {
SourcegraphAuth {
endpoint: Option<String>,
token: Option<SecretString>,
clear: bool,
},

#[serde(rename = "sourcegraph/dotcom_login")]
Expand Down Expand Up @@ -362,15 +363,23 @@ impl Request {
ResponseData::SourcegraphUserInfo(user_info),
))
}
RequestData::SourcegraphAuth { endpoint, token } => {
RequestData::SourcegraphAuth {
endpoint,
token,
clear,
} => {
use crate::auth;

let credentials = CodyCredentials {
endpoint,
token: token.map(|t| t.0),
};
if credentials.token.is_some() || credentials.endpoint.is_some() {
auth::set_credentials(credentials)?;
if clear {
auth::set_credentials(CodyCredentials::default())?;
} else {
let credentials = CodyCredentials {
endpoint,
token: token.map(|t| t.0),
};
if credentials.token.is_some() || credentials.endpoint.is_some() {
auth::set_credentials(credentials)?;
}
}

Ok(Response::new(
Expand Down

0 comments on commit 9554203

Please sign in to comment.