Compare commits
4 Commits
26f1008dc4
...
5bce49eed4
Author | SHA1 | Date | |
---|---|---|---|
|
5bce49eed4 | ||
|
51b788bb50 | ||
|
7f43eb43e0 | ||
|
81b960c231 |
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.24.2
|
|||||||
require github.com/labstack/echo/v4 v4.13.4
|
require github.com/labstack/echo/v4 v4.13.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/TwiN/go-color v1.4.1 // indirect
|
||||||
github.com/labstack/gommon v0.4.2 // indirect
|
github.com/labstack/gommon v0.4.2 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -1,5 +1,7 @@
|
|||||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
|
github.com/TwiN/go-color v1.4.1 h1:mqG0P/KBgHKVqmtL5ye7K0/Gr4l6hTksPgTgMk3mUzc=
|
||||||
|
github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
|
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
|
||||||
|
@ -1,59 +1,101 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/TwiN/go-color"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProgramFunction func() error
|
type ProgramFunction func() error
|
||||||
type Program struct {
|
type Program struct {
|
||||||
Name string
|
Name string
|
||||||
Function ProgramFunction
|
Function ProgramFunction
|
||||||
|
ShortDescription string
|
||||||
|
LongDescription string
|
||||||
|
Arguments []Argument
|
||||||
|
}
|
||||||
|
type Argument struct {
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
Required bool
|
||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
var HelpHeader = `Meow
|
var HelpHeader = `This is the help page of transfem-startpage.
|
||||||
Ze
|
` + color.Purple + `transfem-startpage {program} {...args}` + color.Reset + `
|
||||||
Dong`
|
The following Programs are available:`
|
||||||
var Programs = []Program{
|
var Programs = []Program{
|
||||||
{
|
{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
Function: Help,
|
ShortDescription: "get more information on how the cli in general or a specific program works",
|
||||||
Description: "get more information on how the cli or a program works",
|
LongDescription: "What did you expect to find here?",
|
||||||
|
Arguments: []Argument{
|
||||||
|
{
|
||||||
|
Name: "program",
|
||||||
|
Type: "string",
|
||||||
|
Required: false,
|
||||||
|
Description: "defines the program you want to know more about",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "start",
|
Name: "start",
|
||||||
Function: Start,
|
Function: Start,
|
||||||
Description: "start the webserver",
|
ShortDescription: "start the webserver",
|
||||||
|
LongDescription: `The start program starts the webserver.
|
||||||
|
It loads the config file of the according profile.
|
||||||
|
It uses the default values if no config file was found.`,
|
||||||
|
Arguments: []Argument{
|
||||||
|
{
|
||||||
|
Name: "profile",
|
||||||
|
Type: "string",
|
||||||
|
Required: false,
|
||||||
|
Description: "tells the program which config to load, default is 'default'",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "cache",
|
Name: "cache",
|
||||||
Function: Cache,
|
Function: Cache,
|
||||||
Description: "do something with the cache",
|
ShortDescription: "do something with the cache",
|
||||||
|
LongDescription: `Does something with the cache.
|
||||||
|
- clear: delete the whole cache
|
||||||
|
- clean: delete all files that aren't used by any program.`,
|
||||||
|
Arguments: []Argument{
|
||||||
|
{
|
||||||
|
Name: "action",
|
||||||
|
Type: "enum(clear;clean)",
|
||||||
|
Required: true,
|
||||||
|
Description: "defines what to do with the cache",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetProgram(programName string) Program {
|
||||||
|
for i, p := range Programs {
|
||||||
|
if p.Name == programName {
|
||||||
|
return Programs[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Panicln("couldn't find program", programName, ". EXITING")
|
||||||
|
return Program{}
|
||||||
|
}
|
||||||
|
|
||||||
func Cli() {
|
func Cli() {
|
||||||
fmt.Println("running transfem startpage")
|
// getting around initialization cycle
|
||||||
|
Programs[0].Function = Help
|
||||||
|
|
||||||
programName := "help"
|
programName := "help"
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
programName = os.Args[1]
|
programName = os.Args[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("running program", programName)
|
var selectedProgram Program = GetProgram(programName)
|
||||||
|
err := selectedProgram.Function()
|
||||||
var selectedProgram *Program = nil
|
if err != nil {
|
||||||
for i, p := range Programs {
|
log.Panicln(err)
|
||||||
if p.Name == programName {
|
|
||||||
selectedProgram = &Programs[i]
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if selectedProgram == nil {
|
|
||||||
log.Panicln("couldn't find program", programName, ". EXITING")
|
|
||||||
}
|
|
||||||
|
|
||||||
selectedProgram.Function()
|
|
||||||
}
|
|
||||||
|
@ -1,8 +1,89 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
func Help() error {
|
"github.com/TwiN/go-color"
|
||||||
log.Println("running help")
|
)
|
||||||
|
|
||||||
|
func padString(s string, n int) string {
|
||||||
|
missing := n - len(s)
|
||||||
|
if missing <= 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ = range missing {
|
||||||
|
s = s + " "
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSingleArgumentString(a Argument) string {
|
||||||
|
requiredString := ""
|
||||||
|
if a.Required {
|
||||||
|
requiredString = "*"
|
||||||
|
}
|
||||||
|
return requiredString + a.Name + ":" + a.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func getArgumentString(arguments []Argument) string {
|
||||||
|
argumentString := color.Blue
|
||||||
|
for _, a := range arguments {
|
||||||
|
argumentString = argumentString + " [" + getSingleArgumentString(a) + "]"
|
||||||
|
}
|
||||||
|
return argumentString + color.Reset
|
||||||
|
}
|
||||||
|
|
||||||
|
func generalHelp() error {
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println(HelpHeader)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
for _, p := range Programs {
|
||||||
|
fmt.Print(color.Bold + padString(p.Name, 7) + color.Reset)
|
||||||
|
fmt.Print(padString(getArgumentString(p.Arguments), 40) + p.ShortDescription + "\n")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func specificHelp(programName string) error {
|
||||||
|
program := GetProgram(programName)
|
||||||
|
|
||||||
|
fmt.Println(color.Bold + "MAN PAGE FOR " + strings.ToUpper(programName) + color.Reset)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Println(color.Purple + "transfem-startpage " + programName + color.Reset + getArgumentString(program.Arguments))
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Println(color.Bold + "arguments" + color.Reset)
|
||||||
|
|
||||||
|
argumentStrings := make([]string, len(program.Arguments))
|
||||||
|
maxArgumentString := 0
|
||||||
|
for i, a := range program.Arguments {
|
||||||
|
s := getSingleArgumentString(a)
|
||||||
|
argumentStrings[i] = s
|
||||||
|
if len(s) > maxArgumentString {
|
||||||
|
maxArgumentString = len(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, a := range program.Arguments {
|
||||||
|
fmt.Println(padString(argumentStrings[i], maxArgumentString+4) + a.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println(program.LongDescription)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Help() error {
|
||||||
|
if len(os.Args) > 2 {
|
||||||
|
return specificHelp(os.Args[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
return generalHelp()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user