@@ -10,12 +10,15 @@ import (
10
10
"log"
11
11
"log/slog"
12
12
"os"
13
+ "slices"
13
14
15
+ "github.com/u-root/gobusybox/src/pkg/bb/findpkg"
14
16
"github.com/u-root/gobusybox/src/pkg/golang"
15
17
"github.com/u-root/mkuimage/uimage"
16
18
"github.com/u-root/mkuimage/uimage/mkuimage"
17
19
"github.com/u-root/uio/cli"
18
20
"github.com/u-root/uio/llog"
21
+ "golang.org/x/exp/maps"
19
22
)
20
23
21
24
func main () {
@@ -50,7 +53,75 @@ func main() {
50
53
f .RegisterFlags (makeCmd .Flags ())
51
54
tf .RegisterFlags (makeCmd .Flags ())
52
55
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 all commands reachable from 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 == "" {
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
+ } else {
95
+ if _ , ok := tpl .Configs [tf .Config ]; ! ok {
96
+ l .Errorf ("Config %s not found" , tf .Config )
97
+ os .Exit (1 )
98
+ }
99
+ for _ , c := range tpl .Configs [tf .Config ].Commands {
100
+ cmds = append (cmds , tpl .CommandsFor (c .Commands ... )... )
101
+ }
102
+ }
103
+
104
+ lookupEnv := findpkg .DefaultEnv ()
105
+ paths , err := findpkg .ResolveGlobs (l .AtLevel (slog .LevelInfo ), env , lookupEnv , cmds )
106
+ if err != nil {
107
+ l .Errorf ("Failed to resolve commands: %v" , err )
108
+ os .Exit (1 )
109
+ }
110
+ uniquePaths := map [string ]struct {}{}
111
+ for _ , p := range paths {
112
+ uniquePaths [p ] = struct {}{}
113
+ }
114
+ ps := maps .Keys (uniquePaths )
115
+ slices .Sort (ps )
116
+ for _ , p := range ps {
117
+ fmt .Println (p )
118
+ }
119
+ },
120
+ }
121
+ l .RegisterVerboseFlag (listCmd .Flags (), "v" , slog .LevelDebug )
122
+ tf .RegisterFlags (listCmd .Flags ())
123
+
124
+ app := cli.App {makeCmd , listconfigsCmd , listCmd }
54
125
app .Run (os .Args )
55
126
}
56
127
0 commit comments