Skip to content
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

Refactor the gh.run to have a success and failure callback #764

Open
wd60622 opened this issue Dec 30, 2024 · 1 comment
Open

Refactor the gh.run to have a success and failure callback #764

wd60622 opened this issue Dec 30, 2024 · 1 comment

Comments

@wd60622
Copy link
Collaborator

wd60622 commented Dec 30, 2024

This pattern is so common that I think it might be worth a refactor to accommodate it.

The pattern:

local gh = require("octo.gh")
local utils = require("octo.utils")

local args = ...
gh.run {
  args = args,
  cb = function(stdout, stderr)
    if stderr and not utils.is_blank(stderr) then
      failure_case(stderr)
    elseif stdout then
      success_case(stdout)
    end
    -- Nothing happening here*
  end,
}

See cases here: https://github.com/search?q=repo%3Apwntester%2Focto.nvim+stderr%29&type=code

The refactor:

--- The new pattern
gh.run {
  args = args, 
  cb = {
    success = success_case,
    failure = failure_case,
  }
}
--- Maybe there is a default of utils.error if no failure case is provided

There are a few that do different checks on the stderr. Is that because they are doing something different or just
didn't know how to handle the cases.

Also fzf breaks this pattern and calls the callback after the if block

Thoughts here? @pwntester @GuillaumeLagrange and anyone else who has thoughts on this.

I think that this would make the code way more modular since the success cases tend to make pickers from the data.

@wd60622 wd60622 changed the title The Refactor the gh.run to have a success and failure callback Dec 30, 2024
@wd60622
Copy link
Collaborator Author

wd60622 commented Jan 23, 2025

Since the callback is only used with the async call, maybe a helper function would be best suited here.

-- In the gh module

M.create_cb = function(opts)
  opts = opts or {}

  if not opts.failure then
    opts.failure = utils.error
  end
  if not opts.success then
    opts.success = utils.info
  end

  return function(stdout, stderr)
    if stderr and not utils.is_blank(stderr) then
      opts.failure(stderr)
    elseif stdout then
      opts.success(stdout)
    end
  end
end

-- When using
gh.run {
  args = args,
  cb = gh.create_cb { success = success_case }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
@wd60622 and others