diff --git a/cmd/project_run.go b/cmd/project_run.go index daa9934..978dffc 100644 --- a/cmd/project_run.go +++ b/cmd/project_run.go @@ -1,6 +1,10 @@ package cmd import ( + "fmt" + "os" + "os/exec" + "github.com/spf13/cobra" ) @@ -18,11 +22,24 @@ func projectRunCmdRun(cmd *cobra.Command, args []string) { plog.Fatal("No project selected, nothing to open") } - plog.Info("Running Projet", plog.Args("lang", project.Language)) + lang := project.GetLanguage() - if project.Language == nil { + if lang == nil { plog.Fatal("GitLab isn't sure what language this project is... can't run.") } + + plog.Debug(fmt.Sprintf("Project is written in %s, %.2f%% coverage", lang.Name, lang.Percentage)) + + switch lang.Name { + case "Go": + cmdArgs := []string{"run", "."} // Run from cwd + cmdArgs = append(cmdArgs, args[1:]...) // Support flags from shell command + cmd := exec.CommandContext(cmd.Context(), "go", cmdArgs...) // Honor parent context + cmd.Env = os.Environ() + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Run() + } } func init() { diff --git a/contrib/gpm_func_omz.zsh b/contrib/gpm_func_omz.zsh index f71e025..13806e3 100644 --- a/contrib/gpm_func_omz.zsh +++ b/contrib/gpm_func_omz.zsh @@ -29,3 +29,5 @@ popen () { gitlab-project-manager project open $1 reset 2>&1 > /dev/null } + +alias prun="gitlab-project-manager project run" diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 19a7471..84dfd3f 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -31,10 +31,17 @@ type Project struct { AvatarURL string LastActivityAt time.Time Readme string - Language *string + Languages *ProjectLanguages gitRepo *git.Repository } +type ProjectLanguages []*ProjectLanguage + +type ProjectLanguage struct { + Name string + Percentage float32 +} + type User struct { ID int Username string @@ -61,6 +68,23 @@ func (p *Project) String() string { return fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace) } +func (p *Project) GetLanguage() *ProjectLanguage { + if p.Languages == nil { + return nil + } + + var lang *ProjectLanguage + var maxPcnt float32 + for _, p := range *p.Languages { + if p.Percentage > maxPcnt { + lang = p + } + maxPcnt = p.Percentage + } + + return lang +} + func (p *Project) SanitizedPath() string { return strings.Trim(p.PathWithNamespace, " '\"%<>|`") } @@ -197,7 +221,7 @@ func (c *Client) handleProjects(projects []*gitlab.Project) []*Project { AvatarURL: project.AvatarURL, LastActivityAt: *project.LastActivityAt, Readme: project.ReadmeURL, - Language: c.GetProjectLanguage(project), + Languages: c.GetProjectLanguages(project), } pList = append(pList, p) } @@ -206,23 +230,26 @@ func (c *Client) handleProjects(projects []*gitlab.Project) []*Project { // A nil return indicates an API error or GitLab doesn't know what // language the project uses. -func (c *Client) GetProjectLanguage(project *gitlab.Project) *string { +func (c *Client) GetProjectLanguages(project *gitlab.Project) *ProjectLanguages { l, _, e := c.gitlab.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx)) if e != nil { pterm.Error.Printfln("Failed requesting project languages: %s", e.Error()) return nil } - var mainLang *string - var mostUsed float32 - for name, p := range *l { - if p > mostUsed { - mostUsed = p - mainLang = &name + var pLangs ProjectLanguages + pLangs = make([]*ProjectLanguage, len(*l)) + + var i int + for name, pcnt := range *l { + pLangs[i] = &ProjectLanguage{ + Name: name, + Percentage: pcnt, } + i++ } - return mainLang + return &pLangs } func NewGitlabClient(ctx context.Context, host, token string) (*Client, error) {