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 440ed19 commit 1d7d9b0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
75 changes: 74 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,77 @@ 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)

Check warning on line 63 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L60-L63

Added lines #L60 - L63 were not covered by tests
}
configs := maps.Keys(tpl.Configs)
slices.Sort(configs)
for _, name := range configs {
fmt.Println(name)

Check warning on line 68 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L65-L68

Added lines #L65 - L68 were not covered by tests
}
},
}
l.RegisterVerboseFlag(listconfigsCmd.Flags(), "v", slog.LevelDebug)
tf.RegisterFlags(listconfigsCmd.Flags())

listCmd := cli.Command{
Name: "list",
Short: "list commands from template (no args: lists all cmds in template)",
Run: func(args []string) {
tpl, err := tf.Get()
if err != nil {
l.Errorf("Failed to get template: %w", err)
os.Exit(1)

Check warning on line 82 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L79-L82

Added lines #L79 - L82 were not covered by tests
}
var cmds []string
if tf.Config == "" && len(args) == 0 {
for _, conf := range tpl.Configs {
for _, c := range conf.Commands {
cmds = append(cmds, tpl.CommandsFor(c.Commands...)...)

Check warning on line 88 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L84-L88

Added lines #L84 - L88 were not covered by tests
}
}
for _, c := range tpl.Commands {
cmds = append(cmds, c...)

Check warning on line 92 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}
}
if tf.Config != "" {
if _, ok := tpl.Configs[tf.Config]; !ok {
l.Errorf("Config %s not found", tf.Config)
os.Exit(1)

Check warning on line 98 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L95-L98

Added lines #L95 - L98 were not covered by tests
}
for _, c := range tpl.Configs[tf.Config].Commands {
cmds = append(cmds, tpl.CommandsFor(c.Commands...)...)

Check warning on line 101 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}
}
cmds = append(cmds, tpl.CommandsFor(args...)...)

Check warning on line 104 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L104

Added line #L104 was not covered by tests

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)

Check warning on line 110 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L106-L110

Added lines #L106 - L110 were not covered by tests
}
uniquePaths := map[string]struct{}{}
for _, p := range paths {
uniquePaths[p] = struct{}{}

Check warning on line 114 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L112-L114

Added lines #L112 - L114 were not covered by tests
}
ps := maps.Keys(uniquePaths)
slices.Sort(ps)
for _, p := range ps {
fmt.Println(p)

Check warning on line 119 in cmd/uimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/uimage/main.go#L116-L119

Added lines #L116 - L119 were not covered by tests
}
},
}
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 1d7d9b0

Please sign in to comment.