Skip to content

Commit fbf96f6

Browse files
committed
Show file argument in plt help and resolve relative paths under bazel run
Add an Examples section and a descriptive Use string so 'vbs plt --help' makes clear each subcommand takes a playlist file. Resolve relative input paths against BUILD_WORKING_DIRECTORY so 'bazel run' works from the directory the user invoked it in, not the runfiles tree.
1 parent 32aac26 commit fbf96f6

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

cmd/plt.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"io"
2121
"os"
22+
"path/filepath"
2223
"text/tabwriter"
2324

2425
"github.com/muesli/coral"
@@ -28,23 +29,27 @@ import (
2829
var pltPrintJSON bool
2930

3031
var pltCmd = &coral.Command{
31-
Use: "plt",
32+
Use: "plt <command> <playlist-file>",
3233
Short: "Work with purple playlists.",
3334
Long: `Parse, build, and prepare media for purple playlist exports from the
34-
source app for use in live meetings.`,
35+
source app for use in live meetings. Each subcommand takes the path to a
36+
playlist export file.`,
37+
Example: ` vbs plt print meeting.playlist
38+
vbs plt build meeting.playlist`,
3539
}
3640

3741
var pltPrintCmd = &coral.Command{
38-
Use: "print <playlist>",
42+
Use: "print <playlist-file>",
3943
Short: "Parse and pretty-print a purple playlist.",
4044
Long: `Parse a purple playlist export and print its cues. Works entirely
4145
offline; no media is downloaded.`,
42-
Run: runPltPrint,
43-
Args: coral.ExactArgs(1),
46+
Example: " vbs plt print meeting.playlist",
47+
Run: runPltPrint,
48+
Args: coral.ExactArgs(1),
4449
}
4550

46-
func runPltPrint(cmd *coral.Command, args []string) {
47-
path := args[0]
51+
func runPltPrint(_ *coral.Command, args []string) {
52+
path := resolveInputPath(args[0])
4853

4954
arc, err := sniffPlaylist(path)
5055
if err != nil {
@@ -74,6 +79,20 @@ func runPltPrint(cmd *coral.Command, args []string) {
7479
}
7580
}
7681

82+
// resolveInputPath makes a user-supplied path usable regardless of how the
83+
// binary was launched. Relative paths are resolved against the directory the
84+
// command was invoked from; under `bazel run` that is reported via
85+
// BUILD_WORKING_DIRECTORY, since the process itself starts in the runfiles tree.
86+
func resolveInputPath(p string) string {
87+
if p == "" || filepath.IsAbs(p) {
88+
return p
89+
}
90+
if wd := os.Getenv("BUILD_WORKING_DIRECTORY"); wd != "" {
91+
return filepath.Join(wd, p)
92+
}
93+
return p
94+
}
95+
7796
// printView is the offline summary rendered by plt print, shared by the text
7897
// and JSON outputs so both show the same data.
7998
type printView struct {

cmd/plt_print_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,38 @@ package cmd
1717
import (
1818
"encoding/json"
1919
"math"
20+
"path/filepath"
2021
"strings"
2122
"testing"
2223
)
2324

25+
func TestResolveInputPath(t *testing.T) {
26+
abs := filepath.Join(t.TempDir(), "meeting.playlist")
27+
28+
t.Run("absolute path is unchanged", func(t *testing.T) {
29+
t.Setenv("BUILD_WORKING_DIRECTORY", "/some/where")
30+
if got := resolveInputPath(abs); got != abs {
31+
t.Errorf("resolveInputPath(%q) = %q, want unchanged", abs, got)
32+
}
33+
})
34+
35+
t.Run("relative path joins BUILD_WORKING_DIRECTORY", func(t *testing.T) {
36+
t.Setenv("BUILD_WORKING_DIRECTORY", "/launch/dir")
37+
got := resolveInputPath("../export/meeting.playlist")
38+
want := filepath.Join("/launch/dir", "../export/meeting.playlist")
39+
if got != want {
40+
t.Errorf("resolveInputPath = %q, want %q", got, want)
41+
}
42+
})
43+
44+
t.Run("relative path unchanged when env unset", func(t *testing.T) {
45+
t.Setenv("BUILD_WORKING_DIRECTORY", "")
46+
if got := resolveInputPath("rel/meeting.playlist"); got != "rel/meeting.playlist" {
47+
t.Errorf("resolveInputPath = %q, want unchanged relative path", got)
48+
}
49+
})
50+
}
51+
2452
func TestDescribeSource(t *testing.T) {
2553
cases := []struct {
2654
name string

0 commit comments

Comments
 (0)