Giter Site home page Giter Site logo

hofstadter-io / hofmod-cli Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 2.0 136 KB

Hofstadter generator for Golang CLIs

Home Page: https://github.com/hofstadter-io/hof

License: BSD 3-Clause "New" or "Revised" License

Go 71.57% CUE 25.83% Makefile 2.60%
cue cuelang hof-lang hof cli golang dsl code-generator

hofmod-cli's Introduction

hofmod-cli

A hof generator for creating advanced Golang CLIs.

Design your CLI structure, arguments, flags, and a whole host of addons and then generate the implementation. Changed your mind about what your CLI should look like? Redesign, regenerate, and keep adding you custom code.

Features:

  • Quickly architect your CLI commands, arguments, flags, configuration
  • Built on the fantastic [spf13/cobra](https://github.com/ andspf13/cobra)library for Golang CLIs
  • Cross-platform builds and releases using GoReleaser, GitHub, and Docker
  • Supports config files in local project falling back to OS specific application dir
  • Your CLI will self check for updates and can self install with a user command
  • Shell auto completion for bash, fish, zsh, and power shell
  • Advanced help system with support for custom overviews, extra topics, and examples
  • Telemetry systems which can hook up to Google Analytics
  • Golang pprof and many other ENV VARs to control inner behavior

Sites to see:

  • Schema - the design spec your write a CLI in
  • Generator - hof generator definition you invoke
  • Templates and partials - files which implement the code
  • Example - the hof tool leverages and powers this, see the hof.cue and design directory for relevant files

Usage

You'll need the hof tool installed. You can download hof from the releases page.

Let's start a new project:

# Start a project
hof init github.com/verdverm/my-cli
cd my-cli

Add the following to the cue.mods file (same format as go.mod)

module github.com/verdverm/my-cli

cue master

require (
    github.com/hofstadter-io/hofmod-cli v0.7.0
)

To fetch the module, run:

hof mod vendor cue

# and after the next file, run
hof gen

Create a file named cli.cue and add the following content:

package cli

import (
	"github.com/hofstadter-io/hofmod-cli/schema"

	"github.com/hofstadter-io/hof/design/cli/cmds"
)

# Typically we put the cli under a nested directory
#Outdir: "./cmd/hof"

#CLI: schema.#Cli & {
    # Name and package path (matches outdir)
	Name:    "hof"
	Package: "github.com/hofstadter-io/hof/cmd/hof"

    # Usage and help
	Usage:      "hof"
	Short:      "Polyglot Development Tool and Framework"
	Long:       Short
	CustomHelp: #RootCustomHelp

    # Print the help when no subcommands are supplied
	OmitRun: true

    # Command stage hooks
	PersistentPrerun:     true
    # You can write code here or...
	PersistentPrerunBody: "runtime.Init()"
 
	PersistentPostrun: true
    # ...or add custom code right in the output

    # Persistent flags work for all subcommands too
    Pflags: [{
		Name:    "labels"
		Long:    "label"
		Short:   "l"
		Type:    "[]string"
		Default: "nil"
		Help:    "Labels for use across all commands"
	}, {
		Name:    "config"
		Long:    "config"
		Short:   ""
		Type:    "string"
		Default: ""
		Help:    "Path to a hof configuration file"
	}, ...]

    # Subcommands and nested down as far as you need
	Commands: [

	]

	//
	// Addons
	//
	Releases: #CliReleases
	Updates:  true

    ...
}

(this was adapted from the [hof](https://github.com/hofstadter-io/hof t) tool)

Now run hof gen to generate your code. Try adding implementation, and then build:

go build -o my-cli cmd/my-cli/main.go

./my-cli

Update your designs, rerun hof gen, rebuild and keep iterating away!

hofmod-cli's People

Contributors

verdverm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mrunkel jornh

hofmod-cli's Issues

FlagPole updates

  • Docs strings, default to help & flag default
  • DeaultFlagPole func for when not using via CLI

always generates update and version subcommands

I'm explicitly setting the schema fields to false:

#CLI: schema.#Cli & {
	Name: "test"
	Package: "example.com/test/test/cmd/test"
	Short: "a test"

	CompletionCommands: false
	Updates: false
	
    OmitRun: false

	Pflags: [...schema.#Flag] & [{
		Name: "test"
		Long: "test"
		Short: "t"
		Type: "string"
		Default: ""
		Help: "test test test"
	}]

	// Commands: []
}

and while they are not added to the Cmd in root.go the actual commands still get generated:

cmd/test
├── cmd
│  ├── completions.go
│  ├── hls
│  │  └── cli
│  │     └── root
│  │        └── help.hls
│  ├── root.go
│  ├── root_test.go
│  ├── update.go
│  └── version.go
├── flags
│  └── root.go
├── main.go
└── verinfo
   └── verinfo.go

which makes the build fail like this:

building go files...
go build -o test ./cmd/test
go: finding module for package example.com/test/test/cmd/test/ga
cmd/test/cmd/completions.go:8:2: cannot find module providing package example.com/test/test/cmd/test/ga: module example.com/test/test/cmd/test/ga: git ls-remote -q origin in /home/user/go/pkg/mod/cache/vcs/BLAAAAAAAAAA: exit status 128:
	ERROR: Repository not found.
	fatal: Could not read from remote repository.
	
	Please make sure you have the correct access rights
	and the repository exists.
make: *** [Makefile:10: build] Error 1

generated "rest" args will panic when the extra arg is not present

This below was generated from, not sure why the rest is there? I think we use it to pass remaining args to the template, but where is the config? Did I put this in manually?

#CreateCommand: schema.#Command & {
	Name:  "create"
	Usage: "create <module location>"
	Short: "dynamic blueprints from any git repo"
	Long:  #CreateRootHelp

	Args: [{
		Name: "module"
		Type: "string"
		Required: false
		Help: "git repository or directory with a creator, accepts subdirs on both"
	}]

	Run: func(cmd *cobra.Command, args []string) {
		var err error

		// Argument Parsing

		var module string

		if 0 < len(args) {

			module = args[0]

		}

		err = CreateRun(module, args[1:])
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	},

Version command

add a default included version command that integrates with goreleaser variables

Manage multiple versions of the same application

  • install versions with suffix
  • copy current to suffixless
  • ? create a place to install all "vendor" tools

(vendor here means we should start adding a level of hierarchy for vendor/company -> tools / projects / configs / etc)

Make feedback work

  • implement feedback command
  • sending options
    • email
    • api
    • services?
  • capturing recent commands?
  • run editor are accept file as input

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.