Skip to content

Commit a0805a0

Browse files
simonfaltumIsaac
andauthored
skills read: end flags before a leading-hyphen file name (#8121)
`databricks skills read` prints a `databricks skills read-file <name> <path>` command for each supporting file. A name or path that starts with `-` was read by Cobra as a flag when an agent ran the printed command, so the file could not be read. This PR prints `--` before the arguments in that case. ## Changes - `skills read` prints `databricks skills read-file -- <name> <path>` when the skill name or the file path starts with `-`. - Other names print exactly as before, so existing output is unchanged. - `read-file` needs no change: Cobra already treats the arguments after `--` as positional. ## Why A file such as `-notes.md` is listed in the manifest and the manifest checks accept it, but the printed command `read-file guide-skill -notes.md` fails with an unknown flag. Ending the flags with `--` lets the printed command read the file as printed. ## Decisions to review - `--` is printed only when a name or path starts with `-`, not always. Always printing it would change the output for every skill, and the existing goldens with it. The cost is one extra branch in `writeReadText`. ## Tests - `TestWriteReadTextEndsFlagsBeforeLeadingHyphenFile` covers a leading-hyphen file name, a leading-hyphen skill name, and a leading-hyphen file name that also needs quotes (Linux and Windows quoting), and checks that ordinary files on the same line keep their command. - `acceptance/cmd/skills/read` checks that the printed command for `-notes.md` (a file in the shared release fixture) appears in the output, then runs it with `eval` and checks that it prints the file. It also runs `read-file -- guide-skill -notes.md` directly. - `acceptance/cmd/skills/disabled` already checks that the `skills` group is not registered when `DATABRICKS_SKILLS_PREVIEW` is unset, so the change is behind the guard. - Not tested: the printed Windows form in Windows PowerShell 5.1, because the acceptance script runs in bash on the Windows CI cell. - `go test ./cmd/skills/... ./libs/skills/...` and `go test ./acceptance -run TestAccept/cmd/skills` pass, and `./task fmt-q lint-q` is clean. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent cfd268f commit a0805a0

6 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎acceptance/cmd/skills/read/output.txt‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ Read the guide in references for details.
1313

1414
Other files in this skill, each read with its command:
1515
databricks skills read-file guide-skill references/guide.md
16+
databricks skills read-file -- guide-skill -notes.md
17+
18+
=== A file whose name starts with a hyphen: the printed command ends the flags and runs as printed
19+
20+
>>> eval databricks skills read-file -- guide-skill -notes.md
21+
Notes that start with a hyphen.
22+
23+
=== read-file accepts -- before a leading-hyphen file name
24+
25+
>>> [CLI] skills read-file -- guide-skill -notes.md
26+
Notes that start with a hyphen.
1627

1728
=== A skill without supporting files
1829

@@ -33,7 +44,8 @@ description: A skill for tests.
3344
"source": "[TESTROOT]/cmd/skills/read/../release",
3445
"content": "---\nname: guide-skill\ndescription: A skill with a supporting file.\n---\n\n# Guide skill\n\nRead the guide in references for details.\n",
3546
"files": [
36-
"references/guide.md"
47+
"references/guide.md",
48+
"-notes.md"
3749
]
3850
}
3951

‎acceptance/cmd/skills/read/script‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ export DATABRICKS_SKILLS_DIR=$TESTDIR/../release
44
title "A skill with a supporting file: SKILL.md, then the file list"
55
trace $CLI skills read guide-skill
66

7+
title "A file whose name starts with a hyphen: the printed command ends the flags and runs as printed"
8+
databricks() { "$CLI" "$@"; }
9+
line="databricks skills read-file -- guide-skill -notes.md"
10+
$CLI skills read guide-skill | contains.py " $line" > LOG.read
11+
trace eval "$line"
12+
13+
title "read-file accepts -- before a leading-hyphen file name"
14+
trace $CLI skills read-file -- guide-skill -notes.md
15+
716
title "A skill without supporting files"
817
trace $CLI skills read demo-skill
918

‎acceptance/cmd/skills/release/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "0.0.1",
33
"skills": {
44
"demo-skill": {"version": "0.0.1", "description": "A skill for tests.", "files": ["SKILL.md"], "commands": ["fs", "air"]},
5-
"guide-skill": {"version": "0.0.1", "description": "A skill with a supporting file.", "files": ["SKILL.md", "references/guide.md"]},
5+
"guide-skill": {"version": "0.0.1", "description": "A skill with a supporting file.", "files": ["SKILL.md", "references/guide.md", "-notes.md"]},
66
"future-skill": {"version": "0.0.1", "description": "A skill that needs a newer CLI.", "files": ["SKILL.md"], "min_cli_version": "99.0.0"}
77
}
88
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Notes that start with a hyphen.

‎cmd/skills/read.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ func writeReadText(w io.Writer, goos, name string, content []byte, files []strin
122122
fmt.Fprintf(&b, " %s\n", f)
123123
continue
124124
}
125-
fmt.Fprintf(&b, " databricks skills read-file %s %s\n", quotedName, quotedFile)
125+
// A leading "-" would make Cobra read the name or file as a flag, so "--" ends the flags first.
126+
endOfFlags := ""
127+
if strings.HasPrefix(name, "-") || strings.HasPrefix(f, "-") {
128+
endOfFlags = "-- "
129+
}
130+
fmt.Fprintf(&b, " databricks skills read-file %s%s %s\n", endOfFlags, quotedName, quotedFile)
126131
}
127132
}
128133
_, err := io.WriteString(w, b.String())

‎cmd/skills/read_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ func TestWriteReadTextQuotesFileCommands(t *testing.T) {
2727
assert.Contains(t, out.String(), " databricks skills read-file guide-skill \"references/my guide.md\"\n")
2828
}
2929

30+
func TestWriteReadTextEndsFlagsBeforeLeadingHyphenFile(t *testing.T) {
31+
var out bytes.Buffer
32+
err := writeReadText(&out, "linux", "guide-skill", []byte("# Guide\n"), []string{"-notes.md", "references/ok.md"})
33+
require.NoError(t, err)
34+
assert.Contains(t, out.String(), " databricks skills read-file -- guide-skill -notes.md\n databricks skills read-file guide-skill references/ok.md\n")
35+
36+
out.Reset()
37+
err = writeReadText(&out, "linux", "-guide-skill", []byte("# Guide\n"), []string{"references/ok.md"})
38+
require.NoError(t, err)
39+
assert.Contains(t, out.String(), " databricks skills read-file -- -guide-skill references/ok.md\n")
40+
41+
out.Reset()
42+
err = writeReadText(&out, "linux", "guide-skill", []byte("# Guide\n"), []string{"-my notes.md"})
43+
require.NoError(t, err)
44+
assert.Contains(t, out.String(), " databricks skills read-file -- guide-skill '-my notes.md'\n")
45+
46+
out.Reset()
47+
err = writeReadText(&out, "windows", "guide-skill", []byte("# Guide\n"), []string{"-my notes.md"})
48+
require.NoError(t, err)
49+
assert.Contains(t, out.String(), " databricks skills read-file -- guide-skill \"-my notes.md\"\n")
50+
}
51+
3052
func TestWriteReadTextPrintsPathWithoutCommandWhenItCannotBeQuoted(t *testing.T) {
3153
var out bytes.Buffer
3254
err := writeReadText(&out, "linux", "guide-skill", []byte("# Guide\n"), []string{"references/a\nb.md", "references/ok.md"})

0 commit comments

Comments
 (0)