-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplumber.R
42 lines (33 loc) · 1.01 KB
/
plumber.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# plumber.R
library(plumber)
library(here)
library(stringr)
# Startup functions -------------------------------------------------------
clean_ag_output <- function(string) {
match <- str_extract(string,':[0-9]*$')
name <- str_remove(string, match) %>% str_remove('^:')
value <- str_remove(match,':') %>% as.integer()
data.frame(File = name, Count = value)
}
# /search -----------------------------------------------------------------
#* Return the sum of two numbers
#* @param repo The repo to search
#* @param word The word to find
#* @get /search
#* @serializer csv
function(repo, word) {
print(glue::glue('{repo} / {word}'))
# change dir so we don't print the filepath
setwd(here('clones',repo))
# This is very ugly, but ag returns exit 1 on match-not-found
suppressWarnings(
system2('ag', c('--ackmate','-c', word, '.'), stdout = TRUE)
) -> res
if (length(res) == 0) {
tibble::tibble(File='name',Count=0,.rows=0)
} else {
res %>%
purrr::map_dfr(clean_ag_output) %>%
dplyr::arrange(-Count)
}
}