Skip to content

Commit

Permalink
feat: 去掉gh.yml,直接把yaml文件写到merge命令里
Browse files Browse the repository at this point in the history
  • Loading branch information
xbpk3t committed Jul 30, 2024
1 parent 7a2af49 commit e3242f2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 60 deletions.
8 changes: 4 additions & 4 deletions cmd/f.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var ghCmd = &cobra.Command{
wf.FatalError(err)
}

ghs := gh.NewConfigRepoFile(data).ToRepos()
ghs := gh.NewConfigRepos(data).ToRepos()
repos = append(ghs, repos...)
if len(args) > 0 && strings.HasPrefix(args[0], "#") {
tags := repos.ExtractTags()
Expand Down Expand Up @@ -253,7 +253,7 @@ func RenderRepos(repos gh.Repos) (item *aw.Item) {
Valid(true).
Autocomplete(name).Icon(&aw.Icon{Value: iconPath})

docsURL := fmt.Sprintf("%s#%s", wf.Config.GetString("docs"), strings.ToLower(repo.Tag))
docsURL := fmt.Sprintf("%s/%s#%s", wf.Config.GetString("docs"), strings.ToLower(repo.Tag), strings.ToLower(repo.Type))

item.Cmd().Subtitle(fmt.Sprintf("Quicklook: %s", repoURL)).Arg(remark.String())
item.Opt().Subtitle(fmt.Sprintf("复制URL: %s", repoURL)).Arg(repoURL)
Expand All @@ -266,8 +266,8 @@ func RenderRepos(repos gh.Repos) (item *aw.Item) {
// 渲染des
// 也就是item中的subtitle
func renderReposDes(repo gh.Repository) (des strings.Builder) {
if repo.Tag != "" {
des.WriteString(fmt.Sprintf("[#%s]", repo.Tag))
if repo.Type != "" {
des.WriteString(fmt.Sprintf("[#%s]", repo.Type))
} else {
des.WriteString(repo.Des)
}
Expand Down
22 changes: 15 additions & 7 deletions gh-merge/cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hxhac/docs-alfred/utils"
"gopkg.in/yaml.v3"
"log"
"log/slog"
"os"

"github.com/spf13/cobra"
Expand All @@ -20,15 +21,15 @@ var mergeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
var cr gh.ConfigRepos

gf, err2 := utils.Fetch("https://cdn.hxha.xyz/f/gh/gh.yml")
if err2 != nil {
return
for _, fn := range ghFiles {
url := fmt.Sprintf("%s%s", URL, fn)
fx, err := utils.Fetch(url)
if err != nil {
slog.Error("Fetch Error: %s", slog.Any("URL", url))
}
cr = append(cr, gh.NewConfigRepos(fx)...)
}

// cr = append(cr, gh.NewConfigRepoFile(gf)...)

cr = gh.NewConfigRepos(gf)

// 定义输出文件路径
outputPath := "gh.yml"

Expand Down Expand Up @@ -58,6 +59,11 @@ func WriteYAMLToFile(data gh.ConfigRepos, outputPath string) error {
return nil
}

var (
URL string
ghFiles []string
)

func init() {
rootCmd.AddCommand(mergeCmd)

Expand All @@ -70,4 +76,6 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// mergeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
mergeCmd.Flags().StringVar(&URL, "url", "", "CDN Base URL")
mergeCmd.Flags().StringSliceVar(&ghFiles, "yf", []string{}, "gh.yml files")
}
50 changes: 12 additions & 38 deletions pkg/gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package gh
import (
"bytes"
"errors"
"fmt"
"github.com/hxhac/docs-alfred/utils"
"gopkg.in/yaml.v3"
"io"
"log"
"log/slog"
"slices"
"strings"
"time"
Expand All @@ -33,8 +30,9 @@ type Repository struct {
Name string `yaml:"name,omitempty"`
User string
Des string `yaml:"des,omitempty"` // 描述
Tag string `yaml:"tag"` // used to mark Type
Alias string `yaml:"alias,omitempty"` // 如果有alias,则直接渲染为alias,而不是[User/Name](URL)
Type string `yaml:"type"` // used to mark Type
Alias string `yaml:"alias,omitempty"` // 如果有alias,则直接渲染为[alias](URL),而不是[User/Name](URL)
Tag string `yaml:"file,omitempty"` // 原本的文件名
Qs Qs `yaml:"qs,omitempty"`
Cmd Cmd `yaml:"cmd,omitempty"`
// Pix []string `yaml:"pix"`
Expand Down Expand Up @@ -70,31 +68,7 @@ type Repos []Repository

type Gh []string

func NewConfigRepos(f []byte) (cr ConfigRepos) {
var gh Gh
err := yaml.Unmarshal(f, &gh)
if err != nil {
return nil
}

for _, s := range gh {
var fx []byte

// fp := fmt.Sprintf("data/gh/%s", s)
// fx, err = os.ReadFile(fp)
url := fmt.Sprintf("https://cdn.hxha.xyz/f/gh/%s", s)
fx, err = utils.Fetch(url)
if err != nil {
slog.Error("Fetch Error: %s", slog.Any("URL", url))
return nil
}

cr = append(cr, NewConfigRepoFile(fx)...)
}
return cr
}

func NewConfigRepoFile(f []byte) ConfigRepos {
func NewConfigRepos(f []byte) ConfigRepos {
var ghs ConfigRepos

d := yaml.NewDecoder(bytes.NewReader(f))
Expand Down Expand Up @@ -148,7 +122,7 @@ func (cr ConfigRepos) ToRepos() Repos {
// repo.User = splits[0]
// repo.Name = splits[1]
// repo.IsStar = true
// repo.Tag = config.Type
// repo.Type = config.Type
// repos = append(repos, repo)

if found {
Expand All @@ -157,7 +131,7 @@ func (cr ConfigRepos) ToRepos() Repos {
// repo.User = splits[0]
// repo.Name = splits[1]
// repo.IsStar = true
// repo.Tag = config.Type
// repo.Type = config.Type
// repos = append(repos, repo)
// } else if len(splits) > 2 {
// // 确保 splits 不是 nil 并且有足够的元素
Expand All @@ -167,7 +141,7 @@ func (cr ConfigRepos) ToRepos() Repos {
// repo.User = splits[0]
// repo.Name = splits[1] + "/" + strings.Join(splits[curator+1:], "/")
// repo.IsStar = true
// repo.Tag = config.Type
// repo.Type = config.Type
// repos = append(repos, repo)
// } else {
// log.Printf("Index Error: src not found in splits")
Expand All @@ -184,7 +158,7 @@ func (cr ConfigRepos) ToRepos() Repos {
repo.User = splits[0]
repo.Name = splits[1]
repo.IsStar = true
repo.Tag = config.Type
repo.Type = config.Type
repos = append(repos, repo)
} else {
log.Printf("URL Split Error: unexpected format: %s", repo.URL)
Expand All @@ -205,18 +179,18 @@ func (cr ConfigRepos) ToRepos() Repos {
func (rs Repos) ExtractTags() []string {
var tags []string
for _, repo := range rs {
if repo.Tag != "" && !slices.Contains(tags, repo.Tag) {
tags = append(tags, repo.Tag)
if repo.Type != "" && !slices.Contains(tags, repo.Type) {
tags = append(tags, repo.Type)
}
}
return tags
}

// QueryReposByTag Query Repos by Tag
// QueryReposByTag Query Repos by Type
func (rs Repos) QueryReposByTag(tag string) Repos {
var res Repos
for _, repo := range rs {
if repo.Tag == tag {
if repo.Type == tag {
res = append(res, repo)
}
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/work/works.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type DocsTemps []DocsTemp
// types :=
//
// dc = append(dc, DocsTemp{
// Tag: doc.Tag,
// Type: doc.Type,
// Types: make(Types, len(doc.Type)),
// })
//
Expand All @@ -110,20 +110,20 @@ type DocsTemps []DocsTemp
//
// // Extract types for each tag
// for _, doc := range d {
// if types, ok := docsTempsMap[doc.Tag]; ok {
// if types, ok := docsTempsMap[doc.Type]; ok {
// // Append type to existing tag
// types = append(types, Type{Name: doc.Type, Qs: doc.Qs})
// docsTempsMap[doc.Tag] = types
// docsTempsMap[doc.Type] = types
// } else {
// // Create new tag entry
// docsTempsMap[doc.Tag] = Types{Type{Name: doc.Type, Qs: doc.Qs}}
// docsTempsMap[doc.Type] = Types{Type{Name: doc.Type, Qs: doc.Qs}}
// }
// }
//
// // Convert map to slice
// docsTemps := make(DocsTemps, 0, len(docsTempsMap))
// for tag, types := range docsTempsMap {
// docsTemp := DocsTemp{Tag: tag, Types: types}
// docsTemp := DocsTemp{Type: tag, Types: types}
// docsTemps = append(docsTemps, docsTemp)
// }
//
Expand All @@ -136,22 +136,22 @@ type DocsTemps []DocsTemp
// // Extract types for each tag
// for i := 0; i < len(d); i++ {
// doc := d[i]
// if types, ok := docsTempsMap[doc.Tag]; ok {
// if types, ok := docsTempsMap[doc.Type]; ok {
// // Append type to existing tag
// types = append(types, Type{Name: doc.Type, Qs: doc.Qs})
// docsTempsMap[doc.Tag] = types
// docsTempsMap[doc.Type] = types
// } else {
// // Create new tag entry
// docsTempsMap[doc.Tag] = Types{Type{Name: doc.Type, Qs: doc.Qs}}
// docsTempsMap[doc.Type] = Types{Type{Name: doc.Type, Qs: doc.Qs}}
// }
// }
//
// // Convert map to slice while preserving order
// docsTemps := make(DocsTemps, len(docsTempsMap))
// for i := 0; i < len(d); i++ {
// doc := d[i]
// if types, ok := docsTempsMap[doc.Tag]; ok {
// docsTemp := DocsTemp{Tag: doc.Tag, Types: types}
// if types, ok := docsTempsMap[doc.Type]; ok {
// docsTemp := DocsTemp{Type: doc.Type, Types: types}
// docsTemps[i] = docsTemp
// }
// }
Expand Down
2 changes: 1 addition & 1 deletion yaml2md/cmd/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var ghCmd = &cobra.Command{
return
}

dfo := gh.NewConfigRepoFile(f)
dfo := gh.NewConfigRepos(f)
df := dfo.FilterReposMD()

// dfo.IsTypeQsEmpty()
Expand Down

0 comments on commit e3242f2

Please sign in to comment.