github.com/wangyougui/gf/v2@v2.6.5/os/gcmd/gcmd_z_unit_feature_object2_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcmd_test
    10  
    11  import (
    12  	"context"
    13  	"os"
    14  	"testing"
    15  
    16  	"github.com/wangyougui/gf/v2/frame/g"
    17  	"github.com/wangyougui/gf/v2/os/gcmd"
    18  	"github.com/wangyougui/gf/v2/os/gctx"
    19  	"github.com/wangyougui/gf/v2/test/gtest"
    20  	"github.com/wangyougui/gf/v2/util/gtag"
    21  )
    22  
    23  type commandBuild struct {
    24  	g.Meta               `name:"build" root:"build" args:"true" brief:"{commandBuildBrief}" dc:"{commandBuildDc}" eg:"{commandBuildEg}" ad:"{commandBuildAd}"`
    25  	nodeNameInConfigFile string // nodeNameInConfigFile is the node name for compiler configurations in configuration file.
    26  	packedGoFileName     string // packedGoFileName specifies the file name for packing common folders into one single go file.
    27  }
    28  
    29  const (
    30  	commandBuildBrief = `cross-building go project for lots of platforms`
    31  	commandBuildEg    = `
    32  gf build main.go
    33  gf build main.go --pack public,template
    34  gf build main.go --cgo
    35  gf build main.go -m none 
    36  gf build main.go -n my-app -a all -s all
    37  gf build main.go -n my-app -a amd64,386 -s linux -p .
    38  gf build main.go -n my-app -v 1.0 -a amd64,386 -s linux,windows,darwin -p ./docker/bin
    39  `
    40  	commandBuildDc = `
    41  The "build" command is most commonly used command, which is designed as a powerful wrapper for 
    42  "go build" command for convenience cross-compiling usage. 
    43  It provides much more features for building binary:
    44  1. Cross-Compiling for many platforms and architectures.
    45  2. Configuration file support for compiling.
    46  3. Build-In Variables.
    47  `
    48  	commandBuildAd = `
    49  PLATFORMS
    50      darwin    amd64,arm64
    51      freebsd   386,amd64,arm
    52      linux     386,amd64,arm,arm64,ppc64,ppc64le,mips,mipsle,mips64,mips64le
    53      netbsd    386,amd64,arm
    54      openbsd   386,amd64,arm
    55      windows   386,amd64
    56  `
    57  	// https://golang.google.cn/doc/install/source
    58  	commandBuildPlatforms = `
    59      darwin    amd64
    60      darwin    arm64
    61      ios       amd64
    62      ios       arm64
    63      freebsd   386
    64      freebsd   amd64
    65      freebsd   arm
    66      linux     386
    67      linux     amd64
    68      linux     arm
    69      linux     arm64
    70      linux     ppc64
    71      linux     ppc64le
    72      linux     mips
    73      linux     mipsle
    74      linux     mips64
    75      linux     mips64le
    76      netbsd    386
    77      netbsd    amd64
    78      netbsd    arm
    79      openbsd   386
    80      openbsd   amd64
    81      openbsd   arm
    82      windows   386
    83      windows   amd64
    84  	android   arm
    85  	dragonfly amd64
    86  	plan9     386
    87  	plan9     amd64
    88  	solaris   amd64
    89  `
    90  	commandBuildBriefPack = `
    91  destination file path for packed file. if extension of the filename is ".go" and "-n" option is given, 
    92  it enables packing SRC to go file, or else it packs SRC into a binary file.
    93  
    94  `
    95  	commandGenDaoBriefJsonCase = `
    96  generated json tag case for model struct, cases are as follows:
    97  | Case            | Example            |
    98  |---------------- |--------------------|
    99  | Camel           | AnyKindOfString    | 
   100  | CamelLower      | anyKindOfString    | default
   101  | Snake           | any_kind_of_string |
   102  | SnakeScreaming  | ANY_KIND_OF_STRING |
   103  | SnakeFirstUpper | rgb_code_md5       |
   104  | Kebab           | any-kind-of-string |
   105  | KebabScreaming  | ANY-KIND-OF-STRING |
   106  `
   107  )
   108  
   109  func init() {
   110  	gtag.Sets(map[string]string{
   111  		`commandBuildBrief`:          commandBuildBrief,
   112  		`commandBuildDc`:             commandBuildDc,
   113  		`commandBuildEg`:             commandBuildEg,
   114  		`commandBuildAd`:             commandBuildAd,
   115  		`commandBuildBriefPack`:      commandBuildBriefPack,
   116  		`commandGenDaoBriefJsonCase`: commandGenDaoBriefJsonCase,
   117  	})
   118  }
   119  
   120  type commandBuildInput struct {
   121  	g.Meta   `name:"build" config:"gfcli.build"`
   122  	Name     string `short:"n" name:"name"     brief:"output binary name"`
   123  	Version  string `short:"v" name:"version"  brief:"output binary version"`
   124  	Arch     string `short:"a" name:"arch"     brief:"output binary architecture, multiple arch separated with ','"`
   125  	System   string `short:"s" name:"system"   brief:"output binary system, multiple os separated with ','"`
   126  	Output   string `short:"o" name:"output"   brief:"output binary path, used when building single binary file"`
   127  	Path     string `short:"p" name:"path"     brief:"output binary directory path, default is './bin'" d:"./bin"`
   128  	Extra    string `short:"e" name:"extra"    brief:"extra custom \"go build\" options"`
   129  	Mod      string `short:"m" name:"mod"      brief:"like \"-mod\" option of \"go build\", use \"-m none\" to disable go module"`
   130  	Cgo      bool   `short:"c" name:"cgo"      brief:"enable or disable cgo feature, it's disabled in default" orphan:"true"`
   131  	JsonCase string `short:"j" name:"jsonCase" brief:"{commandGenDaoBriefJsonCase}" d:"CamelLower"`
   132  	Pack     string `name:"pack" brief:"{commandBuildBriefPack}"`
   133  }
   134  
   135  type commandBuildOutput struct{}
   136  
   137  func (c commandBuild) Index(ctx context.Context, in commandBuildInput) (out *commandBuildOutput, err error) {
   138  	return
   139  }
   140  
   141  func TestNewFromObject(t *testing.T) {
   142  	gtest.C(t, func(t *gtest.T) {
   143  		var (
   144  			ctx = gctx.New()
   145  		)
   146  		cmd, err := gcmd.NewFromObject(commandBuild{
   147  			nodeNameInConfigFile: "gfcli.build",
   148  			packedGoFileName:     "build_pack_data.go",
   149  		})
   150  		t.AssertNil(err)
   151  
   152  		os.Args = []string{"build", "-h"}
   153  		err = cmd.RunWithError(ctx)
   154  		t.AssertNil(err)
   155  	})
   156  }