diff --git a/acceptance/cmd/skills/list/output.txt b/acceptance/cmd/skills/list/output.txt index 31107bd777c..1b5d3be80ed 100644 --- a/acceptance/cmd/skills/list/output.txt +++ b/acceptance/cmd/skills/list/output.txt @@ -5,7 +5,7 @@ Read the skill before you act on it. -foo: A skill whose name starts with a hyphen. demo-skill: A skill for tests. -future-skill: A skill that needs a newer CLI. +future-skill: A skill that needs a newer CLI. (needs CLI 99.0.0 or newer) guide-skill: A skill with a supporting file. To read a skill, run: databricks skills read @@ -15,7 +15,7 @@ Read the skill before you act on it. -foo: A skill whose name starts with a hyphen. demo-skill: A skill for tests. -future-skill: A skill that needs a newer CLI. +future-skill: A skill that needs a newer CLI. (needs CLI 99.0.0 or newer) guide-skill: A skill with a supporting file. preview-skill: An experimental skill for tests. To read a skill, run: databricks skills read @@ -52,6 +52,7 @@ To read a skill, run: databricks skills read "name": "future-skill", "description": "A skill that needs a newer CLI.", "version": "0.0.1", + "min_cli_version": "99.0.0", "files": [ "SKILL.md" ], @@ -103,6 +104,7 @@ To read a skill, run: databricks skills read "name": "future-skill", "description": "A skill that needs a newer CLI.", "version": "0.0.1", + "min_cli_version": "99.0.0", "files": [ "SKILL.md" ], diff --git a/cmd/skills/list.go b/cmd/skills/list.go index 9d3c7102f4b..00d87ebab39 100644 --- a/cmd/skills/list.go +++ b/cmd/skills/list.go @@ -91,9 +91,10 @@ func provenanceLine(catalog *libskills.Catalog) string { } // renderText returns the text output of `skills list`: the catalog rules, then -// one line per skill, then a line on how to read a skill. When the whole output -// is over textBudget, descriptions are cut at a word boundary to the widest -// width that fits. When even the names do not fit, only the names are printed. +// one line per skill, then a line on how to read a skill. A skill that needs a +// newer CLI keeps its line and gets a note after it. When the whole output is +// over textBudget, descriptions are cut at a word boundary to the widest width +// that fits. When even the names do not fit, only the names are printed. func renderText(rules string, list []libskills.Skill) string { if len(list) == 0 { return withRules(rules) + "No skills found.\n" @@ -101,10 +102,14 @@ func renderText(rules string, list []libskills.Skill) string { // Every width lays the list out again, so each description is joined into // one line once, here. list = oneLineSkills(list) + notes := make([]string, len(list)) + for i, s := range list { + notes[i] = newerCLINote(s) + } // A description only makes its line longer, and the search hint is the one // line a wider width can leave out. So when the names do not fit even with // the hint, no width does, and the names-only text is the answer. - names := layoutText(rules, list, 0) + names := layoutText(rules, list, notes, 0) if len(names)-len(searchHint)-1 > textBudget { return names } @@ -115,7 +120,7 @@ func renderText(rules string, list []libskills.Skill) string { // No width above maxLen cuts a description, so the widest width to try is // the smaller of maxLen and the budget. top := min(maxLen, textBudget) - fits := func(w int) bool { return len(layoutText(rules, list, w)) <= textBudget } + fits := func(w int) bool { return len(layoutText(rules, list, notes, w)) <= textBudget } // The length is not monotonic in width: once no description is left out, the // search hint goes away, so a wider width can be shorter. Below the first // width at which no description is left out the hint is always there, and from @@ -123,10 +128,10 @@ func renderText(rules string, list []libskills.Skill) string { // widest fitting width is searched in the upper part first, then the lower. complete := 1 + sort.Search(top, func(i int) bool { return !dropsAny(list, i+1) }) if w := widestFitIn(complete, top, fits); w > 0 { - return layoutText(rules, list, w) + return layoutText(rules, list, notes, w) } if w := widestFitIn(1, complete-1, fits); w > 0 { - return layoutText(rules, list, w) + return layoutText(rules, list, notes, w) } return names } @@ -154,27 +159,35 @@ func dropsAny(list []libskills.Skill, width int) bool { } // layoutText lays out the text output with descriptions cut to width. A width -// of 0 leaves every description out. A skill is listed by its name alone when -// its description is left out or has no word boundary within width, so one -// long description does not drop the others. Descriptions must be one line. +// of 0 leaves every description out. A skill is listed by its name, followed +// by its newer-CLI note if it has one, when its description is left out or has +// no word boundary within width, so one long description does not drop the +// others. Descriptions must be one line. // The search hint must stay the only line a wider width can leave out: the // width search in renderText relies on it, together with dropsAny. -func layoutText(rules string, list []libskills.Skill, width int) string { +func layoutText(rules string, list []libskills.Skill, notes []string, width int) string { var b strings.Builder b.WriteString(withRules(rules)) dropped := false - for _, s := range list { + for i, s := range list { desc := s.Description cut := "" if width > 0 { cut = cutWords(desc, width) } - if cut == "" { + note := notes[i] + switch { + case cut == "" && note == "": b.WriteString(s.Name + "\n") - dropped = dropped || desc != "" - } else { + case cut == "": + // Keep the colon so the name is the text before it. + b.WriteString(s.Name + ": " + note + "\n") + case note == "": fmt.Fprintf(&b, "%s: %s\n", s.Name, cut) + default: + fmt.Fprintf(&b, "%s: %s %s\n", s.Name, cut, note) } + dropped = dropped || (cut == "" && desc != "") } if dropped { b.WriteString(searchHint + "\n") @@ -183,6 +196,16 @@ func layoutText(rules string, list []libskills.Skill, width int) string { return b.String() } +// newerCLINote returns the note that marks a skill which needs a newer CLI than +// the running one, or "" when this CLI is new enough. The skill stays listed, +// as `skills read` still serves it with a warning. +func newerCLINote(s libskills.Skill) string { + if !needsNewerCLI(s) { + return "" + } + return fmt.Sprintf("(needs CLI %s or newer)", s.MinCLIVer) +} + // oneLineSkills returns list with each description joined into one line. func oneLineSkills(list []libskills.Skill) []libskills.Skill { out := make([]libskills.Skill, len(list)) @@ -229,11 +252,12 @@ type listJSON struct { } type listJSONSkill struct { - Name string `json:"name"` - Description string `json:"description"` - Version string `json:"version"` - Files []string `json:"files"` - Commands []string `json:"commands"` + Name string `json:"name"` + Description string `json:"description"` + Version string `json:"version"` + MinCLIVersion string `json:"min_cli_version,omitempty"` + Files []string `json:"files"` + Commands []string `json:"commands"` } // nonNilList keeps an absent list as [] in JSON rather than null. @@ -252,11 +276,12 @@ func newListJSON(catalog *libskills.Catalog, list []libskills.Skill) listJSON { } for _, s := range list { out.Skills = append(out.Skills, listJSONSkill{ - Name: s.Name, - Description: s.Description, - Version: s.Version, - Files: nonNilList(s.Files), - Commands: nonNilList(s.Commands), + Name: s.Name, + Description: s.Description, + Version: s.Version, + MinCLIVersion: s.MinCLIVer, + Files: nonNilList(s.Files), + Commands: nonNilList(s.Commands), }) } return out diff --git a/cmd/skills/list_test.go b/cmd/skills/list_test.go index ae0c4d906e1..2f2e2ab5e29 100644 --- a/cmd/skills/list_test.go +++ b/cmd/skills/list_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" libskills "github.com/databricks/cli/libs/skills" @@ -161,7 +162,11 @@ func renderTextScan(rules string, list []libskills.Skill) string { return withRules(rules) + "No skills found.\n" } list = oneLineSkills(list) - names := layoutText(rules, list, 0) + notes := make([]string, len(list)) + for i, s := range list { + notes[i] = newerCLINote(s) + } + names := layoutText(rules, list, notes, 0) if len(names)-len(searchHint)-1 > textBudget { return names } @@ -170,7 +175,7 @@ func renderTextScan(rules string, list []libskills.Skill) string { maxLen = max(maxLen, len(s.Description)) } for w := min(maxLen, textBudget); w > 0; w-- { - if out := layoutText(rules, list, w); len(out) <= textBudget { + if out := layoutText(rules, list, notes, w); len(out) <= textBudget { return out } } @@ -261,6 +266,25 @@ func TestRenderTextNoSearchHintWithoutDescriptions(t *testing.T) { assert.Equal(t, "alpha\nbeta\n"+readHint+"\n", renderText("", list)) } +func TestRenderTextMarksSkillThatNeedsNewerCLI(t *testing.T) { + list := []libskills.Skill{ + {Name: "alpha", Description: "Alpha does one thing well."}, + {Name: "future", Description: "Needs a newer CLI.", MinCLIVer: "99.0.0"}, + } + assert.Equal(t, "alpha: Alpha does one thing well.\nfuture: Needs a newer CLI. (needs CLI 99.0.0 or newer)\n"+readHint+"\n", + renderText("", list)) +} + +func TestRenderTextMarksNamesOnlyLineThatNeedsNewerCLI(t *testing.T) { + list := []libskills.Skill{{Name: "alpha"}, {Name: "future", MinCLIVer: "99.0.0"}} + assert.Equal(t, "alpha\nfuture: (needs CLI 99.0.0 or newer)\n"+readHint+"\n", renderText("", list)) +} + +func TestRenderTextNoMarkWhenCLIIsNewEnough(t *testing.T) { + list := []libskills.Skill{{Name: "current", Description: "Current.", MinCLIVer: build.GetInfo().Version}} + assert.Equal(t, "current: Current.\n"+readHint+"\n", renderText("", list)) +} + func TestRenderTextNoSkills(t *testing.T) { assert.Equal(t, "No skills found.\n", renderText("", nil)) } diff --git a/cmd/skills/read.go b/cmd/skills/read.go index ed4c253430c..a55646965fb 100644 --- a/cmd/skills/read.go +++ b/cmd/skills/read.go @@ -15,7 +15,6 @@ import ( "github.com/databricks/cli/libs/shellquote" libskills "github.com/databricks/cli/libs/skills" "github.com/spf13/cobra" - "golang.org/x/mod/semver" ) // skillFile is the file that holds a skill's instructions. Every other file of @@ -72,10 +71,9 @@ func runRead(cmd *cobra.Command, src libskills.Source, name string) error { return err } - info := build.GetInfo() - if skill.MinCLIVer != "" && semver.Compare("v"+info.Version, "v"+skill.MinCLIVer) < 0 { + if needsNewerCLI(skill) { cmdio.LogString(ctx, fmt.Sprintf("Warning: skill %q needs CLI version %s or newer, this CLI is %s. Update the CLI to use it.", - name, skill.MinCLIVer, info.Version)) + name, skill.MinCLIVer, build.GetInfo().Version)) } files := otherFiles(skill.Files) diff --git a/cmd/skills/search.go b/cmd/skills/search.go index bdb75c09d0b..3c95b5cbb13 100644 --- a/cmd/skills/search.go +++ b/cmd/skills/search.go @@ -10,16 +10,18 @@ import ( "github.com/spf13/cobra" ) -// searchTemplate prints the matches in the line format of skills list, then the -// line that says how to read a skill. With no match it points at skills list. +// searchTemplate prints the matches as name and description lines like +// skills list, without its newer-CLI note, then the line that says how to read +// a skill. With no match it points at skills list. const searchTemplate = `{{if .Skills}}{{range .Skills}}{{.Name}}{{with .Description}}: {{.}}{{end}} {{end}}To read a skill, run: databricks skills read {{else}}No skill matches. Run databricks skills list to see every skill. {{end}}` // searchJSON is the JSON output of skills search. It has the envelope and the -// per-skill fields of skills list, so one parser reads both. skills list defines -// the same shape in its own file; the two should share one type once both land. +// per-skill fields of skills list except min_cli_version, so one parser reads +// both. skills list defines the same shape in its own file; the two should +// share one type once both land. type searchJSON struct { Source string `json:"source"` ReleaseVersion string `json:"release_version"` diff --git a/cmd/skills/skills.go b/cmd/skills/skills.go index 25185cdbc54..3d7471d0bbc 100644 --- a/cmd/skills/skills.go +++ b/cmd/skills/skills.go @@ -6,7 +6,10 @@ import ( "context" "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/internal/build" + libskills "github.com/databricks/cli/libs/skills" "github.com/spf13/cobra" + "golang.org/x/mod/semver" ) // Spec declares the skills command group. @@ -24,3 +27,10 @@ func buildCommand(_ context.Context, cmd *cobra.Command) { cmd.AddCommand(newReadFileCmd()) cmd.AddCommand(newSearchCmd()) } + +// needsNewerCLI reports whether the running CLI is older than the skill's +// min_cli_version. A development build compares below the release it builds +// toward, so it counts as older than a skill that needs that release. +func needsNewerCLI(s libskills.Skill) bool { + return s.MinCLIVer != "" && semver.Compare("v"+build.GetInfo().Version, "v"+s.MinCLIVer) < 0 +}