-
Notifications
You must be signed in to change notification settings - Fork 112
add: fpm search #1054
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
base: main
Are you sure you want to change the base?
add: fpm search #1054
Changes from 53 commits
7d5a99d
06d353c
94fdf25
0a4f910
7a4bdb5
cc9bd57
0a99d4a
b048451
0b66b85
d2b173a
6d5c44b
354fda1
baed1f5
3456c39
384ef93
ea021d3
7dbd3f7
11c05cb
762b78e
08f0171
511a107
a87b472
5f3a18d
334eeb5
a0dbf40
494194f
5eb04a5
034051d
373155c
18c3975
606c20a
e9822d9
c1216f0
fc62c32
ced681e
52b0d79
0ca0eae
4a4c6ce
8800925
b02bd72
e05e51d
469051f
c1b0c18
aa1112b
8496b5c
4beb573
5c259a4
1c86fdf
4a1975f
0f668a5
dad1f49
c3f27ba
81ea75b
805f1e5
a62162a
ad779ec
2d088aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,178 @@ | ||||||
!> Search a package from both local and remote registry using the `search` subcommand. | ||||||
!> The package can be searched by packagename, namespace, query (description and README.md), and license from the registries (local and remote). | ||||||
!> the remote registry URL can also be specified by the paramter --registry. | ||||||
!> It can be used as `fpm search --query q* --page 1 --registry URL --namespace n* --package p* --package_version v* --license l* --limit 10 --sort-by [name] --sort [asc/desc]` | ||||||
module fpm_cmd_search | ||||||
use fpm_command_line, only: fpm_search_settings | ||||||
use fpm_manifest, only: package_config_t, get_package_data | ||||||
use fpm_model, only: fpm_model_t | ||||||
use fpm_error, only: error_t, fpm_stop | ||||||
use fpm_versioning, only: version_t | ||||||
use fpm_filesystem, only: exists, join_path, get_temp_filename, delete_file, basename, & | ||||||
canon_path, dirname, list_files, is_hidden_file | ||||||
use fpm_git, only: git_archive | ||||||
use fpm_downloader, only: downloader_t | ||||||
use fpm_strings, only: string_t, string_array_contains, split, str,glob | ||||||
use fpm, only: build_model | ||||||
use fpm_error, only : error_t, fatal_error, fpm_stop | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate import of fpm_error module. The module is already imported on line 9 with error_t and fpm_stop. Consider consolidating these imports. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
use jonquil, only : json_object | ||||||
use tomlf, only : toml_array, get_value, len, toml_key, toml_loads, toml_table, & | ||||||
toml_serializer,toml_value | ||||||
use tomlf_utils_io, only : read_whole_file | ||||||
use fpm_settings, only: fpm_global_settings, get_global_settings, official_registry_base_url | ||||||
|
||||||
implicit none | ||||||
private | ||||||
public :: cmd_search | ||||||
|
||||||
contains | ||||||
|
||||||
!> Search the fpm registry for a package | ||||||
subroutine cmd_search(settings) | ||||||
!> Settings for the search command. | ||||||
class(fpm_search_settings), intent(in) :: settings | ||||||
type(fpm_global_settings) :: global_settings | ||||||
character(:), allocatable :: tmp_file, name, namespace, description, query_url, package_version | ||||||
integer :: stat, unit, ii | ||||||
type(json_object) :: json | ||||||
type(json_object), pointer :: p | ||||||
!> Error handling. | ||||||
type(error_t), allocatable :: error | ||||||
type(toml_array), pointer :: array | ||||||
type(version_t), allocatable :: version | ||||||
|
||||||
!> Downloader instance. | ||||||
class(downloader_t), allocatable :: downloader | ||||||
allocate (downloader) | ||||||
|
||||||
call get_global_settings(global_settings, error) | ||||||
if (allocated(error)) then | ||||||
call fpm_stop(1, "Error retrieving global settings"); return | ||||||
end if | ||||||
|
||||||
!> Generate a temporary file to store the downloaded package search data | ||||||
tmp_file = get_temp_filename() | ||||||
open (newunit=unit, file=tmp_file, action='readwrite', iostat=stat) | ||||||
if (stat /= 0) then | ||||||
call fatal_error(error, "Error creating temporary file for downloading package."); return | ||||||
end if | ||||||
query_url = settings%registry//'/packages_cli' & | ||||||
& // '?query='//settings%query & | ||||||
& // '&page='//settings%page & | ||||||
& // '&package='//settings%package & | ||||||
& // '&namespace='//settings%namespace & | ||||||
& // '&version='//settings%version & | ||||||
& // '&license='//settings%license & | ||||||
& // '&limit='//settings%limit & | ||||||
& // '&sort_by='//settings%sort_by & | ||||||
& // '&sort='//settings%sort | ||||||
|
||||||
!> Get the package data from the registry | ||||||
call downloader%get_pkg_data(query_url, version, tmp_file, json, error) | ||||||
close (unit) | ||||||
if (allocated(error)) then | ||||||
call fpm_stop(1, "Error retrieving package data from registry: "//settings%registry); return | ||||||
end if | ||||||
print * | ||||||
print *, "Searching packages in Local Registry:" | ||||||
print * | ||||||
call search_package(settings%query, settings%namespace, settings%package, settings%version) | ||||||
if (json%has_key("packages")) then | ||||||
call get_value(json, 'packages', array) | ||||||
print * | ||||||
print '(A,I0,A)', ' Found ', len(array), ' packages in fpm - registry:' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent spacing: should be 'fpm-registry' (no spaces around hyphen).
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
print * | ||||||
do ii=1, len(array) | ||||||
call get_value(array, ii, p) | ||||||
call get_value(p, 'name', name) | ||||||
call get_value(p, 'namespace', namespace) | ||||||
call get_value(p, 'description', description) | ||||||
call get_value(p, 'version', package_version) | ||||||
|
||||||
print *, "Name: ", name | ||||||
perazz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
print *, "namespace: ", namespace | ||||||
print *, "Description: ", description | ||||||
print *, "version: ", package_version | ||||||
print * | ||||||
end do | ||||||
else | ||||||
call fpm_stop(1, "Invalid package data returned"); return | ||||||
end if | ||||||
end subroutine cmd_search | ||||||
|
||||||
subroutine search_package(query,namespace,package,version) | ||||||
type(fpm_global_settings) :: global_settings | ||||||
type(error_t), allocatable :: error | ||||||
character(:), allocatable, intent(in) :: namespace, package, version, query | ||||||
character(:), allocatable :: path, array(:), versioncheck(:), toml_package_data, print_package(:) | ||||||
character(:), allocatable :: description, wild | ||||||
type(string_t), allocatable :: file_names(:) | ||||||
type(toml_table), allocatable :: table | ||||||
integer :: i, j, unit, stat | ||||||
logical :: result | ||||||
|
||||||
call get_global_settings(global_settings, error) | ||||||
if (allocated(error)) then | ||||||
call fpm_stop(1, "Error retrieving global settings"); return | ||||||
end if | ||||||
|
||||||
path = global_settings%registry_settings%cache_path | ||||||
|
||||||
! Scan directory for packages | ||||||
call list_files(path, file_names,recurse=.true.) | ||||||
do i=1,size(file_names) | ||||||
result = package_search_wild(namespace,package,version,file_names(i)%s) | ||||||
call split(file_names(i)%s,array,'/') | ||||||
if (result) then | ||||||
!> query search for description | ||||||
call read_whole_file(file_names(i)%s, toml_package_data, stat) | ||||||
if (stat /= 0) then | ||||||
call fatal_error(error, "Error reading file: "//file_names(i)%s); return | ||||||
end if | ||||||
! Load TOML data into a table | ||||||
call toml_loads(table,toml_package_data) | ||||||
if (allocated(error)) then | ||||||
call fpm_stop(1, "Error loading toml file"); return | ||||||
end if | ||||||
|
||||||
if (allocated(table)) then | ||||||
call get_value(table, 'description', description) | ||||||
if (query /="") then | ||||||
result = glob(description,query) | ||||||
if (result) call print_package_data(array,description) | ||||||
else | ||||||
call print_package_data(array,description) | ||||||
end if | ||||||
else | ||||||
call fpm_stop(1, "Error Searching for the query"); return | ||||||
end if | ||||||
endif | ||||||
end do | ||||||
end subroutine search_package | ||||||
|
||||||
function package_search_wild(namespace,package,version, file_name) result(result) | ||||||
character(:), allocatable, intent(in) :: namespace, package, version, file_name | ||||||
character(:), allocatable :: array(:), versioncheck(:) | ||||||
logical :: result | ||||||
|
||||||
call split(file_name,array,'/') | ||||||
call split(array(size(array)-1),versioncheck,'.') | ||||||
result = array(size(array)) == "fpm.toml" | ||||||
result = result .and. glob(array(size(array)-3),namespace) | ||||||
result = result .and. glob(array(size(array)-2),package) | ||||||
result = result .and. glob(array(size(array)-1),version) | ||||||
result = result .and. size(versioncheck) > 2 | ||||||
end function package_search_wild | ||||||
|
||||||
subroutine print_package_data(package_array,description) | ||||||
character(:), allocatable, intent(in) :: package_array(:) | ||||||
character(*), intent(in) :: description | ||||||
|
||||||
print *, "Name: ", package_array(size(package_array)-2) | ||||||
print *, "Namespace: ", package_array(size(package_array)-3) | ||||||
print *, "Version: ", package_array(size(package_array)-1) | ||||||
print *, "Description: ", description | ||||||
print * | ||||||
end subroutine print_package_data | ||||||
end | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multiple issues: 'packagename' should be 'package name', 'paramter' should be 'parameter', and the second line should start with a capital letter.
Copilot uses AI. Check for mistakes.