uperl / File-XDG

Basic implementation of the XDG base directory specification
1 stars 0 forks source link

[Feature request] `lookup_config_file` to return all config files, not the first found one #27

Open van-de-bugger opened 1 month ago

van-de-bugger commented 1 month ago

lookup_config_file returns the first found config file. Sometimes such behavior is not convenient. For example, a config file ~/.config/app/app.conf may define an app configuration only partially, and global config file /etc/xdg/app/app.conf is still required to get settings, not defined in the ~/.config/app/app.conf. It would be nice if lookup_config_file returns not only the first found file, but all found files.

A possible implementation is trivial:

sub _lookup_file {
    my ($self, $type, @subpath) = @_;

    Carp::croak('subpath not specified') unless @subpath;
    Carp::croak("invalid type: $type") unless defined $self->{$type};

    my @dirs = ($self->{$type}, split(/\Q$Config{path_sep}\E/, $self->_dirs($type)));
    my @paths = map { $self->_file($_, @subpath) } @dirs;
    my ($match) = grep { -f $_ } @paths;

    return $match;
}

The two last lines of the function should be:

    my @match = grep { -f $_ } @paths;

    return wantarray ? @match : $match[ 0 ];

Alternatively, it could be a set of new functions like lookup_config_files or lookup_all_config_files, if you don't like context-sensitive functions.

plicease commented 1 month ago

I'm okay in theory with context-sensitive functions, but adding it here would be a breaking change. We should either have this behavior opt-in as a constructor arg (or new api version), or a new method name as you suggested. I'm leaning toward lookup_config_files and lookup_data_files. I think we should have both for consistency.