Skip to content

Commit

Permalink
fix(scaffold): fix generated pkg.yaml and add limit and cmd options (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke authored Oct 24, 2023
1 parent 695026b commit eab46b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 48 deletions.
17 changes: 12 additions & 5 deletions pkg/cli/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ func (runner *Runner) newScaffoldCommand() *cli.Command {
return &cli.Command{
Name: "scaffold",
Usage: `Scaffold a package`,
UsageText: `$ aqua-registry scaffold [--deep] <package name>`,
UsageText: `$ aqua-registry scaffold [-limit <the number of versions] [-cmd <command>[,<command>...]] <package name>`,
Description: `Scaffold a package.
aqua >= v1.14.0 is required.
e.g.
$ aqua-registry scaffold cli/cli
Expand All @@ -36,12 +34,21 @@ This tool does the following things.
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "deep",
Usage: "Resolve version_overrides. aqua >= v1.34.0 is required",
Usage: "This flag was deprecated and had no meaning from aqua v2.15.0. This flag will be removed in aqua v3.0.0. https://github.com/aquaproj/aqua/issues/2351",
},
&cli.StringFlag{
Name: "cmd",
Usage: "A list of commands joined with single quotes ','",
},
&cli.IntFlag{
Name: "limit",
Aliases: []string{"l"},
Usage: "the maximum number of versions",
},
},
}
}

func (runner *Runner) scaffoldAction(c *cli.Context) error {
return scaffold.Scaffold(c.Context, c.Bool("deep"), c.Args().Slice()...) //nolint:wrapcheck
return scaffold.Scaffold(c.Context, c.String("cmd"), c.Int("limit"), c.Args().Slice()...) //nolint:wrapcheck
}
60 changes: 17 additions & 43 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package scaffold

import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

genrg "github.com/aquaproj/registry-tool/pkg/generate-registry"
Expand All @@ -19,7 +19,7 @@ const (
filePermission os.FileMode = 0o644
)

func Scaffold(ctx context.Context, deep bool, pkgNames ...string) error {
func Scaffold(ctx context.Context, cmds string, limit int, 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, pkgFile, rgFile, deep); err != nil {
if err := aquaGR(ctx, pkgName, pkgFile, rgFile, cmds, limit); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Update registry.yaml")
Expand All @@ -44,28 +44,29 @@ e.g. $ aqua-registry scaffold cli/cli`)
if err := aquaG(pkgFile); err != nil {
return err
}
if !deep {
if err := createPkgFile(ctx, pkgName, pkgFile); err != nil {
return err
}
}
return nil
}

func aquaGR(ctx context.Context, pkgName, pkgFilePath, rgFilePath string, deep bool) error {
func aquaGR(ctx context.Context, pkgName, pkgFilePath, rgFilePath string, cmds string, limit int) error {
outFile, err := os.Create(rgFilePath)
if err != nil {
return fmt.Errorf("create a file %s: %w", rgFilePath, err)
}
defer outFile.Close()
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)
}
command := fmt.Sprintf("+ aqua gr --out-testdata %s", pkgFilePath)
args := []string{"gr", "-out-testdata", pkgFilePath}
if cmds != "" {
args = append(args, "-cmd", cmds)
command += " -cmd " + cmds
}
if limit != 0 {
s := strconv.Itoa(limit)
args = append(args, "-limit", s)
command += " -limit " + s
}
fmt.Fprintf(os.Stderr, "%s %s > %s\n", command, pkgName, rgFilePath)
cmd = exec.CommandContext(ctx, "aqua", append(args, pkgName)...) //nolint:gosec
cmd.Stdout = outFile
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand All @@ -81,30 +82,3 @@ func aquaG(pkgFilePath string) error {
}
return nil
}

func createPkgFile(ctx context.Context, pkgName, pkgFilePath string) error {
outFile, err := os.Create(pkgFilePath)
if err != nil {
return fmt.Errorf("create a file %s: %w", pkgFilePath, err)
}
defer outFile.Close()
if _, err := outFile.WriteString("packages:\n"); err != nil {
return fmt.Errorf("write a string to file %s: %w", pkgFilePath, err)
}
buf := &bytes.Buffer{}
fmt.Fprintf(os.Stderr, "+ aqua -c aqua-all.yaml g %s >> %s\n", pkgName, pkgFilePath)
cmd := exec.CommandContext(ctx, "aqua", "-c", "aqua-all.yaml", "g", pkgName)
cmd.Stdout = buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("execute a command: aqua g %s: %w", pkgName, err)
}
txt := ""
for _, line := range strings.Split(strings.TrimSpace(buf.String()), "\n") {
txt += fmt.Sprintf(" %s\n", line)
}
if _, err := outFile.WriteString(txt); err != nil {
return fmt.Errorf("write a string to file %s: %w", pkgFilePath, err)
}
return nil
}

0 comments on commit eab46b7

Please sign in to comment.