Skip to content

Commit

Permalink
Merge pull request #307 from aquaproj/feat/support-scaffold-deep
Browse files Browse the repository at this point in the history
feat(scaffold): support --deep
  • Loading branch information
suzuki-shunsuke authored Feb 26, 2023
2 parents 04591ac + 1852f53 commit cc4161b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
run:
timeout: 2m
linters:
enable-all: true
disable:
Expand Down
10 changes: 8 additions & 2 deletions pkg/cli/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (runner *Runner) newScaffoldCommand() *cli.Command {
return &cli.Command{
Name: "scaffold",
Usage: `Scaffold a package`,
UsageText: `$ aqua-registry scaffold <package name>`,
UsageText: `$ aqua-registry scaffold [--deep] <package name>`,
Description: `Scaffold a package.
aqua >= v1.14.0 is required.
Expand All @@ -33,9 +33,15 @@ This tool does the following things.
6. aqua i
`,
Action: runner.scaffoldAction,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "deep",
Usage: "Resolve version_overrides. aqua >= v1.34.0 is required",
},
},
}
}

func (runner *Runner) scaffoldAction(c *cli.Context) error {
return scaffold.Scaffold(c.Context, c.Args().Slice()...) //nolint:wrapcheck
return scaffold.Scaffold(c.Context, c.Bool("deep"), c.Args().Slice()...) //nolint:wrapcheck
}
22 changes: 15 additions & 7 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
filePermission os.FileMode = 0o644
)

func Scaffold(ctx context.Context, pkgNames ...string) error {
func Scaffold(ctx context.Context, deep bool, pkgNames ...string) error {
if len(pkgNames) != 1 {
return errors.New(`usage: $ aqua-registry scaffold <pkgname>
e.g. $ aqua-registry scaffold cli/cli`)
Expand All @@ -31,7 +31,7 @@ e.g. $ aqua-registry scaffold cli/cli`)
if err := os.MkdirAll(pkgDir, dirPermission); err != nil {
return fmt.Errorf("create directories: %w", err)
}
if err := aquaGR(ctx, pkgName, rgFile); err != nil {
if err := aquaGR(ctx, pkgName, pkgFile, rgFile, deep); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Update registry.yaml")
Expand All @@ -44,23 +44,31 @@ e.g. $ aqua-registry scaffold cli/cli`)
if err := aquaG(ctx, pkgName); err != nil {
return err
}
if err := createPkgFile(ctx, pkgName, pkgFile); err != nil {
return err
if !deep {
if err := createPkgFile(ctx, pkgName, pkgFile); err != nil {
return err
}
}
if err := aquaI(ctx); err != nil {
return err
}
return nil
}

func aquaGR(ctx context.Context, pkgName, rgFilePath string) error {
func aquaGR(ctx context.Context, pkgName, pkgFilePath, rgFilePath string, deep bool) error {
outFile, err := os.Create(rgFilePath)
if err != nil {
return fmt.Errorf("create a file %s: %w", rgFilePath, err)
}
defer outFile.Close()
fmt.Fprintf(os.Stderr, "+ aqua gr %s > %s\n", pkgName, rgFilePath)
cmd := exec.CommandContext(ctx, "aqua", "gr", pkgName)
var cmd *exec.Cmd
if deep {
fmt.Fprintf(os.Stderr, "+ aqua gr --deep --out-testdata %s %s > %s\n", pkgFilePath, pkgName, rgFilePath)
cmd = exec.CommandContext(ctx, "aqua", "gr", "--deep", "--out-testdata", pkgFilePath, pkgName)
} else {
fmt.Fprintf(os.Stderr, "+ aqua gr %s > %s\n", pkgName, rgFilePath)
cmd = exec.CommandContext(ctx, "aqua", "gr", pkgName)
}
cmd.Stdout = outFile
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down

0 comments on commit cc4161b

Please sign in to comment.