Compare commits

..

No commits in common. "5bce49eed46c440df64201307dcf418b8825f04e" and "26f1008dc448ba041f3b2e4a397ac96990905be1" have entirely different histories.

4 changed files with 31 additions and 157 deletions

1
go.mod
View File

@ -5,7 +5,6 @@ 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
View File

@ -1,7 +1,5 @@
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=

View File

@ -1,101 +1,59 @@
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 = `This is the help page of transfem-startpage. var HelpHeader = `Meow
` + color.Purple + `transfem-startpage {program} {...args}` + color.Reset + ` Ze
The following Programs are available:` Dong`
var Programs = []Program{ var Programs = []Program{
{ {
Name: "help", Name: "help",
ShortDescription: "get more information on how the cli in general or a specific program works", Function: Help,
LongDescription: "What did you expect to find here?", Description: "get more information on how the cli or a program works",
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,
ShortDescription: "start the webserver", Description: "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,
ShortDescription: "do something with the cache", Description: "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() {
// getting around initialization cycle fmt.Println("running transfem startpage")
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]
} }
var selectedProgram Program = GetProgram(programName) log.Println("running program", programName)
err := selectedProgram.Function()
if err != nil { var selectedProgram *Program = nil
log.Panicln(err) for i, p := range Programs {
if p.Name == programName {
selectedProgram = &Programs[i]
break
} }
} }
if selectedProgram == nil {
log.Panicln("couldn't find program", programName, ". EXITING")
}
selectedProgram.Function()
}

View File

@ -1,89 +1,8 @@
package cli package cli
import ( import "log"
"fmt"
"os"
"strings"
"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 { func Help() error {
if len(os.Args) > 2 { log.Println("running help")
return specificHelp(os.Args[2]) return nil
}
return generalHelp()
} }