appkit

package module
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2022 License: MIT Imports: 11 Imported by: 14

README

Appkit for Go applications

Very basic scaffolding library for Go applications.

Features:

  • Options datastore mostly for command line flags.
  • Version string generation.
  • Printing of licenses of the dependencies.

Usage

To use the features, have the following kind of main.go in the program

package main

//go:generate licrep -o licenses.go

import (
	"fmt"
	"os"
	"github.com/kopoli/appkit"
)

var (
	version     = "Undefined"
	timestamp   = "Undefined"
	buildGOOS   = "Undefined"
	buildGOARCH = "Undefined"
	progVersion = "" + version
)

// ...

func main() {

	opts := appkit.NewOptions()
	opts.Set("program-name", os.Args[0])
	opts.Set("program-version", progVersion)
	opts.Set("program-timestamp", timestamp)
	opts.Set("program-buildgoos", buildGOOS)
	opts.Set("program-buildgoarch", buildGOARCH)

	printVersion := true
	printLicenses := true

	if printVersion {
		fmt.Println(appkit.VersionString(opts))
		os.Exit(0)
	}

	if printLicenses {
		l, err := GetLicenses()
		// if err ...
		s, err := appkit.LicenseString(l)
		// if err ...
		fmt.Print(s)
		os.Exit(0)
	}
}

Install the dependencies:

$ go get github.com/kopoli/licrep
$ go get github.com/kopoli/gobu

Licrep generates the licenses.go. The reason for including licenses is that if one distributes only binaries, e.g. MIT license requires the license text is present.

Gobu builds the application and automatically fills the version information from git tags.

Therefore generate the application binary with:

$ go generate
$ gobu version

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasFlags added in v0.10.0

func HasFlags(fs *flag.FlagSet) bool

HasFlags returns true if a flag.FlagSet has any flags.

func JoinArguments added in v0.10.0

func JoinArguments(args []string) string

JoinArguments is the counter operation for SplitArguments. See that for more details.

func LicenseFormatString

func LicenseFormatString(licenses interface{}, format string, a ...interface{}) (string, error)

LicenseString returns a string of the licenses printed out in the given format.

The licenses is expected to be of the following kind of type:

map[string]struct {
  Name string
  Text string
}

The map key is the package name, Name is the license name, and Text is the license text.

The format string is similar to fmt's but with the following additional verbs: %Lp Package name %Ln License name %Lt License text

The licenses are printed in the alphabetical order of the package name.

As an implementation detail, https://github.com/kopoli/licrep generates a compatible map.

func LicenseString

func LicenseString(licenses interface{}) (string, error)

func OptionsHelp added in v0.12.0

func OptionsHelp(fs *flag.FlagSet) string

OptionsHelp is similar to flag.PrintDefaults, with the following differences: - Returns a string instead of printing to flagset.Output() - If one character flag has the same Usage as a longer one, combine them. - Use tabwriter

func SplitArguments added in v0.10.0

func SplitArguments(argstr string) []string

SplitArguments splits the arguments from the option "cmdline-args". See Parse for details.

func SplitCommand added in v0.10.0

func SplitCommand(cmdstr string) []string

SplitCommand splits a command string to a command and its synonyms. See NewCommand for more information.

func VersionString

func VersionString(opts Options) string

VersionString returns a version string that should be printed with the -v or the --version flag. It gets the components from the following keys from the options: program-name program-version program-timestamp

Types

type Command added in v0.10.0

type Command struct {
	Cmd []string

	// Help that describes command
	Help string
	// The sub-command portion of the Usage line in the help
	SubCommandHelp string
	// The argument portion of the Usage line in the help
	ArgumentHelp string

	Flags *flag.FlagSet
	// contains filtered or unexported fields
}

func NewCommand added in v0.10.0

func NewCommand(parent *Command, cmd string, help string) *Command

NewCommand creates a recursive command line argument with flags.

Parent of the top-level command should be nil. The cmd string can contain multiple space-separated commands that are regarded as synonyms of the command. The help string is displayed if help option is given.

If parent == nil, then the Usage function prints out all sub-commands with helps. This can be overridden by re-defining the Flags.Usage function.

Example:

opts := appkit.NewOptions()
base := appkit.NewCommand(nil, "", "")
optVersion := base.Flags.Bool("version", false, "Display version")
add := appkit.NewCommand(base, "add a", "Adding stuff")
_ = appkit.NewCommand(add, "package p", "Add package")
_ = appkit.NewCommand(add, "dependency d", "Add dependency")
del := appkit.NewCommand(base, "delete del d", "Deleting stuff")
_ = appkit.NewCommand(del, "package p", "Delete package")
_ = appkit.NewCommand(del, "dependency d", "Delete dependency")
optRecurse := del.Flags.Bool("recurse", false, "Delete recursively")

err = base.Parse(os.Args[1:], opts)
if err == flag.ErrHelp {
   os.Exit(0)
}

if *optVersion {
fmt.Println(appkit.VersionString(opts))
   os.Exit(0)
}
cmd := opts.Get("cmdline-command", "")
switch cmd {
case "add package":
...
case "delete package":
...
}

func (*Command) CommandList added in v0.10.0

func (c *Command) CommandList(out io.Writer)

CommandList prints out a recursive tree of sub-commands to the given io.Writer.

func (*Command) FullCommandName added in v0.11.0

func (c *Command) FullCommandName() string

FullCommandName returns the full (sub-)command as a string

func (*Command) HasSubcommands added in v0.11.0

func (c *Command) HasSubcommands() bool

HasSubcommands returns true if a command has any sub-commands defined.

func (*Command) IsTopLevel added in v0.11.0

func (c *Command) IsTopLevel() bool

IsTopLevel returns true if the command has no parents

func (*Command) Parse added in v0.10.0

func (c *Command) Parse(args []string, opts Options) error

Parse the command line arguments according to the recursive command structure.

The actual command will be set to the option "cmdline-command" inside the opts structure. Additional positional arguments after the command are in the "cmdline-args" option.

Recursive commands in "cmdline-command" are space separated. If there are multiple synonyms defined for a command, the first one is listed.

The positional arguments in "cmdline-args" are NUL separated inside the string. They can be split to an array using SplitArguments.

type Options

type Options interface {
	Set(key string, value string)
	Get(key string, fallback string) string
	IsSet(key string) bool
}

Options is an interface to get and set string-like options for components

func NewOptions

func NewOptions() Options

NewOptions returns a new Options structure

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL