fortify: clean up config loading
All checks were successful
Tests / Go tests (push) Successful in 40s
Nix / NixOS tests (push) Successful in 3m28s

Move duplicate code to function. Also handle - as config from stdin.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2024-12-23 17:57:54 +09:00
parent c109ac2653
commit 70bffeaa1e
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 88 additions and 61 deletions

71
main.go
View File

@ -2,7 +2,6 @@ package main
import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"os/user"
@ -129,64 +128,21 @@ func main() {
// Ignore errors; set is set for ExitOnError.
_ = set.Parse(args[1:])
if len(set.Args()) != 1 {
fmsg.Fatal("show requires 1 argument")
}
likePrefix := false
if len(set.Args()[0]) <= 32 {
likePrefix = true
for _, c := range set.Args()[0] {
if c >= '0' && c <= '9' {
continue
}
if c >= 'a' && c <= 'f' {
continue
}
likePrefix = false
break
}
}
var (
config *fst.Config
instance *state.State
name string
)
// try to match from state store
if likePrefix && len(set.Args()[0]) >= 8 {
fmsg.VPrintln("argument looks like prefix")
s := state.NewMulti(os.Paths().RunDirPath)
if entries, err := state.Join(s); err != nil {
fmsg.Printf("cannot join store: %v", err)
// drop to fetch from file
} else {
for id := range entries {
v := id.String()
if strings.HasPrefix(v, set.Args()[0]) {
// match, use config from this state entry
instance = entries[id]
config = instance.Config
break
}
fmsg.VPrintf("instance %s skipped", v)
}
}
if len(set.Args()) != 1 {
fmsg.Fatal("show requires 1 argument")
} else {
name = set.Args()[0]
config, instance = tryShort(name)
}
if config == nil {
fmsg.VPrintf("reading from file")
config = new(fst.Config)
if f, err := os.Open(set.Args()[0]); err != nil {
fmsg.Fatalf("cannot access config file %q: %s", set.Args()[0], err)
panic("unreachable")
} else if err = json.NewDecoder(f).Decode(&config); err != nil {
fmsg.Fatalf("cannot parse config file %q: %s", set.Args()[0], err)
panic("unreachable")
}
config = tryPath(name)
}
printShow(instance, config, short)
@ -196,20 +152,13 @@ func main() {
fmsg.Fatal("app requires at least 1 argument")
}
config := new(fst.Config)
if f, err := os.Open(args[1]); err != nil {
fmsg.Fatalf("cannot access config file %q: %s", args[1], err)
panic("unreachable")
} else if err = json.NewDecoder(f).Decode(&config); err != nil {
fmsg.Fatalf("cannot parse config file %q: %s", args[1], err)
panic("unreachable")
}
// append extra args
// config extraArgs...
config := tryPath(args[1])
config.Command = append(config.Command, args[2:]...)
// invoke app
runApp(config)
panic("unreachable")
case "run": // run app in permissive defaults usage pattern
set := flag.NewFlagSet("run", flag.ExitOnError)

78
parse.go Normal file
View File

@ -0,0 +1,78 @@
package main
import (
"encoding/json"
"io"
direct "os"
"strings"
"git.gensokyo.uk/security/fortify/fst"
"git.gensokyo.uk/security/fortify/internal/fmsg"
"git.gensokyo.uk/security/fortify/internal/state"
)
func tryPath(name string) (config *fst.Config) {
var r io.Reader
config = new(fst.Config)
if name != "-" {
if f, err := os.Open(name); err != nil {
fmsg.Fatalf("cannot access configuration file %q: %s", name, err)
panic("unreachable")
} else {
// finalizer closes f
r = f
}
} else {
r = direct.Stdin
}
if err := json.NewDecoder(r).Decode(&config); err != nil {
fmsg.Fatalf("cannot load configuration: %v", err)
panic("unreachable")
}
return
}
func tryShort(name string) (config *fst.Config, instance *state.State) {
likePrefix := false
if len(name) <= 32 {
likePrefix = true
for _, c := range name {
if c >= '0' && c <= '9' {
continue
}
if c >= 'a' && c <= 'f' {
continue
}
likePrefix = false
break
}
}
// try to match from state store
if likePrefix && len(name) >= 8 {
fmsg.VPrintln("argument looks like prefix")
s := state.NewMulti(os.Paths().RunDirPath)
if entries, err := state.Join(s); err != nil {
fmsg.Printf("cannot join store: %v", err)
// drop to fetch from file
} else {
for id := range entries {
v := id.String()
if strings.HasPrefix(v, name) {
// match, use config from this state entry
instance = entries[id]
config = instance.Config
break
}
fmsg.VPrintf("instance %s skipped", v)
}
}
}
return
}