Skip to content

Commit

Permalink
list and listconfigs commands
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 24, 2024
1 parent 843d403 commit 083bbe7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
73 changes: 72 additions & 1 deletion cmd/uimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"log"
"log/slog"
"os"
"slices"

"github.com/u-root/gobusybox/src/pkg/bb/findpkg"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uimage"
"github.com/u-root/mkuimage/uimage/mkuimage"
"github.com/u-root/uio/cli"
"github.com/u-root/uio/llog"
"golang.org/x/exp/maps"
)

func main() {
Expand Down Expand Up @@ -50,7 +53,75 @@ func main() {
f.RegisterFlags(makeCmd.Flags())
tf.RegisterFlags(makeCmd.Flags())

app := cli.App{makeCmd}
listconfigsCmd := cli.Command{
Name: "listconfigs",
Short: "list template configs",
Run: func(args []string) {
tpl, err := tf.Get()
if err != nil {
l.Errorf("Failed to get template: %w", err)
os.Exit(1)
}
configs := maps.Keys(tpl.Configs)
slices.Sort(configs)
for _, name := range configs {
fmt.Println(name)
}
},
}
l.RegisterVerboseFlag(listconfigsCmd.Flags(), "v", slog.LevelDebug)
tf.RegisterFlags(listconfigsCmd.Flags())

listCmd := cli.Command{
Name: "list",
Short: "list all commands reachable from template",
Run: func(args []string) {
tpl, err := tf.Get()
if err != nil {
l.Errorf("Failed to get template: %w", err)
os.Exit(1)
}
var cmds []string
if tf.Config == "" {
for _, conf := range tpl.Configs {
for _, c := range conf.Commands {
cmds = append(cmds, tpl.CommandsFor(c.Commands...)...)
}
}
for _, c := range tpl.Commands {
cmds = append(cmds, c...)
}
} else {
if _, ok := tpl.Configs[tf.Config]; !ok {
l.Errorf("Config %s not found", tf.Config)
os.Exit(1)
}
for _, c := range tpl.Configs[tf.Config].Commands {
cmds = append(cmds, tpl.CommandsFor(c.Commands...)...)
}
}

lookupEnv := findpkg.DefaultEnv()
paths, err := findpkg.ResolveGlobs(l.AtLevel(slog.LevelInfo), env, lookupEnv, cmds)
if err != nil {
l.Errorf("Failed to resolve commands: %v", err)
os.Exit(1)
}
uniquePaths := map[string]struct{}{}
for _, p := range paths {
uniquePaths[p] = struct{}{}
}
ps := maps.Keys(uniquePaths)
slices.Sort(ps)
for _, p := range ps {
fmt.Println(p)
}
},
}
l.RegisterVerboseFlag(listCmd.Flags(), "v", slog.LevelDebug)
tf.RegisterFlags(listCmd.Flags())

app := cli.App{makeCmd, listconfigsCmd, listCmd}
app.Run(os.Args)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/u-root/gobusybox/src v0.0.0-20240218001334-a32c1883bffa
github.com/u-root/u-root v0.12.0
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/tools v0.17.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ github.com/u-root/gobusybox/src v0.0.0-20240218001334-a32c1883bffa h1:NNmn/fsvgA
github.com/u-root/gobusybox/src v0.0.0-20240218001334-a32c1883bffa/go.mod h1:vN1IwhlCo7gTDTJDUs6WCKM4/C2uiq5w0XvZCqLtb5s=
github.com/u-root/u-root v0.12.0 h1:K0AuBFriwr0w/PGS3HawiAw89e3+MU7ks80GpghAsNs=
github.com/u-root/u-root v0.12.0/go.mod h1:FYjTOh4IkIZHhjsd17lb8nYW6udgXdJhG1c0r6u0arI=
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a h1:BH1SOPEvehD2kVrndDnGJiUF0TrBpNs+iyYocu6h0og=
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 h1:pyC9PaHYZFgEKFdlp3G8RaCKgVpHZnecvArXvPXcFkM=
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
Expand Down

0 comments on commit 083bbe7

Please sign in to comment.