DiscoverSkillByPathWithOptions looks up a single skill by its exact path in the repository, applying the given options.
(client *api.Client, host, owner, repo, commitSHA, skillPath string, opts DiscoverSkillByPathOptions)
| 743 | // DiscoverSkillByPathWithOptions looks up a single skill by its exact path in |
| 744 | // the repository, applying the given options. |
| 745 | func DiscoverSkillByPathWithOptions(client *api.Client, host, owner, repo, commitSHA, skillPath string, opts DiscoverSkillByPathOptions) (*Skill, error) { |
| 746 | skillPath = strings.TrimSuffix(skillPath, "/SKILL.md") |
| 747 | skillPath = strings.TrimSuffix(skillPath, "/") |
| 748 | |
| 749 | skillName := path.Base(skillPath) |
| 750 | if !validateName(skillName) { |
| 751 | return nil, fmt.Errorf("invalid skill name %q", skillName) |
| 752 | } |
| 753 | |
| 754 | parentPath := path.Dir(skillPath) |
| 755 | apiPath, err := safeurl.JoinPath("repos", owner, repo, "contents", parentPath) |
| 756 | if err != nil { |
| 757 | return nil, err |
| 758 | } |
| 759 | apiPath.SetQuery("ref", commitSHA) |
| 760 | |
| 761 | var contents []struct { |
| 762 | Name string `json:"name"` |
| 763 | Path string `json:"path"` |
| 764 | SHA string `json:"sha"` |
| 765 | Type string `json:"type"` |
| 766 | } |
| 767 | if err := client.REST(host, "GET", apiPath.String(), nil, &contents); err != nil { |
| 768 | return nil, fmt.Errorf("path %q not found in %s/%s: %w", parentPath, owner, repo, err) |
| 769 | } |
| 770 | |
| 771 | var treeSHA string |
| 772 | for _, entry := range contents { |
| 773 | if entry.Name == skillName && entry.Type == "dir" { |
| 774 | treeSHA = entry.SHA |
| 775 | break |
| 776 | } |
| 777 | } |
| 778 | if treeSHA == "" { |
| 779 | return nil, fmt.Errorf("skill directory %q not found in %s/%s", skillPath, owner, repo) |
| 780 | } |
| 781 | |
| 782 | skillTreePath, err := safeurl.JoinPath("repos", owner, repo, "git", "trees", treeSHA) |
| 783 | if err != nil { |
| 784 | return nil, err |
| 785 | } |
| 786 | var skillTree treeResponse |
| 787 | if err := client.REST(host, "GET", skillTreePath.String(), nil, &skillTree); err != nil { |
| 788 | return nil, fmt.Errorf("could not read skill directory: %w", err) |
| 789 | } |
| 790 | |
| 791 | var blobSHA string |
| 792 | for _, entry := range skillTree.Tree { |
| 793 | if entry.Path == "SKILL.md" && entry.Type == "blob" { |
| 794 | blobSHA = entry.SHA |
| 795 | break |
| 796 | } |
| 797 | } |
| 798 | if blobSHA == "" { |
| 799 | return nil, fmt.Errorf("no SKILL.md found in %s", skillPath) |
| 800 | } |
| 801 | |
| 802 | var namespace, convention string |