代码之家  ›  专栏  ›  技术社区  ›  Alex Forbes

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

  •  1
  • Alex Forbes  · 技术社区  · 7 年前

    我正在创建一个快速而脏的Go应用程序,以从Vault中提取应用程序机密,并使用Vault代码本身进行身份验证。作为其中的一部分,我将从导入aws凭据模块 github.com/hashicorp/vault/builtin/credential/aws . 这一切都很顺利。

    然而,在运行我的应用程序时,我注意到Go“testing”模块的命令行标志出现在标志中。

    这可以通过编译和运行以下示例脚本来复制:

    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()
    }
    

    调用二进制文件时,标志显示如下:

    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 ...]
    

    我是一个新手,所以我完全有可能在这里做一些我不应该做的事情,但乍一看,将此模块导入命令行工具似乎是合理的。

    据我所知,模块中没有任何内容使用测试库(backend\u test.go除外),因此我对这些标志的显示方式有点困惑,尤其是它们没有显示在Vault命令行界面本身中。

    是否可以在不包含这些标志的情况下导入和使用Vault的凭据/aws模块?或者在定义自己的测试标志之前,以某种方式清除测试标志?

    1 回复  |  直到 7 年前
        1
  •  3
  •   leaf bebop    7 年前

    那是因为即使你使用 _ 屏蔽 github.com/hashicorp/vault/builtin/credential/aws ,导入实际发生。和导入的包 testing ,生成所有这些标志。

    你可以摆脱 测试 使用新标志集标记。

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

    操场: https://play.golang.org/p/O8ibPn77L46