Skip to content

Commit

Permalink
refactor: resolve nilaway nil panic errors
Browse files Browse the repository at this point in the history
之前就遇到过这个问题,今天终于解决了。

1、为啥 local的其他hook都可以执行,但是这个不行呢

2、怎么配置才能执行?我不想写绝对路径,最好可以不修改这里的写法,只做配置。为啥我在terminal直接执行pre-commit不会报这个错,但是在IDEA里执行是会报错的?意思是真正的问题在于IDEA commit执行的shell不是我terminal的shell,那么应该怎么配置呢?

其实是因为golang bin不在~/.zshrc里

所以IDEA里识别不到这些用go install安装的cli
  • Loading branch information
xbpk3t committed Dec 29, 2024
1 parent 44685b9 commit 0e3a242
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 107 deletions.
52 changes: 28 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ repos:
types: [yaml]
language: python

# - repo: https://github.com/gitleaks/gitleaks
# rev: v8.19.2
# hooks:
# - id: gitleaks
# name: Scan Secrets
# entry: gitleaks detect --source . -v
# - repo: https://github.com/gitleaks/gitleaks
# rev: v8.19.2
# hooks:
# - id: gitleaks
# name: Scan Secrets
# entry: gitleaks detect --source . -v

- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.96.1
Expand Down Expand Up @@ -101,19 +101,35 @@ repos:
entry: gofumpt -l -w .
language: golang
types: [go]
pass_filenames: false

- id: go-mod-tidy
name: go-mod-tidy
entry: go mod tidy -v
language: golang
pass_filenames: false

# - id: betteralign
# name: betteralign
# entry: betteralign -apply ./...
# language: system
# types: [go]
# pass_filenames: false
- id: betteralign
name: betteralign
entry: betteralign -apply ./...
language: system
types: [go]
pass_filenames: false
additional_dependencies: []


- id: nilaway
name: nilaway
entry: nilaway ./...
language: golang
pass_filenames: false

- id: race-test
name: race-test
entry: go build -race cmd
language: golang
pass_filenames: false


# - id: go-test
# name: go-test
Expand All @@ -126,15 +142,3 @@ repos:
# entry: go vet -vettool=$(which shadow) -strict
# language: golang
# pass_filenames: false

# - id: nilaway
# name: nilaway
# entry: nilaway ./...
# language: golang
# pass_filenames: false

- id: race-test
name: race-test
entry: go build -race cmd
language: golang
pass_filenames: false
67 changes: 61 additions & 6 deletions alfred/cmd/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,29 @@ var ghCmd = &cobra.Command{
// 主命令处理函数
func handleGhCommand(cmd *cobra.Command, args []string) {
builder := alfred.NewItemBuilder(wf)
r, _ := parser.NewParser[gh2.ConfigRepos](data).ParseSingle()
r, err := parser.NewParser[gh2.ConfigRepos](data).ParseSingle()
if err != nil {
builder.BuildBasicItem(
"Error parsing config",
err.Error(),
"",
cons.IconError,
)
wf.SendFeedback()
return
}

repos := r.WithType().ToRepos()
if repos == nil {
builder.BuildBasicItem(
"Invalid configuration",
"No repositories found in config",
"",
cons.IconWarning,
)
wf.SendFeedback()
return
}

if len(args) > 0 && strings.HasPrefix(args[0], "#") {
handleTagSearch(repos, args, builder)
Expand All @@ -43,15 +64,47 @@ func handleGhCommand(cmd *cobra.Command, args []string) {

// 处理标签搜索
func handleTagSearch(repos gh2.Repos, args []string, builder *alfred.ItemBuilder) {
if repos == nil {
builder.BuildBasicItem(
"Invalid configuration",
"No repositories found",
"",
cons.IconWarning,
)
wf.SendFeedback()
return
}

// 参数验证
if len(args) == 0 || !strings.HasPrefix(args[0], "#") {
renderTagItems(repos.ExtractTags())
tags := repos.ExtractTags()
if len(tags) == 0 {
builder.BuildBasicItem(
"No tags found",
"No tags available in repositories",
"",
cons.IconWarning,
)
} else {
renderTagItems(tags)
}
wf.SendFeedback()
return
}

// 提取标签
tags := repos.ExtractTags()
if len(tags) == 0 {
builder.BuildBasicItem(
"No tags found",
"No tags available in repositories",
"",
cons.IconWarning,
)
wf.SendFeedback()
return
}

ptag := strings.TrimPrefix(args[0], "#")

// 如果输入的标签存在
Expand All @@ -60,10 +113,12 @@ func handleTagSearch(repos gh2.Repos, args []string, builder *alfred.ItemBuilder
if len(filteredRepos) > 0 {
renderRepos(filteredRepos, builder)
} else {
// 没有找到相关仓库时显示提示
wf.NewItem("No repositories found").
Subtitle(fmt.Sprintf("No repositories found with tag: %s", ptag)).
Icon(aw.IconWarning)
builder.BuildBasicItem(
"No repositories found",
fmt.Sprintf("No repositories found with tag: %s", ptag),
"",
cons.IconWarning,
)
}
} else {
// 显示所有标签并根据输入进行过滤
Expand Down
55 changes: 44 additions & 11 deletions alfred/cmd/ws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"fmt"

aw "github.com/deanishe/awgo"
"github.com/spf13/cobra"
"github.com/xbpk3t/docs-alfred/alfred/internal/alfred"
"github.com/xbpk3t/docs-alfred/alfred/internal/cons"
Expand All @@ -9,7 +12,7 @@ import (

var wsCmd = &cobra.Command{
Use: "ws",
Short: "Workspace search command",
Short: "Searching from workspaces",
PersistentPreRun: handlePreRun,
Run: handleWsCommand,
}
Expand All @@ -19,20 +22,50 @@ func handleWsCommand(cmd *cobra.Command, args []string) {

f, err := ws.ParseConfig(data)
if err != nil {
wf.FatalError(err)
wf.NewItem("Error parsing config").
Subtitle(err.Error()).
Icon(aw.IconError)
wf.SendFeedback()
return
}

tks := f.Search(args)
if f == nil {
wf.NewItem("Invalid configuration").
Subtitle("No workspace configuration found").
Icon(aw.IconWarning)
wf.SendFeedback()
return
}

for _, tk := range tks {
item := builder.BuildBasicItem(
tk.Name,
tk.Des,
tk.URL,
cons.IconCheck,
)
builder.AddCommonModifiers(item, tk.URL, tk.Des)
if len(args) > 0 {
items := f.Search(args)
if len(items) == 0 {
wf.NewItem("No matching workspaces found").
Subtitle(fmt.Sprintf("No workspaces found matching: %s", args[0])).
Icon(aw.IconWarning)
} else {
renderURLItems(items, builder)
}
} else {
renderURLItems(f.ExtractURLs(), builder)
}

wf.SendFeedback()
}

func renderURLItems(items []ws.URL, builder *alfred.ItemBuilder) {
for _, item := range items {
name := item.Name
if name == "" {
name = item.URL
}

wfItem := builder.BuildBasicItem(
name,
item.Des,
item.URL,
cons.IconCheck,
)
builder.AddCommonModifiers(wfItem, item.URL, item.Des)
}
}
12 changes: 7 additions & 5 deletions alfred/internal/cons/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cons

const (
// Icons
IconCheck = "icons/check.svg"
IconSearch = "icons/search.svg"
IconQs = "icons/a.svg"
IconDoc = "icons/b.svg"
IconQsDoc = "icons/ab.svg"
IconCheck = "icons/check.svg"
IconSearch = "icons/search.svg"
IconQs = "icons/a.svg"
IconDoc = "icons/b.svg"
IconQsDoc = "icons/ab.svg"
IconError = "icons/error.png"
IconWarning = "icons/warning.png"

// URLs
GithubSearchURL = "https://github.com/search?q=%s&type=repositories"
Expand Down
Loading

0 comments on commit 0e3a242

Please sign in to comment.