@@ -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,77 @@ 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 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 }
54
127
app .Run (os .Args )
55
128
}
56
129
0 commit comments