dpj775835868 2018-01-02 14:20
浏览 67
已采纳

导入Vault / builtin / credential / aws会将测试标记添加到命令行应用程序

I'm creating a quick and dirty Go app to pull application secrets from Vault, and authenticating using the Vault code itself. As part of this, I'm importing the aws credential module from github.com/hashicorp/vault/builtin/credential/aws. This is all working well.

However when running my app, I notice that the command line flags from the Go "testing" module are appearing in the flags.

This can be reproduced by compiling and running the following example script:

package main

import (
    "flag"
    _ "github.com/hashicorp/vault/builtin/credential/aws"
    // Note: this import is masked only to make this demo script compile.
    // In my actual code I need to use it, and it is not masked.
)

var myFlag string

func main() {
    flag.StringVar(
        &myFlag, "myFlag", "", "Test flag",
    )
    flag.Parse()
    flag.Usage()
}

The flags appear like so when calling the binary:

Usage of poc:
  -myFlag string
        Test flag
  -test.bench regexp
        run only benchmarks matching regexp
  -test.benchmem
        print memory allocations for benchmarks
  -test.benchtime d
        run each benchmark for duration d (default 1s)
  -test.blockprofile file
        write a goroutine blocking profile to file
  -test.blockprofilerate rate
        set blocking profile rate (see runtime.SetBlockProfileRate) (default 1)
  -test.count n
        run tests and benchmarks n times (default 1)
  [... more flags from the go testing module ...]

I'm a newbie to Go, so it's entirely possible I'm doing something I'm not supposed to here, but at first glance it seems reasonable to import this module for a command-line tool.

As far as I can see, nothing within the module uses the testing library (other than backend_test.go), so I'm a bit confused as to how these flags are appearing, especially as they don't appear in the Vault command line interface itself.

Is it possible to import and use Vault's credential/aws module without including these flags? Or somehow clear the testing flags before defining my own?

  • 写回答

1条回答 默认 最新

  • duankang5285 2018-01-02 14:34
    关注

    That is because even though you use _ to mask github.com/hashicorp/vault/builtin/credential/aws, the import auctually happened. And the package imported testing, which generate all these flags.

    You can get rid of the testing flags by using a new FlagSet.

    func main() {
        f:=flag.NewFlagSet("Your app name",flag.ExitOnError)
        f.StringVar(
            &myFlag, "myFlag", "", "Test flag",
        )
        f.Parse(os.Args)
        f.Usage()
    }
    

    Playground: https://play.golang.org/p/O8ibPn77L46

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记