Compare commits

...

4 Commits

Author SHA1 Message Date
Hazel Noack
5bce49eed4 implemented specific help 2025-07-16 12:08:53 +02:00
Hazel Noack
51b788bb50 finished general help 2025-07-16 11:45:28 +02:00
Hazel Noack
7f43eb43e0 added arguments data structure 2025-07-16 11:27:32 +02:00
Hazel Noack
81b960c231 implemented general help 2025-07-16 11:20:58 +02:00
4 changed files with 157 additions and 31 deletions

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.24.2
require github.com/labstack/echo/v4 v4.13.4
require (
github.com/TwiN/go-color v1.4.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
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/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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=

View File

@ -1,59 +1,101 @@
package cli
import (
"fmt"
"log"
"os"
"github.com/TwiN/go-color"
)
type ProgramFunction func() error
type Program struct {
Name string
Function ProgramFunction
ShortDescription string
LongDescription string
Arguments []Argument
}
type Argument struct {
Name string
Function ProgramFunction
Type string
Required bool
Description string
}
var HelpHeader = `Meow
Ze
Dong`
var HelpHeader = `This is the help page of transfem-startpage.
` + color.Purple + `transfem-startpage {program} {...args}` + color.Reset + `
The following Programs are available:`
var Programs = []Program{
{
Name: "help",
Function: Help,
Description: "get more information on how the cli or a program works",
Name: "help",
ShortDescription: "get more information on how the cli in general or a specific 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",
Function: Start,
Description: "start the webserver",
Name: "start",
Function: Start,
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",
Function: Cache,
Description: "do something with the cache",
Name: "cache",
Function: 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() {
fmt.Println("running transfem startpage")
// getting around initialization cycle
Programs[0].Function = Help
programName := "help"
if len(os.Args) > 1 {
programName = os.Args[1]
}
log.Println("running program", programName)
var selectedProgram *Program = nil
for i, p := range Programs {
if p.Name == programName {
selectedProgram = &Programs[i]
break
}
var selectedProgram Program = GetProgram(programName)
err := selectedProgram.Function()
if err != nil {
log.Panicln(err)
}
if selectedProgram == nil {
log.Panicln("couldn't find program", programName, ". EXITING")
}
selectedProgram.Function()
}

View File

@ -1,8 +1,89 @@
package cli
import "log"
import (
"fmt"
"os"
"strings"
func Help() error {
log.Println("running help")
"github.com/TwiN/go-color"
)
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
}
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()
}