Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ packages:
github.com/pilat/devbox/internal/git:
interfaces:
CommandRunner:
Service:
github.com/pilat/devbox/internal/pkg/fs:
interfaces:
FileSystem:
FileInfo:
DirEntry:
5 changes: 2 additions & 3 deletions cmd/devbox/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/spf13/cobra"
)
Expand All @@ -18,7 +17,7 @@ func init() {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand All @@ -42,7 +41,7 @@ func init() {

func runDestroy(ctx context.Context, p *project.Project) error {
fmt.Println("[*] Removing project...")
if err := manager.Destroy(ctx, p); err != nil {
if err := mgr.Destroy(ctx, p); err != nil {
return fmt.Errorf("failed to remove project: %w", err)
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var projectName string

var dockerClient client.APIClient
var apiService api.Compose
var mgr *manager.Manager

func main() {
for _, fn := range []func() error{
Expand All @@ -39,7 +40,8 @@ func initCobra() error {
root.PersistentFlags().StringVarP(&projectName, "name", "n", "", "Project name")

_ = root.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return manager.ListProjects(toComplete), cobra.ShellCompDirectiveNoFileComp
projects, _ := mgr.List(toComplete)
return projects, cobra.ShellCompDirectiveNoFileComp
})

return root.Execute()
Expand All @@ -60,14 +62,16 @@ func initDocker() error {

apiService = compose.NewComposeService(dockerCLI)

mgr = manager.New()

return nil
}

func validArgsWrapper(f func(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := context.Background()

_, err := manager.AutodetectProject(projectName)
_, err := mgr.AutodetectProject(ctx, projectName)

if err != nil && !cmd.Flags().Changed("name") { // not auto-detected and name is not even mentioned
return []string{"--name"}, cobra.ShellCompDirectiveNoFileComp
Expand Down
3 changes: 1 addition & 2 deletions cmd/devbox/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/spf13/cobra"
)
Expand All @@ -18,7 +17,7 @@ func init() {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/devbox/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/exec"
"path/filepath"

"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/spf13/cobra"
)
Expand All @@ -27,7 +26,7 @@ func init() {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
project, err := manager.AutodetectProject(projectName)
project, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return fmt.Errorf("failed to detect project: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/devbox/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/pilat/devbox/internal/app"
"github.com/pilat/devbox/internal/git"
"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/pilat/devbox/internal/table"
"github.com/spf13/cobra"
Expand All @@ -24,7 +23,7 @@ func init() {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/devbox/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strings"

"github.com/pilat/devbox/internal/manager"
"github.com/spf13/cobra"
)

Expand All @@ -31,8 +30,8 @@ func init() {
projectName = guessName(gitURL)
}

if err := runInit(projectName, gitURL, branch); err != nil {
return fmt.Errorf("failed to list projects: %w", err)
if err := runInit(ctx, projectName, gitURL, branch); err != nil {
return fmt.Errorf("failed to initialize project: %w", err)
}

return nil
Expand All @@ -45,9 +44,9 @@ func init() {
root.AddCommand(cmd)
}

func runInit(name, gitURL, branch string) error {
func runInit(ctx context.Context, name, gitURL, branch string) error {
fmt.Println("[*] Initializing project...")
if err := manager.Init(name, gitURL, branch); err != nil {
if err := mgr.Init(ctx, name, gitURL, branch); err != nil {
return fmt.Errorf("failed to init project: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/devbox/install_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/pilat/devbox/internal/manager"
"github.com/spf13/cobra"
)

Expand All @@ -17,7 +16,7 @@ func init() {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/devbox/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"

"github.com/pilat/devbox/internal/git"
"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/pilat/devbox/internal/table"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -35,16 +33,19 @@ func runList(ctx context.Context, filter string) error {
fmt.Println("")
fmt.Println(" Projects:")

projects := manager.ListProjects(filter)
projectNames, err := mgr.List(filter)
if err != nil {
return fmt.Errorf("failed to list projects: %w", err)
}

t := table.New("Name", "Message", "Author", "Date")
for _, projectName := range projects {
app, err := project.New(ctx, projectName, []string{"*"})
for _, projectName := range projectNames {
proj, err := mgr.Load(ctx, projectName, []string{"*"})
if err != nil {
return fmt.Errorf("failed to get project: %w", err)
}

g := git.New(app.WorkingDir)
g := git.New(proj.WorkingDir)
info, err := g.GetInfo(ctx)
if err != nil {
return fmt.Errorf("failed to get git info: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/devbox/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/docker/cli/cli/streams"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/spf13/cobra"
)
Expand All @@ -19,7 +18,7 @@ func init() {
Long: "That command will show logs of services in devbox project",
Args: cobra.MinimumNArgs(0),
ValidArgsFunction: validArgsWrapper(func(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -32,7 +31,7 @@ func init() {
return results, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down
23 changes: 13 additions & 10 deletions cmd/devbox/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func init() {
Long: "That command will mount source code to the project",
Args: cobra.MinimumNArgs(0),
ValidArgsFunction: validArgsWrapper(func(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

if sourceName == "" {
if sources, _, _ := manager.AutodetectSource(p, "", manager.AutodetectSourceForMount); len(sources) > 0 {
if result, _ := mgr.AutodetectSource(ctx, p, "", manager.AutodetectSourceForMount); result != nil && len(result.Sources) > 0 {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
}
Expand All @@ -34,26 +34,29 @@ func init() {
return []string{"--source"}, cobra.ShellCompDirectiveNoFileComp
}

// We are not suggesting --path since we assume that user wants to mount the current directory

return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}

sources, affectedServices, err := manager.AutodetectSource(p, sourceName, manager.AutodetectSourceForMount)
result, err := mgr.AutodetectSource(ctx, p, sourceName, manager.AutodetectSourceForMount)
if err != nil {
return fmt.Errorf("failed to autodetect source: %w", err)
}

if err := runMount(ctx, p, sources, targetPath); err != nil {
mountPath := targetPath
if mountPath == "" && result.LocalPath != "" {
mountPath = result.LocalPath
}

if err := runMount(ctx, p, result.Sources, mountPath); err != nil {
return fmt.Errorf("failed to mount source code: %w", err)
}

if err := runRestart(ctx, p, affectedServices, false); err != nil {
if err := runRestart(ctx, p, result.AffectedServices, false); err != nil {
return fmt.Errorf("failed to restart services: %w", err)
}

Expand All @@ -69,12 +72,12 @@ func init() {
cmd.PersistentFlags().StringVarP(&targetPath, "path", "p", "", "Path to mount")

_ = cmd.RegisterFlagCompletionFunc("source", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(context.Background(), projectName)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

return manager.GetLocalMountCandidates(p, toComplete), cobra.ShellCompDirectiveNoFileComp
return mgr.GetLocalMountCandidates(p, toComplete), cobra.ShellCompDirectiveNoFileComp
})

root.AddCommand(cmd)
Expand Down
5 changes: 3 additions & 2 deletions cmd/devbox/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/pilat/devbox/internal/table"
"github.com/spf13/cobra"
Expand All @@ -20,7 +19,7 @@ func init() {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down Expand Up @@ -57,10 +56,12 @@ func runPs(ctx context.Context, p *project.Project) error {
containers, err := apiService.Ps(ctx, p.Name, opts)
if err != nil {
errCh <- fmt.Errorf("failed to list services: %w", err)
return
}

if len(containers) == 0 {
errCh <- nil
return
}

for _, container := range containers {
Expand Down
7 changes: 3 additions & 4 deletions cmd/devbox/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/docker/compose/v2/pkg/api"
"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +20,7 @@ func init() {
Args: cobra.MinimumNArgs(0),
ValidArgsFunction: validArgsWrapper(suggestRunningServices),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down Expand Up @@ -57,7 +56,7 @@ func init() {
cmd.PersistentFlags().StringSliceVarP(&profiles, "profile", "p", []string{}, "Profile to use")

_ = cmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(context.Background(), projectName)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -69,7 +68,7 @@ func init() {
}

func suggestRunningServices(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/devbox/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/docker/compose/v2/pkg/api"
"github.com/pilat/devbox/internal/manager"
"github.com/pilat/devbox/internal/project"
"github.com/spf13/cobra"
)
Expand All @@ -19,7 +18,7 @@ func init() {
Long: "You can pass additional arguments to the scenario",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validArgsWrapper(func(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -32,7 +31,7 @@ func init() {
return p.GetScenarios(toComplete), cobra.ShellCompDirectiveNoFileComp
}),
RunE: runWrapper(func(ctx context.Context, cmd *cobra.Command, args []string) error {
p, err := manager.AutodetectProject(projectName)
p, err := mgr.AutodetectProject(ctx, projectName)
if err != nil {
return err
}
Expand Down
Loading