Skip to content

Commit 1d7d9b0

Browse files
committed
list and listconfigs commands
Signed-off-by: Chris Koch <[email protected]>
1 parent 440ed19 commit 1d7d9b0

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

cmd/uimage/main.go

+74-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import (
1010
"log"
1111
"log/slog"
1212
"os"
13+
"slices"
1314

15+
"github.com/u-root/gobusybox/src/pkg/bb/findpkg"
1416
"github.com/u-root/gobusybox/src/pkg/golang"
1517
"github.com/u-root/mkuimage/uimage"
1618
"github.com/u-root/mkuimage/uimage/mkuimage"
1719
"github.com/u-root/uio/cli"
1820
"github.com/u-root/uio/llog"
21+
"golang.org/x/exp/maps"
1922
)
2023

2124
func main() {
@@ -50,7 +53,77 @@ func main() {
5053
f.RegisterFlags(makeCmd.Flags())
5154
tf.RegisterFlags(makeCmd.Flags())
5255

53-
app := cli.App{makeCmd}
56+
listconfigsCmd := cli.Command{
57+
Name: "listconfigs",
58+
Short: "list template configs",
59+
Run: func(args []string) {
60+
tpl, err := tf.Get()
61+
if err != nil {
62+
l.Errorf("Failed to get template: %w", err)
63+
os.Exit(1)
64+
}
65+
configs := maps.Keys(tpl.Configs)
66+
slices.Sort(configs)
67+
for _, name := range configs {
68+
fmt.Println(name)
69+
}
70+
},
71+
}
72+
l.RegisterVerboseFlag(listconfigsCmd.Flags(), "v", slog.LevelDebug)
73+
tf.RegisterFlags(listconfigsCmd.Flags())
74+
75+
listCmd := cli.Command{
76+
Name: "list",
77+
Short: "list commands from template (no args: lists all cmds in template)",
78+
Run: func(args []string) {
79+
tpl, err := tf.Get()
80+
if err != nil {
81+
l.Errorf("Failed to get template: %w", err)
82+
os.Exit(1)
83+
}
84+
var cmds []string
85+
if tf.Config == "" && len(args) == 0 {
86+
for _, conf := range tpl.Configs {
87+
for _, c := range conf.Commands {
88+
cmds = append(cmds, tpl.CommandsFor(c.Commands...)...)
89+
}
90+
}
91+
for _, c := range tpl.Commands {
92+
cmds = append(cmds, c...)
93+
}
94+
}
95+
if tf.Config != "" {
96+
if _, ok := tpl.Configs[tf.Config]; !ok {
97+
l.Errorf("Config %s not found", tf.Config)
98+
os.Exit(1)
99+
}
100+
for _, c := range tpl.Configs[tf.Config].Commands {
101+
cmds = append(cmds, tpl.CommandsFor(c.Commands...)...)
102+
}
103+
}
104+
cmds = append(cmds, tpl.CommandsFor(args...)...)
105+
106+
lookupEnv := findpkg.DefaultEnv()
107+
paths, err := findpkg.ResolveGlobs(l.AtLevel(slog.LevelInfo), env, lookupEnv, cmds)
108+
if err != nil {
109+
l.Errorf("Failed to resolve commands: %v", err)
110+
os.Exit(1)
111+
}
112+
uniquePaths := map[string]struct{}{}
113+
for _, p := range paths {
114+
uniquePaths[p] = struct{}{}
115+
}
116+
ps := maps.Keys(uniquePaths)
117+
slices.Sort(ps)
118+
for _, p := range ps {
119+
fmt.Println(p)
120+
}
121+
},
122+
}
123+
l.RegisterVerboseFlag(listCmd.Flags(), "v", slog.LevelDebug)
124+
tf.RegisterFlags(listCmd.Flags())
125+
126+
app := cli.App{makeCmd, listconfigsCmd, listCmd}
54127
app.Run(os.Args)
55128
}
56129

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/u-root/gobusybox/src v0.0.0-20240218001334-a32c1883bffa
1010
github.com/u-root/u-root v0.12.0
1111
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701
12+
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
1213
golang.org/x/sync v0.6.0
1314
golang.org/x/sys v0.16.0
1415
golang.org/x/tools v0.17.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ github.com/u-root/gobusybox/src v0.0.0-20240218001334-a32c1883bffa h1:NNmn/fsvgA
4444
github.com/u-root/gobusybox/src v0.0.0-20240218001334-a32c1883bffa/go.mod h1:vN1IwhlCo7gTDTJDUs6WCKM4/C2uiq5w0XvZCqLtb5s=
4545
github.com/u-root/u-root v0.12.0 h1:K0AuBFriwr0w/PGS3HawiAw89e3+MU7ks80GpghAsNs=
4646
github.com/u-root/u-root v0.12.0/go.mod h1:FYjTOh4IkIZHhjsd17lb8nYW6udgXdJhG1c0r6u0arI=
47-
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a h1:BH1SOPEvehD2kVrndDnGJiUF0TrBpNs+iyYocu6h0og=
48-
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
4947
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 h1:pyC9PaHYZFgEKFdlp3G8RaCKgVpHZnecvArXvPXcFkM=
5048
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
5149
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=

0 commit comments

Comments
 (0)