It‘s a cmd-line tool like `make` and `task`, supporting nested args and alias using `cobra`

makex

It's a cmd-line tool like make and task, supporting nested options and alias using cobra.
With makex, we can easily execute nested commands, like makex cmd1, makex cmd1 cmd2, which you defined yourself.

Usage

you can run makex template init to generate makexfile(makex.yaml) in $pwd, then you can edit your own makex.yaml and run with makex, just like make in cobra style

For Example, given a following simple makexfile(makex.yaml)

cmds:
    - name: init
      cmd: echo "hello world: makex init"
      cmds:
          - name: pb
            cmd: echo "hello world: makex init pb"

you can run makex init in the same dir of your makex.yaml to execute init commands, which is echo "hello world: makex init". You can also run makex init pb to execute echo "hello world: makex init pb".
To get help info, you can run makex help init (makex init -h, makex init --help) to see help information. If init is an empty cmd, makex init will also print help info.

Normally, you can just type makex, makex help, makex -h, makex --help to get help info.

More cli usage, you can ask help for cobra doc.

Install

You can choose one of the three intsall methods.

# download release
curl -fsSL https://raw.githubusercontent.com/wymli/makex/master/install.sh | INSTALL_TYPE=release sh -
# download source code and build from scratch
curl -fsSL https://raw.githubusercontent.com/wymli/makex/master/install.sh | INSTALL_TYPE=build sh -
# use go install to download release to $GOPATH/bin, make sure you have set $GOPATH
go install github.com/wymli/makex@latest

Example

A makefile in makex, is named makex.yaml. We should place it in the root dir of your project.

asciicast

This is the makexfile used in the video.

interpreter: sh  # see https://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html

# user defined functions
udfs:
  - name: builtin   # this entry is just for promption, meaning we have `color` in builtin functions
    prompt: color
    used: false
  - name: genpb
    cmd: |
      genpb(){
        protoc -I $dir \
        --go_out $out_dir --go_opt paths=source_relative \
        --go-grpc_out $out_dir --go-grpc_opt paths=source_relative \
        *.proto
      }

# running with `makex init, makex tidy, makex userrpc build, makex userrpc build pb`
# run `makex help` will give you a list
cmds:
  - name: init
    cmd: |
      go mod init github.com/wymli/makex_example
  - name: tidy
    cmd: |
      go mod tidy
  - name: userrpc
    aliases: []
    usage: userrpc is abould user center rpc
    imports: []
    cmds:
      - name: build
        aliases: [gen]
        cmd: |
          cd user_rpc
          go build
          # go build user_rpc/main.go -o bin/user_rpc
        cmds:
          - name: pb
            imports: [genpb]
            cmd: |
              dir=.
              out_dir=.
              cd user_rpc/proto
              genpb
      - name: run
        cmd: |
          ./user_rpc/user_rpc

Execute logic

We organize all shell commands into one big temp shell file.
We first process imports in cmd

  • if the udf of imports has cmd field, we will copy udf.cmd to the shell file
  • if the udf of imports has load field, we will use . ${udf.load} to load file.

Then we copy cmd.cmd to shell file and run it using a shell interpreter(default sh).

You can see the whold tmp shell file if you running makex with -v flag

Makexfile(makex.yaml) Schema

package parser

type Makexfile struct {
	Interpreter string `yaml:"interpreter,omitempty"`
	Udfs        []UDF  `yaml:"udfs,omitempty"`
	Cmds        []Cmd  `yaml:"cmds,omitempty"`
}

type UDF struct {
	Name   string `yaml:"name,omitempty"`
	Prompt string `yaml:"prompt,omitempty"`
	Cmd    string `yaml:"cmd,omitempty"`
	Load   string `yaml:"load,omitempty"`
	Used   *bool  `yaml:"used,omitempty"`
}

type Cmd struct {
	Name    string   `yaml:"name,omitempty"`
	Aliases []string `yaml:"aliases,omitempty"`
	Usage   string   `yaml:"usage,omitempty"`
	Imports []string `yaml:"imports,omitempty"`
	Cmd     string   `yaml:"cmd,omitempty"`
	Cmds    []Cmd    `yaml:"cmds,omitempty"`
}

UDF

UDF can be seen as a kind of code snippets.

  • Name: used when imported in cmd (Cmd.Imports)
  • Prompt: useful information, like exported funtion name list
  • Cmd/Load: the payload, if cmd is not empty, we will exec cmd, otherwise will load shell file. cmd should be shell commands, and load should be shell file(relative to the makex.yaml)
  • Used: use or not

Cmd

Usage is just like cobra.

  • Name: cmd name
  • Aliases: cmd alias
  • Usage: info showed in help usage
  • Imports: using udf
  • Cmd: command to execute
  • Cmds: sub-commands

Shell

we use sh, not bash or other shell.

you can change interpreter in makex.yaml easily, but normally builtin shell function is coded using sh

a marked difference between different shell is that when showing color in echo, sh is just echo without -e, while bash needs echo -e

Config

you can configure you owm template on makexfile(makex.yaml), which is located at $HOME/.makex/makex_config.yaml.

  • do cat ~/.makex/makex_config.yaml for detail

you can store your own commonly used udf(code snippets) as builtin functions at $HOME/.makex/shell/

  • do ls ~/.makex/shell/ for detail
  • each udf is organized as a file
    • filename withoud ext is its import name
    • a file can contains as many functions as you want
    • we will load the whole file before exec cmd

Debug

you can run with -v to show debug logs. The contents of the assembled shell file will also be displayed.

Similar Resources

Gostall - Run go install ./cmd/server and not have the binary install in your GOBIN be called server?

GOSTALL Ever wanted to run go install ./cmd/server and not have the binary insta

Jan 7, 2022

Simple CLI using spf13/cobra and Flink REST API

Flinkctl Flinkctl is a simple CLI written on Go using Cobra created to facilitate my team's daily basis work with multiple Flink clusters at Azion Tec

Aug 16, 2022

Go-file-downloader-ftctl - A file downloader cli built using golang. Makes use of cobra for building the cli and go concurrent feature to download files.

ftctl This is a file downloader cli written in Golang which uses the concurrent feature of go to download files. The cli is built using cobra. How to

Jan 2, 2022

Todo-cmd: an app you can add your tasks , edit or delete them

TODO CMD APP! 🧙‍♂️ Table of contents General info Update Requirements set-up usage General info todo-cmd is an app you can add your tasks , edit or d

Dec 13, 2021

Reusable golang-migrate library using cobra utility

shift Reusable golang-migrate library using cobra utility Example Usage package main import ( "sql/db" "github.com/purwandi/shift" "github.com

Dec 16, 2021

CLI to run a docker image with R. CLI built using cobra library in go.

CLI  to run a docker image with R. CLI built using cobra library in go.

BlueBeak Installation Guide Task 1: Building the CLI The directory structure looks like Fastest process: 1)cd into bbtools 2)cd into bbtools/bin 3)I h

Dec 20, 2021

Cobra CLI tool to generate applications and commands

Cobra Generator Cobra provides its own program that will create your application and add any commands you want. It's the easiest way to incorporate Co

Jan 3, 2023

Bofin - A command line tool that can be used by to make Weblink development more productive

Bofin A command line tool that can be used by to make Weblink development more p

Jan 13, 2022
Go cmd utility that prints its command line arguments using strings.Join

Results This is an exercise of the book The Go Programming Language, by Alan A. A. Donovan and Brian Kernighan. Comparison between different versions

Dec 18, 2021
Command Line Alias Manager and Plugin System - Written in Golang
Command Line Alias Manager and Plugin System - Written in Golang

aly - Command Line Alias Manager and Packager Aly offers the simplest way to manage, share, and obtain command line aliases! Warning: This project is

Jun 16, 2022
A simple library to build golang command line (cli / cmd)apps

A simple library to build golang command line (cli / cmd)apps

Jan 11, 2022
ag is a tool for defining an alias for a group of commands

AG Introduction ag is a command line tool that similar to Makefile. with ag you can make an alias for group of commands with custom flags. This tool i

Jul 17, 2022
`tmax` is a powerful tool to help you get terminal cmd directly.
`tmax`  is a powerful tool to help you get terminal cmd directly.

The positioning of tmax is a command line tool with a little artificial intelligence. If you frequently deal with the terminal daily, tmax will greatly improve your work efficiency.

Oct 15, 2022
Blocking CMD shell interaction tool.

CliToolkit Blocking command line shell interaction tool. CliToolkit is a small cmd package for Go cmd shell interaction application. Installation # do

Oct 28, 2021
all-in-one cmd tool to search man page of different platform

Overview remote-man is an all-in-one cmd tool to search man page of different platform. support search platform Linux MacOS FreeBSD Installation compi

Oct 31, 2022
The blackbean is a command tool for elasticsearch operations by using cobra.
The blackbean is a command tool for elasticsearch operations by using cobra.

The blackbean is a command tool for elasticsearch operations by using cobra. Besides, blackbean is the name of my lovely French bulldog.

Mar 3, 2022
A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a proxy.
A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a proxy.

Fake-SMS A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a pr

Dec 31, 2022
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.

asciigraph Go package to make lightweight ASCII line graphs ╭┈╯. Installation go get github.com/guptarohit/asciigraph Usage Basic graph package main

Jan 8, 2023