驗證toml配置文件合法性

背景

咱們知道如今主流的配置文件格式有xml,json,toml,yaml,這裏我以toml爲例,由於咱們項目用的是toml格式的配置文件。有一次線上發佈代碼的時候因爲沒有驗證toml配置文件的合法性,配置推上去後,程序沒有正常啓動,致使服務不可用。linux

思考

出現這個失誤以後,我認爲應該能夠在發佈代碼以前,先提早驗證下配置文件的合法性,避免出現低級的人爲失誤,
把錯誤的配置文件推到線上去,因而想找能夠驗證toml配置文件的合法性的工具。git

方案

這裏須要安裝golang,假設在linux環境下
  1. 獲取源碼github

    go get github.com/BurntSushi/tomlgolang

  2. 進到目錄github.com/BurntSushi/tomlcmd/tomlv 下,進行編譯

    go build .json

  3. 而後把tomlv放到/bin目錄下。
  4. 驗證toml配置文件合法性,用法以下:

    tomlv some-toml-file.tomlide

若是錯誤會有輸出,好比工具

Error in 'config.conf': Near line 1 (last key parsed 'service'): expected a top-level item to end with a newline, comment, or EOF, but got ']' instead

而後咱們能夠根據這個命令的返回狀態來判斷是否驗證成功,0 成功 ,1 失敗。ui

  1. 還有查看toml的配置項的各個類型:

    tomlv -types some-toml-file.tomlcode

結果以下:
驗證toml配置文件合法性xml

相關文章
相關標籤/搜索