dtvjl64442 2017-03-09 01:00
浏览 658
已采纳

如何在yaml文件中动态处理UNIX / Windows目录分隔符

We facing a problem where we need to configure a path in one of our config-file.yml. It's a path to an CLI-Script. We facing an old known problem "directory separators" for UNIX/Windows - / - \ . Now we want to configure just one path for both OSs.

Currently we need to switch the path manually depending on the currently used OS. This breaks our VCS handling / application stability all the time.

Unix

bin_path: path/to/script

Windows

bin_path: path\to\script

Is there something like a placeholder known from PHP superglobal DIRECTORY_SEPARATOR? We focusing a solution where we not need to replace the separators in our scripts. I stuck in this problem for a couple of times when using yaml files.

  • 写回答

1条回答 默认 最新

  • duangai1368 2017-03-09 02:06
    关注

    YAML is a data description language and as such has no support for data flowing into the YAML document from the outside (i.e. variables you can use in the document). The only thing you can theoretically do is to structure your YAML document in a way that lets you select the value you actually want afterwards. Example:

    bin_path:
      unix: path/to/script
      windows: path\to\script
    

    Then, when loading the file, you can select the appropriate path for the current operating system. However, this seems convoluted compared to the approach where you simply replace / with \ when on Windows.


    An other solution is to just use one path configured for Windows or UNIX like in the following example. In this way you don't need to configure a specific path for each operation system.

    bin_path: path/to/script
    

    In e.g. PHP you could normalize your pass inside your application by using realpath.

    realpath('path/to/script'); //creates an absolute path including OS based directory separators
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部