bgo: an extensible go program building tool

bgo v0.3.0+

preface

We are already bgo: make it easier to build go programs The basic capabilities of bgo are introduced in.

After several days of iteration, I initially felt that it was relatively stable, so I issued version 0.3.0 as a cut before the Spring Festival.

The new version is available

After the festival, thanks to cmdr Upgrade of the original alias function (there are many accidents on New Year's Eve, all caused by rush), bgo A small step towards more than just a main package batch Builder:

We provide pre built check code quanlities through Aliases.

This is v0 New features after 3.1 +.

Check code qualities function

This function requires third-party tools. At present, golint and gocyclo are required to be present, and gofmt tools are also required. The first two need to be installed in advance:

go install golang.org/x/lint/golint
go install github.com/fzipp/gocyclo

When the above tools are effective, bgo can perform the quality inspection function provided by the above tools:

bgo check-code-quanlities
bgo chk
# Or
bgo chk ./...

This command executes gofmt, golint and gocyclo tools in turn to check the code quality of your current folder and its subordinates.

You can attach parameters such as. / Or other specified folder offset.

Homebrew

The brew version has pre built installation script. You need to update hedzr/brew Tap and reinstall bgo.

Since brew updates itself and all taps through the update command, its efficiency may be too low. I think an alternative is:

brew untap hedzr/brew
brew tap hedzr/brew

brew will now install the preset configuration file to / usr/local/etc/bgo /.

Later, we will introduce what other environments should do.

text

The functions mentioned above depend on cmdr Alias capabilities. Therefore, it is necessary for us to explain this.

Installation of bgo new version

First of all, bgo's release package now carries an etc/bgo subdirectory, which you need to move to $home / bgo Directory:

wget https://github.com/hedzr/bgo/releases/download/v0.3.3/bgo-darwin-amd64.tgz
tar -xf bgo-darwin-amd64.tgz
mv bin/bgo /usr/local/bin/bgo
mv etc/bgo ~/.bgo

# If you use the zsh environment, regenerate the autocomplete script
bgo gen sh --zsh

Now at $home / The BGO directory contains the definition of the pre built aliases command.

Specifically, it ($HOME/.bgo/conf.d/80.aliases.yml) is as follows:

app:

  aliases:
    # group:                                  # group-name (optional). such as: "alias"
    commands:
      # - title: list
      #   short-name: ls
      #   # aliases: []
      #   # name: ""
      #   invoke-sh: ls -la -G                # for macOS, -G = --color; for linux: -G = --no-group
      #   # invoke: "another cmdr command"
      #   # invoke-proc: "..." # same with invoke-sh
      #   desc: list the current directory

      - title: check-code-qualities
        short-name: chk
        # aliases: [check]
        # name: ""
        # group: ""
        # hidden: false
        invoke-sh: |
          echo "Command hit: { {.Cmd.GetDottedNamePath}}"
          echo "fmt { {.ArgsString}}"
          gofmt -l -s -w { {range .Args}}{ {.}}{ {end}}
          echo "lint { {.ArgsString}}"
          golint { {.ArgsString}}
          echo "cyclo ."
          gocyclo -top 20 .
        # invoke: "another cmdr command"
        # invoke-proc: "..." # same with invoke-sh
        shell: /usr/bin/env bash  # optional, default is /bin/bash
        desc: pre-options before releasing. typically fmt,lint,cyclo,...

I guess I don't need any additional explanation.

At the time of official release, this file has more command alias definitions

Here is a set of expansion mechanism (through shell script fragments). You can continue to expand your own specific commands, and further simplify various tasks before and after construction through bgo's command system.

In the yaml code above, uncomment the map where title: list is located to enable a list command, which is a synonym for ls. Of course, it has no practical significance. Its purpose is to explain to you how to add another command (or another n commands).

Establish multi-level sub command system

Configuration file attached to cmdr 91.cmd-aliases.yml It shows you how to build your own sub command system through alias capabilities:

app:

  aliases:
    group:                                  # group-name (optional). such as: "alias"
    commands:
      - title: list
        short-name: ls
        # aliases: []
        # name: ""
        invoke-sh: ls -la -G                # for macOS, -G = --color; for linux: -G = --no-group
        # invoke: "another cmdr command"
        # invoke-proc: "..." # same with invoke-sh
        desc: list the current directory
      - title: pwd
        invoke-sh: pwd
        desc: print the current directory
      - title: services
        desc: "the service commands and options"
        subcmds:
          - title: ls
            invoke: /server/list            # invoke a command from the command tree in this app
            invoke-proc:                    # invoke the external commands (via: executable)
            invoke-sh:                      # invoke the external commands (via: shell)
            shell: /bin/bash                # or /usr/bin/env bash|zsh|...
            desc: list the services
          - title: start
            flags: []
            desc: start a service
          - title: stop
            flags: []
            desc: stop a service
          - title: git-version
            invoke-proc: git describe --tags --abbrev=0
            desc: print the git version
            group: Proc
          - title: git-revision
            invoke-proc: git rev-parse --short HEAD
            desc: print the git revision
            group: Proc
          - title: kx1
            invoke: /kb
            desc: invoke /kb command
            group: Internal
          - title: kx2
            invoke: ../.././//kb --size 32mb   # allow related path to seek a cmdr-command, and the ugly path is allowed (multiple slashes, ...)
            desc: invoke /kb command
            group: Internal
          - title: kx3
            invoke: /kb --size 2kb
            desc: invoke /kb command
            group: Internal
        flags:
          - title: name
            default: noname
            type: string          # bool, string, duration, int, uint, ...
            group:
            toggle-group:
            desc: specify the name of a service

Not only that, you can even provide exclusive flag options for subcommands.

Use Flag option

Because the fields of invoke, invoke proc and invoke sh support template expansion, you can extract the actual value of a flag through the template syntax. The actual value refers to the actual value entered by the user on the command line or the default value of the flag.

The aliases definition of an example can be as follows:

      - title: echo
        invoke-sh: |
          # pwd
          echo "{ {$flg := index .Cmd.Flags 0}}{ {$dpath :=$flg.GetDottedNamePath}} { {$fullpath := .Store.Wrap $dpath}} { {$fullpath}} | { {.Store.GetString $fullpath}}"
        desc: print the name
        flags:
          - title: name
            default:              # default value
            type: string          # bool, string, duration, int, uint, ...
            group:
            toggle-group:
            desc: specify the name to be printed

This template string sequence is almost equivalent to the Golang Code:

flg := obj.Cmd.Flags[0]
dpath := flg.GetDottedNamePath()
fullpath := obj.Store.Wrap(dpath)
stringVal := obj.Store.GetString(dpath) // Extract the actual value of the option from the cmdr Option Store in the form of string

The template obtains the hit subcommand echo from the context variable environment obj, that is, obj Cmd; Similarly, obj Store obtains an instance of the cmdr Option Store, followed by a series of cmdr interface calls, so as to obtain the actual value of -- name set in the Option Store after the cmdr processes the command line parameters.

The effect of its operation is similar to:

$ bgo echo --name watching
  app.echo.name | watching

The complete path of echo subcommand is app Echo and app are implicit prefixes. cmdr uses such implicit prefixes to build a namespace, while store Wrap () is to append such a prefix to a general path.

Update your autocomplete script

After expanding your Aliases, you need to update the autocomplete script to reflect the changes. Yes, Aliases are fully integrated into the bgo command system, so regenerate the autocomplete script once to include Aliases in the autocomplete prompt list.

A schematic diagram is shown above.

Section

With the above information, you can expand bgo without modifying the bgo source code.

You can try:

  • Add cov (coverage) command to do coverage test briefly and quickly
  • bench
  • Add pre script for resource file packaging
  • wait

In the new version, the coverage command definition has been included in the distribution package. You may wish to compare it.

Or browse the configuration file definition in repo directly ci/etc/bgo/conf.d/80.aliases.yml

What bgo wants to do is shorten the repeated and lengthy command line to the level of one or two subcommands, so that each work is within 6-8 keystrokes. This is much more useful than making full use of zsh's automatic completion and page up and page down functions.

In fact, alias has the ability of expanding the program, and now it has the ability of integration.

In fact, we haven't introduced the Extensions feature supported by cmdr, which allows you to store a series of folders and script files in specific folders and integrate them into the existing command system, just as the alias feature above does with the help of YAML configuration file.

The external extension capabilities provided by Cmdr, such as Extensions, Aliases and Addons, are mainly inspired by git aliases and the features of some DOS applications before the Internet.

Personally reproducing the effect in my memory must be the best reward for a developer. It doesn't matter if I stay in my boudoir. I've got the reward I want.

Digression

I've always wanted to reproduce a LIST. I just haven't had the energy to do it.

LIST.COM is DOS shareware developed by the late Vernon D. Buerg. This tool is one of the powerful tools when I do reverse TWAY. Well, it doesn't do reverse personally, it's just a file Lister.

About bgo configuration file

Support multiple formats

Also from the support provided by cmdr, bgo can automatically recognize json, toml and yml (or yaml) suffixes.

In other words, in a working directory, there are bgo.yml or bgo.toml is OK. Although we always use yaml in our example to show you how to write it, changing it to a different file format can work equally. BGO or cmdr will load them automatically.

Configuration folder

You've noticed that we're at $home / BGO is provided under BGO YML, and the subdirectory conf.d provides 80 aliases. yml.

According to the Convention of cmdr, there must be a configuration file with the same name (i.e. bgo) in a configuration file directory, and the suffix should be json. toml. yml. One of yaml. The configuration files of cmdr are divided into three types: primary, secondary and alternative.

For bgo, $home / bgo is a possible location of the primary configuration file group. bgo and cmdr will automatically search several predefined locations (including / usr/local/etc/bgo and $HOME/.bgo) to try to load the primary configuration file, then use its conf.d as the monitoring folder and load any of them json.toml.yml.yaml .

Therefore, it doesn't matter to mix different configuration file formats.

conf.d auto configuration folder

In the directory of the main configuration file, the conf.d subdirectory is monitored, and any configuration file in it can be automatically overloaded. Of course, automatic overloading is meaningless for bgo.

However, you can add a series of configuration files in this folder, such as alias-001 yml, alias-002. yml. This structure will be very convenient for ci operation or devops operation. You don't need to rack your brains to arrange the script. If you want to add a large yaml fragment to a yaml, you also need to ensure that their embedding position and indentation level are appropriate. Just add one YML.

Auxiliary profile

As for the current working directory bgo.yml is loaded as an Alternative/Secondary configuration file group.

According to the Convention of cmdr, if bgo. If YML is loaded as an Alternative configuration file, the configuration file does not contain subfolder monitoring, and the automatic configuration file writeback function can be enabled. However, this function is not turned on in BGO, because the writeback function will erase the comments in the configuration file, which may not be what you want.

The reason why the identity is uncertain is that cmdr supports loading three groups of configurations from primary / secondary / alternative locations at the same time and merging them together. Although we have not specifically stated, bgo has many possibilities for expansion here.

Use a different format when bgo init

Different configuration file formats can be established using the following syntax:

bgo init --output=.bgo.yml
bgo init --output=.bgo.yaml
bgo init --output=.bgo.json
bgo init --output=.bgo.toml

The suffix of the output file name determines which configuration file format the initialization operation will store the scanning results in.

Postscript

Current problems of bgo:

  • There is no powerful test under Windows system, so it is uncertain whether the loading location of the configuration file can be effectively identified.
  • The bash shell in the configuration file may be changed to powershell or cmd in windows? I don't know. Maybe it needs a little adjustment to work.

    • FEEL FREE TO ISSUE ME

Last time I said, this time I suddenly thought and did it directly. Then I went to have a look goxc, ouch, I'll go

Look, people just stopped watching.

Suddenly feel the future meow.

Why do I think go build cross compilation is not so simple or troublesome. Alas, anyway, bgo is such a tool.

REFs

🔚

Keywords: Go compiler build

Added by kenrbnsn on Wed, 02 Feb 2022 17:28:56 +0200