http-boot-config/config/main.go

112 lines
2.2 KiB
Go
Raw Normal View History

2023-08-13 14:20:55 +00:00
package main
2023-08-13 15:59:05 +00:00
import (
"errors"
"flag"
"io/fs"
2023-08-13 16:46:06 +00:00
"os"
2023-08-13 15:59:05 +00:00
"git.faercol.me/faercol/http-boot-config/config/logger"
"git.faercol.me/faercol/http-boot-config/config/prober"
)
2023-08-13 16:46:06 +00:00
type action int
const (
actionList action = iota
actionGetRemote
actionUnknown
)
2023-08-13 15:59:05 +00:00
type cliArgs struct {
debug bool
colour bool
2023-08-13 16:46:06 +00:00
action action
}
var defaultArgs cliArgs = cliArgs{
debug: false,
colour: true,
action: actionUnknown,
2023-08-13 15:59:05 +00:00
}
2023-08-13 16:46:06 +00:00
func parseArgs() (cliArgs, error) {
args := defaultArgs
var firstArg int
getRemoteFlagSet := flag.NewFlagSet("get-remote", flag.ExitOnError)
listFlagSet := flag.NewFlagSet("list", flag.ExitOnError)
2023-08-13 15:59:05 +00:00
debugFlag := flag.Bool("debug", false, "Display debug logs")
noColourFlag := flag.Bool("no-colour", false, "Disable colour logs")
2023-08-13 16:46:06 +00:00
for i, v := range os.Args {
switch v {
case "list":
args.action = actionList
case "get-remote":
args.action = actionGetRemote
default:
continue
}
firstArg = i + 1
}
2023-08-13 15:59:05 +00:00
2023-08-13 16:46:06 +00:00
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")
2023-08-13 15:59:05 +00:00
}
2023-08-13 16:46:06 +00:00
flag.Parse()
args.debug = *debugFlag
args.colour = !*noColourFlag
return args, nil
2023-08-13 15:59:05 +00:00
}
2023-08-13 16:46:06 +00:00
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())
}
2023-08-13 15:59:05 +00:00
l.Info("Found the following EFI applications:")
for _, a := range apps {
prefix := " "
if a.Active {
prefix = "*"
}
l.Infof("\t- %s[%d] %s: %s", prefix, a.ID, a.Name, a.Path)
}
}
2023-08-13 14:20:55 +00:00
2023-08-13 16:46:06 +00:00
func getRemoteConfig(l *logger.SimpleLogger) {
l.Info("Getting config from remote server...")
}
2023-08-13 14:20:55 +00:00
2023-08-13 16:46:06 +00:00
func main() {
args, err := parseArgs()
2023-08-13 15:59:05 +00:00
if err != nil {
2023-08-13 16:46:06 +00:00
l := logger.New(true, false)
l.Fatalf("Invalid command: %s", err.Error())
2023-08-13 15:59:05 +00:00
}
2023-08-13 16:46:06 +00:00
l := logger.New(args.colour, args.debug)
2023-08-13 14:20:55 +00:00
2023-08-13 16:46:06 +00:00
switch args.action {
case actionList:
displayAppList(l)
case actionGetRemote:
getRemoteConfig(l)
default:
l.Fatal("Unknown action")
}
2023-08-13 14:20:55 +00:00
}