Skip to content

Commit

Permalink
Merge pull request #5 from sudonym-btc/chore/git-clone-hook
Browse files Browse the repository at this point in the history
chore: add git and gh hooks to command listener
  • Loading branch information
PatrickGeyer authored Sep 17, 2023
2 parents 6eb579c + befd1bd commit cbc8fa3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 13 deletions.
4 changes: 1 addition & 3 deletions cmd/view/helper/task/task.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package task

import (
"fmt"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/gookit/slog"
Expand Down Expand Up @@ -155,7 +153,7 @@ func Failed(id int, err *error) func() tea.Msg {
}

func (m Model) Job() tea.Cmd {
fmt.Print("Job task")
slog.Error("Job task")
return nil
}

Expand Down
77 changes: 71 additions & 6 deletions cmd/view/tip/command-resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package tipView

import (
"fmt"
"regexp"

"github.com/gookit/slog"
"github.com/sudonym-btc/zap/cmd/view/helper/task"
packageAnalyzer "github.com/sudonym-btc/zap/service/package-analyzer"
"github.com/thoas/go-funk"
Expand Down Expand Up @@ -30,7 +34,7 @@ var SpecialCommandResolvers = []CommandResolver{
},
Command: "address",
}}
var PackageCommandResolvers = funk.Map(packageAnalyzer.PackageManagers, func(pm packageAnalyzer.PackageManager) CommandResolver {
var PackageCommandResolvers = append(funk.Map(packageAnalyzer.PackageManagers, func(pm packageAnalyzer.PackageManager) CommandResolver {
return CommandResolver{
Model: func(m TipModel) task.ModelI {
return InitialPackageManagerModel(pm, &m.name, &m.version, m)
Expand All @@ -39,7 +43,53 @@ var PackageCommandResolvers = funk.Map(packageAnalyzer.PackageManagers, func(pm
Detect: pm.Detect,
ParseArgs: pm.ParseArgs,
}
}).([]CommandResolver)
}).([]CommandResolver), CommandResolver{
Model: func(m TipModel) task.ModelI {
return initialGithubRepoModel(&m.name, m)
},
Command: "git",
Detect: func(dir string) bool {
return false
},
ParseArgs: func(args []string) (bool, *[]packageAnalyzer.PackageInfo, error) {
slog.Debug("Parsing args", args)
if len(args) > 0 && args[0] == "clone" {
slog.Debug("Found git clone command")
args = args[1:]
if len(args) > 0 {
packages := funk.Map(args, func(arg string) packageAnalyzer.PackageInfo {
return packageAnalyzer.PackageInfo{Name: extractOrgRepo(arg)}
}).([]packageAnalyzer.PackageInfo)
return true, &packages, nil
}
return true, nil, nil
}
return false, nil, nil
},
}, CommandResolver{
Model: func(m TipModel) task.ModelI {
return initialGithubRepoModel(&m.name, m)
},
Command: "gh",
Detect: func(dir string) bool {
return false
},
ParseArgs: func(args []string) (bool, *[]packageAnalyzer.PackageInfo, error) {
slog.Debug("Parsing args", args)
if len(args) > 0 && args[0] == "repo" && args[1] == "clone" {
slog.Debug("Found git clone command")
args = args[2:]
if len(args) > 0 {
packages := funk.Map(args, func(arg string) packageAnalyzer.PackageInfo {
return packageAnalyzer.PackageInfo{Name: arg}
}).([]packageAnalyzer.PackageInfo)
return true, &packages, nil
}
return true, nil, nil
}
return false, nil, nil
},
})

var CommandResolvers = append(SpecialCommandResolvers, PackageCommandResolvers...)

Expand All @@ -50,7 +100,22 @@ type CommandResolver struct {
ParseArgs func([]string) (bool, *[]packageAnalyzer.PackageInfo, error)
}

// type PackageCommandResolver struct {
// CommandResolver
// ParseArgs func([]string) (bool, *[]packageAnalyzer.PackageInfo, error)
// }
func extractOrgRepo(url string) string {
// Define the regular expression pattern to match GitHub URLs
pattern := `(?:https:\/\/github\.com\/|git@github\.com:)([\w-]+)\/([\w-]+)\.git`

// Compile the regular expression
regex := regexp.MustCompile(pattern)

// Find the matches in the URL
matches := regex.FindStringSubmatch(url)

// If there's a match, return the org/repo part, otherwise return an empty string
if len(matches) == 3 {
org := matches[1]
repo := matches[2]
return fmt.Sprintf("%s/%s", org, repo)
}

return ""
}
4 changes: 4 additions & 0 deletions cmd/view/tip/github-repo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tipView

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/google/go-github/github"
Expand Down Expand Up @@ -62,6 +64,8 @@ func (m GithubRepoModel) View() string {
return view.PadVertical.Render(lipgloss.JoinHorizontal(lipgloss.Left, view.PadRight.Render(m.Progress.Spinner.View()), "Loading repository..."))
} else if len(m.Children) > 0 {
return taskView.DisplayOnlyDoneOrInProgress(m.Children)
} else if m.Progress.Failed {
return lipgloss.JoinHorizontal(lipgloss.Left, view.Cross.Render(), view.ErrorMessageStyle("Failed fetching repo: "+fmt.Sprint(*m.Progress.Error)))
}

return "No users found"
Expand Down
9 changes: 6 additions & 3 deletions cmd/view/tip/github-user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tipView

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/google/go-github/github"
Expand Down Expand Up @@ -66,10 +68,11 @@ func (m GithubUserModel) View() string {

content := func() string {
return lipgloss.JoinVertical(lipgloss.Left, view.ItemStyle(m.name, ""), func() string {
if m.user == nil {
if m.Progress.InProgress {
return view.PadVertical.Render(lipgloss.JoinHorizontal(lipgloss.Left, view.PadRight.Render(m.Progress.Spinner.View()), "Loading user..."))
}
if len(m.Children) == 0 {
} else if m.Progress.Failed {
return lipgloss.JoinHorizontal(lipgloss.Left, view.Cross.Render(), view.ErrorMessageStyle("Failed fetching user: "+fmt.Sprint(*m.Progress.Error)))
} else if len(m.Children) == 0 {
return "No tippable addresses found"
}
return taskView.DisplayOnlyDoneOrInProgress(m.Children)
Expand Down
9 changes: 8 additions & 1 deletion service/github-manager/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ func (*GithubManager) FetchUser(name string) (*github.User, *github.Response, er

func (*GithubManager) FetchRepo(name string) (*github.Repository, *github.Response, error) {
slog.Debug("Fetching repo", name)
return client.Repositories.Get(context.Background(), strings.Split(name, "/")[0], strings.Split(name, "/")[1])
repo, response, err := client.Repositories.Get(context.Background(), strings.Split(name, "/")[0], strings.Split(name, "/")[1])
if err != nil {
slog.Warn("Failed fetching repo", err)
} else {
slog.Debug("Fetched repo", repo)
}

return repo, response, err
}

func (*GithubManager) FetchOrg(name string) (*github.Organization, *github.Response, error) {
Expand Down

0 comments on commit cbc8fa3

Please sign in to comment.