cli #1

Merged
Hazel merged 13 commits from cli into main 2025-07-16 14:35:52 +00:00
2 changed files with 77 additions and 31 deletions
Showing only changes of commit 5bce49eed4 - Show all commits

View File

@ -1,7 +1,6 @@
package cli
import (
"fmt"
"log"
"os"
@ -13,6 +12,7 @@ type Program struct {
Name string
Function ProgramFunction
ShortDescription string
LongDescription string
Arguments []Argument
}
type Argument struct {
@ -23,12 +23,13 @@ type Argument struct {
}
var HelpHeader = `This is the help page of transfem-startpage.
` + color.Bold + `transfem-startpage {program} {...args}` + color.Reset + `
` + color.Purple + `transfem-startpage {program} {...args}` + color.Reset + `
The following Programs are available:`
var Programs = []Program{
{
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",
@ -42,6 +43,9 @@ var Programs = []Program{
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",
@ -55,6 +59,9 @@ var Programs = []Program{
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",
@ -66,9 +73,18 @@ var Programs = []Program{
},
}
func Cli() {
fmt.Println("running transfem startpage")
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() {
// getting around initialization cycle
Programs[0].Function = Help
@ -77,18 +93,9 @@ func Cli() {
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

@ -2,7 +2,8 @@ package cli
import (
"fmt"
"log"
"os"
"strings"
"github.com/TwiN/go-color"
)
@ -20,6 +21,22 @@ func padString(s string, n int) string {
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)
@ -27,24 +44,46 @@ func generalHelp() error {
for _, p := range Programs {
fmt.Print(color.Bold + padString(p.Name, 7) + color.Reset)
argumentString := color.Purple
for _, a := range p.Arguments {
requiredString := ""
if a.Required {
requiredString = "*"
}
argumentString = argumentString + " [" + requiredString + a.Name + ":" + a.Type + "]"
}
argumentString = argumentString + color.Reset
fmt.Print(padString(argumentString, 40) + p.ShortDescription + "\n")
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 {
log.Println("running help")
if len(os.Args) > 2 {
return specificHelp(os.Args[2])
}
return generalHelp()
}