Skip to content
Draft
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
32 changes: 32 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "2"

run:
timeout: 5m

linters:
default: standard
disable:
- errcheck # TODO: Fix these errors and enable it.
enable:
- forbidigo
- staticcheck
settings:
staticcheck:
checks:
- "SA*" # Only SA checks, matching original staticcheck default behavior
forbidigo:
forbid:
- pattern: ^os\.Getwd$
msg: "Please don't rely on current path in subcommands"
- pattern: ^os\.Chdir$
msg: "Please use absolute paths in subcommands"
- pattern: ^filepath\.Abs$
msg: "filepath.Abs relies on the current working path, what is not allowed in subcommands"
analyze-types: true
exclude-godoc-examples: true
exclusions:
rules:
# Exclude forbidigo from test files (matches original -tests=false behavior)
- linters:
- forbidigo
path: _test\.go$
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ install:
go install -ldflags "$(VERSION_LDFLAGS)" github.com/elastic/elastic-package

lint:
go run honnef.co/go/tools/cmd/staticcheck ./...
go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint run

licenser:
go run github.com/elastic/go-licenser -license Elastic
Expand Down
53 changes: 38 additions & 15 deletions cmd/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,18 @@ func pipelineCommandAction(cmd *cobra.Command, args []string) error {
return cobraext.FlagParsingError(err, cobraext.BenchNumTopProcsFlagName)
}

repositoryRoot, err := files.FindRepositoryRoot()
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

repositoryRoot, err := files.FindRepositoryRoot(cwd)
if err != nil {
return fmt.Errorf("locating repository root failed: %w", err)
}
defer repositoryRoot.Close()

packageRoot, err := packages.FindPackageRoot()
packageRootPath, err := packages.FindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
Expand All @@ -156,21 +161,21 @@ func pipelineCommandAction(cmd *cobra.Command, args []string) error {
if len(dataStreams) > 0 {
common.TrimStringSlice(dataStreams)

if err := validateDataStreamsFlag(packageRoot, dataStreams); err != nil {
if err := validateDataStreamsFlag(packageRootPath, dataStreams); err != nil {
return cobraext.FlagParsingError(err, cobraext.DataStreamsFlagName)
}
}

ctx, stop := signal.Enable(cmd.Context(), logger.Info)
defer stop()

benchFolders, err := pipeline.FindBenchmarkFolders(packageRoot, dataStreams)
benchFolders, err := pipeline.FindBenchmarkFolders(packageRootPath, dataStreams)
if err != nil {
return fmt.Errorf("unable to determine benchmark folder paths: %w", err)
}

if useTestSamples {
testFolders, err := testrunner.FindTestFolders(packageRoot, dataStreams, testrunner.TestType(pipeline.BenchType))
testFolders, err := testrunner.FindTestFolders(packageRootPath, dataStreams, testrunner.TestType(pipeline.BenchType))
if err != nil {
return fmt.Errorf("unable to determine test folder paths: %w", err)
}
Expand Down Expand Up @@ -203,7 +208,8 @@ func pipelineCommandAction(cmd *cobra.Command, args []string) error {
opts := pipeline.NewOptions(
pipeline.WithBenchmarkName(fmt.Sprintf("%s-%d", folder.Package, idx+1)),
pipeline.WithFolder(folder),
pipeline.WithPackageRoot(packageRoot),
pipeline.WithWorkDir(cwd),
pipeline.WithPackageRoot(packageRootPath),
pipeline.WithESAPI(esClient.API),
pipeline.WithNumTopProcs(numTopProcs),
pipeline.WithFormat(reportFormat),
Expand Down Expand Up @@ -295,15 +301,20 @@ func rallyCommandAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("getting package name and version failed, expected format: <package>-<version>: %w", err)
}

var packageRoot string
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

var packageRootPath string
if len(packageName) == 0 {
packageRoot, err = packages.FindPackageRoot()
packageRootPath, err = packages.FindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
}

repositoryRoot, err := files.FindRepositoryRoot()
repositoryRoot, err := files.FindRepositoryRoot(cwd)
if err != nil {
return fmt.Errorf("locating repository root failed: %w", err)
}
Expand Down Expand Up @@ -335,7 +346,8 @@ func rallyCommandAction(cmd *cobra.Command, args []string) error {
rally.WithVariant(variant),
rally.WithBenchmarkName(benchName),
rally.WithDataReindexing(dataReindex),
rally.WithPackageRoot(packageRoot),
rally.WithWorkDir(cwd),
rally.WithPackageRootPath(packageRootPath),
rally.WithESAPI(esClient.API),
rally.WithKibanaClient(kc),
rally.WithProfile(profile),
Expand Down Expand Up @@ -473,12 +485,17 @@ func streamCommandAction(cmd *cobra.Command, args []string) error {
return cobraext.FlagParsingError(err, cobraext.BenchStreamTimestampFieldFlagName)
}

packageRoot, err := packages.FindPackageRoot()
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

packageRootPath, err := packages.FindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}

repositoryRoot, err := files.FindRepositoryRoot()
repositoryRoot, err := files.FindRepositoryRoot(cwd)
if err != nil {
return fmt.Errorf("locating repository root failed: %w", err)
}
Expand Down Expand Up @@ -514,7 +531,7 @@ func streamCommandAction(cmd *cobra.Command, args []string) error {
stream.WithPeriodDuration(periodDuration),
stream.WithPerformCleanup(performCleanup),
stream.WithTimestampField(timestampField),
stream.WithPackageRoot(packageRoot),
stream.WithPackageRootPath(packageRootPath),
stream.WithESAPI(esClient.API),
stream.WithKibanaClient(kc),
stream.WithProfile(profile),
Expand Down Expand Up @@ -584,7 +601,12 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
return cobraext.FlagParsingError(err, cobraext.BenchReindexToMetricstoreFlagName)
}

packageRoot, err := packages.FindPackageRoot()
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

packageRootPath, err := packages.FindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
Expand Down Expand Up @@ -612,13 +634,14 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
}

withOpts := []system.OptionFunc{
system.WithWorkDir(cwd),
system.WithVariant(variant),
system.WithBenchmarkPath(benchPath),
system.WithBenchmarkName(benchName),
system.WithDeferCleanup(deferCleanup),
system.WithMetricsInterval(metricsInterval),
system.WithDataReindexing(dataReindex),
system.WithPackageRoot(packageRoot),
system.WithPackageRootPath(packageRootPath),
system.WithESAPI(esClient.API),
system.WithKibanaClient(kc),
system.WithProfile(profile),
Expand Down
37 changes: 27 additions & 10 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package cmd
import (
"errors"
"fmt"
"path/filepath"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/builder"
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/docs"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
Expand Down Expand Up @@ -59,38 +61,53 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
}
}

repositoryRoot, err := files.FindRepositoryRoot()
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

repositoryRoot, err := files.FindRepositoryRoot(cwd)
if err != nil {
return fmt.Errorf("locating repository root failed: %w", err)
}
defer repositoryRoot.Close()

packageRoot, err := packages.MustFindPackageRoot()
packageRoot, err := packages.MustFindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}

// Currently the build directory is placed inside the repository build/ folder.
// In the future we might want to make this configurable.
buildDir, err := builder.BuildDirectory()
buildDir, err := builder.BuildDirectory(cwd)
if err != nil {
return fmt.Errorf("can't prepare build directory: %w", err)
}
logger.Debugf("Use build directory: %s", buildDir)

target, err := builder.BuildPackage(builder.BuildOptions{
PackageRoot: packageRoot,
BuildDir: buildDir,
CreateZip: createZip,
SignPackage: signPackage,
SkipValidation: skipValidation,
RepositoryRoot: repositoryRoot,
UpdateReadmes: true,
WorkDir: cwd,
PackageRootPath: packageRoot,
BuildDir: buildDir,
CreateZip: createZip,
SignPackage: signPackage,
SkipValidation: skipValidation,
RepositoryRoot: repositoryRoot,
})
if err != nil {
return fmt.Errorf("building package failed: %w", err)
}

targets, err := docs.UpdateReadmes(repositoryRoot, cwd, packageRoot, buildDir)
if err != nil {
return fmt.Errorf("updating files failed: %w", err)
}

for _, target := range targets {
fileName := filepath.Base(target)
cmd.Printf("%s file rendered: %s\n", fileName, target)
}

cmd.Printf("Package built: %s\n", target)

cmd.Println("Done")
Expand Down
7 changes: 6 additions & 1 deletion cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ func setupChangelogCommand() *cobraext.Command {
}

func changelogAddCmd(cmd *cobra.Command, args []string) error {
packageRoot, err := packages.MustFindPackageRoot()
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

packageRoot, err := packages.MustFindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ func setupCheckCommand() *cobraext.Command {
Long: checkLongDescription,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := cobraext.ComposeCommands(cmd, args,
setupLintCommand(),
setupBuildCommand(),
err := cobraext.ComposeCommandActions(cmd, args,
lintCommandAction,
validateSourceCommandAction,
buildCommandAction,
)
if err != nil {
return fmt.Errorf("checking package failed: %w", err)
Expand Down
11 changes: 8 additions & 3 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func cleanCommandAction(cmd *cobra.Command, args []string) error {
return err
}

target, err := cleanup.Build()
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

target, err := cleanup.Build(cwd)
if err != nil {
return fmt.Errorf("can't clean build resources: %w", err)
}
Expand All @@ -48,7 +53,7 @@ func cleanCommandAction(cmd *cobra.Command, args []string) error {
cmd.Printf("Build resources removed: %s\n", target)
}

target, err = cleanup.Stack()
target, err = cleanup.Stack(cwd)
if err != nil {
return fmt.Errorf("can't clean the development stack: %w", err)
}
Expand All @@ -64,7 +69,7 @@ func cleanCommandAction(cmd *cobra.Command, args []string) error {
cmd.Printf("Temporary service logs removed: %s\n", target)
}

target, err = cleanup.ServiceLogsIndependentAgents(profile)
target, err = cleanup.ServiceLogsIndependentAgents(profile, cwd)
if err != nil {
return fmt.Errorf("can't clean temporary service logs: %w", err)
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/create_data_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/packages/archetype"
"github.com/elastic/elastic-package/internal/tui"
Expand All @@ -36,8 +37,12 @@ type newDataStreamAnswers struct {

func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
cmd.Println("Create a new data stream")
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

packageRoot, err := packages.FindPackageRoot()
packageRoot, err := packages.FindPackageRoot(cwd)
if err != nil {
if errors.Is(err, packages.ErrPackageRootNotFound) {
return errors.New("package root not found, you can only create new data stream in the package context")
Expand All @@ -59,7 +64,7 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to obtain spec version from package manifest in \"%s\": %w", packageRoot, err)
}

qs := getInitialSurveyQuestionsForVersion(sv)
qs := getInitialSurveyQuestionsForVersion(sv, packageRoot)

var answers newDataStreamAnswers
err = tui.Ask(qs, &answers)
Expand Down Expand Up @@ -197,8 +202,8 @@ func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, package
}
}

func getInitialSurveyQuestionsForVersion(specVersion *semver.Version) []*tui.Question {
validator := tui.Validator{Cwd: "."}
func getInitialSurveyQuestionsForVersion(specVersion *semver.Version, cwd string) []*tui.Question {
validator := tui.Validator{Cwd: cwd}
qs := []*tui.Question{
{
Name: "name",
Expand Down
6 changes: 3 additions & 3 deletions cmd/create_data_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func TestGetSurveyQuestionsForVersion_BelowSemver3_2_0(t *testing.T) {
version := semver.MustParse("3.1.9")
questions := getInitialSurveyQuestionsForVersion(version)
questions := getInitialSurveyQuestionsForVersion(version, t.TempDir())

require.Len(t, questions, 3, "should return 3 questions for spec version < 3.2.0")

Expand All @@ -30,7 +30,7 @@ func TestGetSurveyQuestionsForVersion_BelowSemver3_2_0(t *testing.T) {

func TestGetSurveyQuestionsForVersion_EqualSemver3_2_0(t *testing.T) {
version := semver.MustParse("3.2.0")
questions := getInitialSurveyQuestionsForVersion(version)
questions := getInitialSurveyQuestionsForVersion(version, t.TempDir())

require.Len(t, questions, 4, "should return 4 questions for spec version >= 3.2.0")

Expand All @@ -40,7 +40,7 @@ func TestGetSurveyQuestionsForVersion_EqualSemver3_2_0(t *testing.T) {

func TestGetSurveyQuestionsForVersion_AboveSemver3_2_0(t *testing.T) {
version := semver.MustParse("3.3.0")
questions := getInitialSurveyQuestionsForVersion(version)
questions := getInitialSurveyQuestionsForVersion(version, t.TempDir())

require.Len(t, questions, 4, "should return 4 questions for spec version > 3.2.0")

Expand Down
Loading