Skip to content

Commit

Permalink
[plugins] fix info for packages with process-compose services (#1689)
Browse files Browse the repository at this point in the history
## Summary

We were relying on the package already being installed and in virtenv.
This change ensures it works when not installed as well.

## How was it tested?

`devbox info php`
  • Loading branch information
mikeland73 authored Dec 21, 2023
1 parent 862bebe commit 34c5f35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
21 changes: 15 additions & 6 deletions internal/plugin/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/samber/lo"
"go.jetpack.io/devbox/internal/devpkg"
"go.jetpack.io/devbox/internal/services"
)

func Readme(ctx context.Context,
Expand All @@ -39,7 +40,7 @@ func Readme(ctx context.Context,
return "", err
}

if err = printServices(cfg, buf, markdown); err != nil {
if err = printServices(cfg, pkg, buf, markdown); err != nil {
return "", err
}

Expand Down Expand Up @@ -72,17 +73,25 @@ func printReadme(cfg *config, w io.Writer, markdown bool) error {
return errors.WithStack(err)
}

func printServices(cfg *config, w io.Writer, markdown bool) error {
svcs, err := cfg.Services()
func printServices(cfg *config, pkg *devpkg.Package, w io.Writer, markdown bool) error {
_, contentPath := cfg.ProcessComposeYaml()
if contentPath == "" {
return nil
}
content, err := pkg.FileContent(contentPath)
if err != nil {
return errors.WithStack(err)
}
serviceNames, err := services.NamesFromProcessCompose(content)
if err != nil {
return errors.WithStack(err)
}
if len(svcs) == 0 {
if len(serviceNames) == 0 {
return nil
}
services := ""
for _, service := range svcs {
services += fmt.Sprintf("* %[1]s\n", service.Name)
for _, serviceName := range serviceNames {
services += fmt.Sprintf("* %[1]s\n", serviceName)
}

_, err = fmt.Fprintf(
Expand Down
10 changes: 5 additions & 5 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ type config struct {
} `json:"shell,omitempty"`
}

func (c *config) ProcessComposeYaml() (string, bool) {
for file := range c.CreateFiles {
func (c *config) ProcessComposeYaml() (string, string) {
for file, contentPath := range c.CreateFiles {
if strings.HasSuffix(file, "process-compose.yaml") || strings.HasSuffix(file, "process-compose.yml") {
return file, true
return file, contentPath
}
}
return "", false
return "", ""
}

func (c *config) Services() (services.Services, error) {
if file, ok := c.ProcessComposeYaml(); ok {
if file, _ := c.ProcessComposeYaml(); file != "" {
return services.FromProcessCompose(file)
}
return nil, nil
Expand Down
13 changes: 13 additions & 0 deletions internal/services/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/f1bonacc1/process-compose/src/types"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"go.jetpack.io/devbox/internal/cuecfg"
)
Expand Down Expand Up @@ -47,6 +48,18 @@ func FromProcessCompose(path string) (Services, error) {
return services, nil
}

func NamesFromProcessCompose(content []byte) ([]string, error) {
var processCompose types.Project
if err := yaml.Unmarshal(content, &processCompose); err != nil {
return nil, err
}
names := []string{}
for name := range processCompose.Processes {
names = append(names, name)
}
return names, nil
}

func lookupProcessCompose(projectDir, path string) string {
if path == "" {
path = projectDir
Expand Down

0 comments on commit 34c5f35

Please sign in to comment.