我试图在程序中读取一个docker文件作为模板,并在发生新情况时替换了细节,但是当我使用dump函数时失败了。我的代码是这样的: let yaml = require('js-yaml'); let filePath = **/docker-compos-template.yaml'; let savePath = '**/docker-compose-save.yaml'; try{ let doc = yaml.safeLoad(fs.readFileSync(filePath),'utf8'); console.log(doc); fs.writeFileSync(savePath, yaml.dump(doc), 'utf8'); }catch(e) { console.log(e); }我的模板文件部分是: environment: - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_SERVER_CA_NAME=ca-santai - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default - FABRIC_CA_SERVER_TLS_ENABLED=true - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.santai.chembean.io-cert.pem转换后的文件为: environment: - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_SERVER_CA_NAME=ca-santai - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default - FABRIC_CA_SERVER_TLS_ENABLED=true - >- FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.santai.chembean.io-cert.pem模板中未定义符号>-。然后在翻译文件中为''。例如我的模板文件是:environment: - CORE_LEDGER_STATE_STATEDATABASE=CouchDB - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb-santai:5984 - CORE_PEER_ID=peer0.santai.chembean.io - CORE_PEER_NETWORKID=chembean - CORE_PEER_ADDRESS=peer0.santai.chembean.io:7051 - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.santai.chembean.io:7051 - CORE_PEER_ADDRESSAUTODETECT=true - CORE_PEER_CHAINCODEADDRESS=peer0.santai.chembean.io:7052 - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052 - CORE_CHAINCODE_STARTUPTIMEOUT=1200s - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.santai.chembean.io:7051 - CORE_PEER_LOCALMSPID=SantaiMSP - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock # the following setting starts chaincode containers on the same # bridge network as the peers # https://docs.docker.com/compose/networking/ - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default - CORE_LOGGING_LEVEL=INFO #- CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_TLS_ENABLED=true - CORE_PEER_GOSSIP_USELEADERELECTION=true - CORE_PEER_GOSSIP_ORGLEADER=false - CORE_PEER_PROFILE_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt但是转换文件是: environment: - CORE_LEDGER_STATE_STATEDATABASE=CouchDB - 'CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb-santai:5984' - CORE_PEER_ID=peer0.santai.chembean.io - CORE_PEER_NETWORKID=chembean - 'CORE_PEER_ADDRESS=peer0.santai.chembean.io:7051' - 'CORE_PEER_GOSSIP_BOOTSTRAP=peer0.santai.chembean.io:7051' - CORE_PEER_ADDRESSAUTODETECT=true - 'CORE_PEER_CHAINCODEADDRESS=peer0.santai.chembean.io:7052' - 'CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052' - CORE_CHAINCODE_STARTUPTIMEOUT=1200s - 'CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.santai.chembean.io:7051' - CORE_PEER_LOCALMSPID=SantaiMSP - 'CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock' - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default - CORE_LOGGING_LEVEL=INFO - CORE_PEER_TLS_ENABLED=true - CORE_PEER_GOSSIP_USELEADERELECTION=true - CORE_PEER_GOSSIP_ORGLEADER=false - CORE_PEER_PROFILE_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt似乎符号''随机出现。有什么好主意可以帮助我解决这些问题吗? 最佳答案 解析和转储yaml文件时可能会出现问题。如果您可以使用replace-in-files替换yaml文件中的某些字符串。只需将放在需要替换文本的位置并相应地放置它们。关于javascript - 翻译docker yaml文件时发生不规则符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54490209/
10-10 14:10