add basic command parser
This commit is contained in:
parent
1383272d2c
commit
136a90db19
1 changed files with 74 additions and 17 deletions
|
@ -4,29 +4,80 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.faercol.me/faercol/http-boot-config/config/logger"
|
"git.faercol.me/faercol/http-boot-config/config/logger"
|
||||||
"git.faercol.me/faercol/http-boot-config/config/prober"
|
"git.faercol.me/faercol/http-boot-config/config/prober"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type action int
|
||||||
|
|
||||||
|
const (
|
||||||
|
actionList action = iota
|
||||||
|
actionGetRemote
|
||||||
|
actionUnknown
|
||||||
|
)
|
||||||
|
|
||||||
type cliArgs struct {
|
type cliArgs struct {
|
||||||
debug bool
|
debug bool
|
||||||
colour bool
|
colour bool
|
||||||
|
action action
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseArgs() cliArgs {
|
var defaultArgs cliArgs = cliArgs{
|
||||||
|
debug: false,
|
||||||
|
colour: true,
|
||||||
|
action: actionUnknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseArgs() (cliArgs, error) {
|
||||||
|
args := defaultArgs
|
||||||
|
var firstArg int
|
||||||
|
|
||||||
|
getRemoteFlagSet := flag.NewFlagSet("get-remote", flag.ExitOnError)
|
||||||
|
listFlagSet := flag.NewFlagSet("list", flag.ExitOnError)
|
||||||
|
|
||||||
debugFlag := flag.Bool("debug", false, "Display debug logs")
|
debugFlag := flag.Bool("debug", false, "Display debug logs")
|
||||||
noColourFlag := flag.Bool("no-colour", false, "Disable colour logs")
|
noColourFlag := flag.Bool("no-colour", false, "Disable colour logs")
|
||||||
|
|
||||||
flag.Parse()
|
for i, v := range os.Args {
|
||||||
|
switch v {
|
||||||
return cliArgs{
|
case "list":
|
||||||
debug: *debugFlag,
|
args.action = actionList
|
||||||
colour: !*noColourFlag,
|
case "get-remote":
|
||||||
|
args.action = actionGetRemote
|
||||||
|
default:
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
firstArg = i + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
switch args.action {
|
||||||
|
case actionList:
|
||||||
|
listFlagSet.Parse(os.Args[firstArg:])
|
||||||
|
case actionGetRemote:
|
||||||
|
getRemoteFlagSet.Parse(os.Args[firstArg:])
|
||||||
|
default:
|
||||||
|
flag.Parse()
|
||||||
|
return cliArgs{}, errors.New("missing an action")
|
||||||
|
}
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
args.debug = *debugFlag
|
||||||
|
args.colour = !*noColourFlag
|
||||||
|
return args, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayAppList(apps []prober.EfiApp, l *logger.SimpleLogger) {
|
func displayAppList(l *logger.SimpleLogger) {
|
||||||
|
l.Info("Checking EFI directory for available boot images...")
|
||||||
|
apps, err := prober.GetEFIApps(l)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, fs.ErrPermission) {
|
||||||
|
l.Fatal("Permission error, try to run the command as sudo")
|
||||||
|
}
|
||||||
|
l.Fatalf("Failed to check EFI directory: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
l.Info("Found the following EFI applications:")
|
l.Info("Found the following EFI applications:")
|
||||||
for _, a := range apps {
|
for _, a := range apps {
|
||||||
prefix := " "
|
prefix := " "
|
||||||
|
@ -37,18 +88,24 @@ func displayAppList(apps []prober.EfiApp, l *logger.SimpleLogger) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRemoteConfig(l *logger.SimpleLogger) {
|
||||||
|
l.Info("Getting config from remote server...")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := parseArgs()
|
args, err := parseArgs()
|
||||||
|
if err != nil {
|
||||||
|
l := logger.New(true, false)
|
||||||
|
l.Fatalf("Invalid command: %s", err.Error())
|
||||||
|
}
|
||||||
l := logger.New(args.colour, args.debug)
|
l := logger.New(args.colour, args.debug)
|
||||||
|
|
||||||
l.Info("Checking EFI directory for available boot images...")
|
switch args.action {
|
||||||
images, err := prober.GetEFIApps(l)
|
case actionList:
|
||||||
if err != nil {
|
displayAppList(l)
|
||||||
if errors.Is(err, fs.ErrPermission) {
|
case actionGetRemote:
|
||||||
l.Fatal("Permission error, try to run the command as sudo")
|
getRemoteConfig(l)
|
||||||
|
default:
|
||||||
|
l.Fatal("Unknown action")
|
||||||
}
|
}
|
||||||
l.Fatalf("Failed to check EFI directory: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
displayAppList(images, l)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue