climux

package module
Version: v0.0.0-...-37f2a4a Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2017 License: MIT Imports: 3 Imported by: 0

README

climux GoDoc

A mux for the cli in go

Install
go get github.com/fredr/climux
Test
go test -v
Use
package main

import (
	"fmt"

	"github.com/fredr/climux"
)

func main() {

	r := climux.NewRouter()
	r.HandleFunc("hello {name}", hello, "says hello to {name}")
	r.HandleFunc("hi {firstname} [lastname]", hi, "says hi to {firstname} with [lastname] if present")
	r.HandleFunc("help", help(r), "shows help")
	r.NotFoundHandler = help(r)

	climux.Handle(r)
}

func help(router *climux.Router) climux.Handler {
	return func(r *climux.Request) {
		fmt.Println("commands:")
		for _, route := range router.Routes {
			fmt.Println(route)
		}
	}
}

func hello(r *climux.Request) {
	fmt.Printf("Hello, %s!", r.Vars()["name"])
}

func hi(r *climux.Request) {
	if lastName, ok := r.Vars()["lastname"]; ok {
		fmt.Printf("Hi, %s %s!", r.Vars()["firstname"], lastName)
	} else {
		fmt.Printf("Hi, %s!", r.Vars()["firstname"])
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handle

func Handle(r *Router)

Handle builds a request and tries to find a matching route in the router

Types

type Handler

type Handler func(*Request)

type Request

type Request struct {
	// contains filtered or unexported fields
}

func (Request) String

func (r Request) String() string

func (Request) Vars

func (r Request) Vars() map[string]string

Vars returns all route variables from the request

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route holds the information to match against Requests

func (Route) String

func (r Route) String() string

type Router

type Router struct {
	Routes          []Route
	NotFoundHandler Handler
}

Router holds all routes

func NewRouter

func NewRouter() *Router

NewRouter returns a new Router instance

func (*Router) HandleFunc

func (r *Router) HandleFunc(path string, fn Handler, description string)

HandleFunc adds a Handler to a path

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL