I am developing an web app with Go. So far so good, but now I am integrating Wercker as a CI tool and started caring about testing. But my app relies heavily on Cobra/Viper configuration/flags/environment_variables scheme, and I do not know how to properly init Viper values before running my test suite. Any help would be much appreciated.
2条回答 默认 最新
- dongxian6715 2016-03-06 14:08关注
When I use Cobra/Viper or any other combination of CLI helpers, my way of doing this is to have the CLI tool run a function whose sole purpose will be to get arguments and pass them to another method who will do the actual work.
Here is a short (and dumb) example using Cobra :
package main import ( "fmt" "os" "github.com/spf13/cobra" ) func main() { var Cmd = &cobra.Command{ Use: "boom", Short: "Explode all the things!", Run: Boom, } if err := Cmd.Execute(); err != nil { fmt.Println(err) os.Exit(-1) } } func Boom(cmd *cobra.Command, args []string) { boom(args...) } func boom(args ...string) { for _, arg := range args { println("boom " + arg) } }
Here, the
Boom
function is hard to test, but theboom
one is easy.You can see another (non-dumb) example of this here (and the correspond test here).
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥100 set_link_state
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度