In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Golang how to package configuration files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.
Background
Recently, we are writing a CLI tool related to managing Aliyun's ECS. Of course, we should consider the security of Aliyun's resources. It requires that the AccessKeyId and AccessKeySecret of Aliyun's account cannot be distributed to users of the CLI tool.
So here we choose to package a configuration file containing AccessKeyId and AccessKeySecret into the CLI tool. Users of the CLI tool will use the packaged configuration file by default, but they can also use the new configuration information by specifying the configuration file or passing parameters.
Realize
Tools
Here we will introduce go-bindata, a library of Golang that can convert arbitrary files into Go code, which can be used to embed binaries into Go programs. At the same time, it also supports the use of gzip to compress file data before converting to the original byte slice.
For a specific description of the tool, please jump to https://github.com/go-bindata/go-bindata
Packing
Use the go-bindata tool to convert the configuration file that contains sensitive information into the source code of Go. The following is part of the project Makefile. The name of the tool is mycli.
NAME = mycliCONFIG = configs/config.yaml.PHONY: buildbuild: cp $(CONFIG) config.yaml mkdir-p cmd/mycli/asset go-bindata-pkg asset-o cmd/mycli/asset/asset.go\ scripts/...\ config.yaml CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build-o bin/linux/mycli cmd/mycli/*.go CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build-o bin/darwin/mycli cmd/mycli/*.go chmod + x. / bin/linux/mycli. / bin/darwin/mycli rm-f config.yaml mycli ln-s bin/linux/mycli mycli
The part that converts the file to Go source code is as follows:
Go-bindata-pkg asset-o cmd/mycli/asset/asset.go\ scripts/...\ config.yaml
Description of the options for the go-bindata command line tool:
-pkg specifies the package name, and the call will be written as asset.Asset ("config.yaml")
-o specify where the generated Go source code is stored
The generated asset.go code is as follows:
/ / Code generated by go-bindata.// sources:// scripts/create.sh// scripts/sub/delete.sh// config.yaml// DO NOT edited package assetfunc bindataRead (data [] byte, name string) ([] byte Error) {...} type asset struct {bytes [] byte info os.FileInfo} type bindataFileInfo struct {name string size int64 mode os.FileMode modTime time.Time} func (fi bindataFileInfo) Name () string {return fi.name} func (fi bindataFileInfo) Size () int64 {return fi.size} func (fi bindataFileInfo) Mode () os.FileMode {return fi.mode} func (fi bindataFileInfo) ModTime () time.Time {return fi.modTime} func (fi bindataFileInfo) IsDir () bool {return false} Func (fi bindataFileInfo) Sys () interface {} {return nil}... Call
Use the Asset method to load the packaged configuration file:
Const preloadConfigFile = "config.yaml" type Config struct {...} func PreloadConfig () (* Config, error) {b, err: = asset.Asset (preloadConfigFile) if err! = nil {return nil, fmt.Errorf ("failed to read config:% v", err)} var config * Config err = yaml.Unmarshal (b, & config) return config, err} Thank you for reading this article carefully I hope the article "how to package the configuration file for Golang" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.