【问题标题】:How can I better handle config files?如何更好地处理配置文件?
【发布时间】:2019-06-05 12:01:56
【问题描述】:

在我正在编写的一个包中,我有一个如下所示的配置模块:

use v6.d;
use JSON::Fast;
use PSBot::Tools;

sub EXPORT(--> Hash) {
    my Str $path = do if %*ENV<TESTING> {
        $*REPO.Str.IO.child('META6.json').e
            ?? $*REPO.Str.IO.child('t/config.json').Str         # For when testing using zef
            !! $*REPO.Str.IO.parent.child('t/config.json').Str; # For when testing using prove
    } elsif $*DISTRO.is-win {
        "%*ENV<LOCALAPPDATA>\\PSBot\\config.json"
    } else {
        "$*HOME/.config/PSBot/config.json"
    };

    unless $path.IO.e {
        note "PSBot config at $path does not exist!";
        note "Copy psbot.json.example there and read the README for instructions on how to set up the config file.";
        exit 1;
    }

    with from-json slurp $path -> %config {
        %(
            USERNAME               => %config<username>,
            PASSWORD               => %config<password>,
            AVATAR                 => %config<avatar>,
            HOST                   => %config<host>,
            PORT                   => %config<port>,
            SERVERID               => %config<serverid>,
            COMMAND                => %config<command>,
            ROOMS                  => set(%config<rooms>.map: &to-roomid),
            ADMINS                 => set(%config<admins>.map: &to-id),
            MAX_RECONNECT_ATTEMPTS => %config<max_reconnect_attempts>,
            GIT                    => %config<git>,
            DICTIONARY_API_ID      => %config<dictionary_api_id>,
            DICTIONARY_API_KEY     => %config<dictionary_api_key>,
            YOUTUBE_API_KEY        => %config<youtube_api_key>,
            TRANSLATE_API_KEY      => %config<translate_api_key>
        )
    }
}

每次我对配置文件进行更改时,我都必须删除 precomp 文件才能使更改出现。有没有办法我可以这样写,以便在编译时不定义导出,这样用户就不必这样做?

【问题讨论】:

  • 我最初的想法是,您会导出关闭由 INIT 移相器初始化的 %config 的潜艇?
  • 任何时候你发现自己想要使用 repo 的文件路径来做某事都可能是错误的。
  • 您考虑过no precompilation 移相器吗?这将使它在每次程序启动时重新编译,并且使用这个模块的其他模块可能也不会被预编译,但这个脚本应该运行得足够快,我认为
  • 我不知道移相器的存在!
  • .oO (Pragmatically 说,我提到 timotimo 与precompilation 相关的小错误很可能phase 那些不明白我为什么要评论它的人,并让那些这样做的人为我糟糕的幽默感而呻吟,所以我想我最好还是给自己听)

标签: config raku


【解决方案1】:

假设我正确理解您的意图,一种方法是:

  1. 去掉EXPORT
  2. $path%config 的计算放到模块的主线中
  3. 将您的“常量”声明为诸如

    sub term:<USERNAME> is export { %config<username> }
    

【讨论】:

  • 只有在编译单元中只使用此模块一次时,这才有效。如果您想更加灵活,可以基于以下代码实现一些东西:sub EXPORT($path = "something") { { PATH =&gt; $path } },然后使用位置参数 use Foo &lt;bar&gt;" (which would set PATH` 将模块加载到该范围内的“bar”),或加载模块(在另一个范围内)使用use Foo,在这种情况下,PATH 将是该范围内的“某物”。
【解决方案2】:

在阅读了你们的 cmets 和@Christoph 的答案后,我决定了这一点。这就是我想做的:

use v6.d;
use JSON::Fast;
use PSBot::Tools;
unit module PSBot::Config;

my Str $path = do if %*ENV<TESTING> {
    %?RESOURCES<test/config.json>.Str
} elsif $*DISTRO.is-win {
    Qh[%*ENV<LOCALAPPDATA>\PSBot\config.json]
} else {
    "$*HOME/.config/PSBot/config.json"
};

unless $path.IO.e {
    note "PSBot config at $path does not exist!";
    note "Copy config.json.example there and read the README for instructions on how to set up the config file.";
    exit 1;
}

my %config = from-json slurp $path;

sub term:<USERNAME>               is export  { %config<username>                   }
sub term:<PASSWORD>               is export  { %config<password>                   }
sub term:<AVATAR>                 is export  { %config<avatar>                     }
sub term:<HOST>                   is export  { %config<host>                       }
sub term:<PORT>                   is export  { %config<port>                       }
sub term:<SERVERID>               is export  { %config<serverid>                   }
sub term:<COMMAND>                is export  { %config<command>                    }
sub term:<ROOMS>                  is export  { set(%config<rooms>.map: &to-roomid) }
sub term:<ADMINS>                 is export  { set(%config<admins>.map: &to-id)    }
sub term:<MAX_RECONNECT_ATTEMPTS> is export  { %config<max_reconnect_attempts>     }
sub term:<GIT>                    is export  { %config<git>                        }
sub term:<DICTIONARY_API_ID>      is export  { %config<dictionary_api_id>          }
sub term:<DICTIONARY_API_KEY>     is export  { %config<dictionary_api_key>         }
sub term:<YOUTUBE_API_KEY>        is export  { %config<youtube_api_key>            }
sub term:<TRANSLATE_API_KEY>      is export  { %config<translate_api_key>          }

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2010-12-09
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多