-
Notifications
You must be signed in to change notification settings - Fork 269
[zeroconfig] Implement --autodetect flag #2325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"go.jetpack.io/devbox/internal/autodetect/detector" | ||
"go.jetpack.io/devbox/internal/devbox" | ||
"go.jetpack.io/devbox/internal/devbox/devopt" | ||
) | ||
|
||
func PopulateConfig(ctx context.Context, path string, stderr io.Writer) error { | ||
pkgs, err := packages(ctx, path) | ||
if err != nil { | ||
return err | ||
} | ||
devbox, err := devbox.Open(&devopt.Opts{ | ||
Dir: path, | ||
Stderr: stderr, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return devbox.Add(ctx, pkgs, devopt.AddOpts{}) | ||
} | ||
|
||
func DryRun(ctx context.Context, path string, stderr io.Writer) error { | ||
pkgs, err := packages(ctx, path) | ||
if err != nil { | ||
return err | ||
} else if len(pkgs) == 0 { | ||
fmt.Fprintln(stderr, "No packages to add") | ||
return nil | ||
} | ||
fmt.Fprintln(stderr, "Packages to add:") | ||
for _, pkg := range pkgs { | ||
fmt.Fprintln(stderr, pkg) | ||
} | ||
return nil | ||
} | ||
|
||
func detectors(path string) []detector.Detector { | ||
return []detector.Detector{ | ||
&detector.PythonDetector{Root: path}, | ||
&detector.PoetryDetector{Root: path}, | ||
} | ||
} | ||
|
||
func packages(ctx context.Context, path string) ([]string, error) { | ||
mostRelevantDetector, err := relevantDetector(path) | ||
if err != nil || mostRelevantDetector == nil { | ||
return nil, err | ||
} | ||
return mostRelevantDetector.Packages(ctx) | ||
} | ||
|
||
// relevantDetector returns the most relevant detector for the given path. | ||
// We could modify this to return a list of detectors and their scores or | ||
// possibly grouped detectors by category (e.g. python, server, etc.) | ||
func relevantDetector(path string) (detector.Detector, error) { | ||
relevantScore := 0.0 | ||
var mostRelevantDetector detector.Detector | ||
for _, detector := range detectors(path) { | ||
score, err := detector.IsRelevant(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if score > relevantScore { | ||
relevantScore = score | ||
mostRelevantDetector = detector | ||
} | ||
} | ||
return mostRelevantDetector, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package detector | ||
|
||
import "context" | ||
|
||
type Detector interface { | ||
IsRelevant(path string) (float64, error) | ||
|
||
Packages(ctx context.Context) ([]string, error) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package detector | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"go.jetpack.io/devbox/internal/searcher" | ||
) | ||
|
||
type PoetryDetector struct { | ||
PythonDetector | ||
Root string | ||
} | ||
|
||
func (d *PoetryDetector) IsRelevant(path string) (float64, error) { | ||
pyprojectPath := filepath.Join(d.Root, "pyproject.toml") | ||
_, err := os.Stat(pyprojectPath) | ||
if err == nil { | ||
return d.maxRelevance(), nil | ||
} | ||
if os.IsNotExist(err) { | ||
return 0, nil | ||
} | ||
return 0, err | ||
} | ||
|
||
func (d *PoetryDetector) Packages(ctx context.Context) ([]string, error) { | ||
pyprojectPath := filepath.Join(d.Root, "pyproject.toml") | ||
pyproject, err := os.ReadFile(pyprojectPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var pyprojectToml struct { | ||
Tool struct { | ||
Poetry struct { | ||
Version string `toml:"version"` | ||
Dependencies struct { | ||
Python string `toml:"python"` | ||
} `toml:"dependencies"` | ||
} `toml:"poetry"` | ||
} `toml:"tool"` | ||
} | ||
err = toml.Unmarshal(pyproject, &pyprojectToml) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
poetryVersion := determineBestVersion(ctx, "poetry", pyprojectToml.Tool.Poetry.Version) | ||
pythonVersion := determineBestVersion(ctx, "python", pyprojectToml.Tool.Poetry.Dependencies.Python) | ||
|
||
return []string{"python@" + pythonVersion, "poetry@" + poetryVersion}, nil | ||
} | ||
|
||
func determineBestVersion(ctx context.Context, pkg, version string) string { | ||
if version == "" { | ||
return "latest" | ||
} | ||
|
||
version = sanitizeVersion(version) | ||
|
||
_, err := searcher.Client().ResolveV2(ctx, pkg, version) | ||
if err != nil { | ||
return "latest" | ||
} | ||
|
||
return version | ||
} | ||
|
||
func sanitizeVersion(version string) string { | ||
// Remove non-numeric characters and 'v' prefix | ||
sanitized := strings.TrimPrefix(version, "v") | ||
return regexp.MustCompile(`[^\d.]`).ReplaceAllString(sanitized, "") | ||
} | ||
|
||
func (d *PoetryDetector) maxRelevance() float64 { | ||
// this is arbitrary, but we want to prioritize poetry over python | ||
return d.PythonDetector.maxRelevance() + 1 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package detector | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type PythonDetector struct { | ||
Root string | ||
} | ||
|
||
func (d *PythonDetector) IsRelevant(path string) (float64, error) { | ||
requirementsPath := filepath.Join(d.Root, "requirements.txt") | ||
_, err := os.Stat(requirementsPath) | ||
if err == nil { | ||
return d.maxRelevance(), nil | ||
} | ||
if os.IsNotExist(err) { | ||
return 0, nil | ||
} | ||
return 0, err | ||
} | ||
|
||
func (d *PythonDetector) Packages(ctx context.Context) ([]string, error) { | ||
return []string{"python@latest"}, nil | ||
} | ||
|
||
func (d *PythonDetector) maxRelevance() float64 { | ||
return 1.0 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,17 @@ import ( | |
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"go.jetpack.io/devbox/internal/autodetect" | ||
"go.jetpack.io/devbox/internal/devbox" | ||
) | ||
|
||
type initFlags struct { | ||
autoDetect bool | ||
dryRun bool | ||
} | ||
|
||
func initCmd() *cobra.Command { | ||
flags := &initFlags{} | ||
command := &cobra.Command{ | ||
Use: "init [<dir>]", | ||
Short: "Initialize a directory as a devbox project", | ||
|
@@ -19,16 +26,32 @@ func initCmd() *cobra.Command { | |
"You can then add packages using `devbox add`", | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runInitCmd(args) | ||
return runInitCmd(cmd, args, flags) | ||
}, | ||
} | ||
|
||
command.Flags().BoolVar(&flags.autoDetect, "autodetect", false, "Automatically detect packages to add") | ||
|
||
command.Flags().BoolVar(&flags.dryRun, "dry-run", false, "Dry run") | ||
command.Flag("autodetect").Hidden = true | ||
|
||
return command | ||
} | ||
|
||
func runInitCmd(args []string) error { | ||
func runInitCmd(cmd *cobra.Command, args []string, flags *initFlags) error { | ||
path := pathArg(args) | ||
|
||
_, err := devbox.InitConfig(path) | ||
if flags.autoDetect && flags.dryRun { | ||
return autodetect.DryRun(cmd.Context(), path, cmd.ErrOrStderr()) | ||
} | ||
|
||
err := devbox.InitConfig(path) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
if flags.autoDetect { | ||
err = autodetect.PopulateConfig(cmd.Context(), path, cmd.ErrOrStderr()) | ||
} | ||
|
||
return errors.WithStack(err) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no concerns with the change. Seems a no-op, right?
Is there a setting or something that we can all use to align our tools?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
devbox does this automatically if I ever run any devbox command that mutates the config. I guess we haven't done that in a while in this repo.