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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ install:
go install -ldflags "$(VERSION_LDFLAGS)" github.com/elastic/elastic-package

lint:
# TODO: Migrate to golangci-lint
go run honnef.co/go/tools/cmd/staticcheck ./...
go run github.com/ashanbrown/forbidigo/v2 -tests=false "^os.(Getwd|Chdir)$$" -- ./...

licenser:
go run github.com/elastic/go-licenser -license Elastic
Expand Down
36 changes: 29 additions & 7 deletions cmd/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,17 @@ 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)
}

packageRootPath, err := packages.FindPackageRoot()
packageRootPath, err := packages.FindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
Expand Down Expand Up @@ -294,15 +299,20 @@ func rallyCommandAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("getting package name and version failed, expected format: <package>-<version>: %w", err)
}

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

var packageRootPath string
if len(packageName) == 0 {
packageRootPath, 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 @@ -333,6 +343,7 @@ func rallyCommandAction(cmd *cobra.Command, args []string) error {
rally.WithVariant(variant),
rally.WithBenchmarkName(benchName),
rally.WithDataReindexing(dataReindex),
rally.WithWorkDir(cwd),
rally.WithPackageRootPath(packageRootPath),
rally.WithESAPI(esClient.API),
rally.WithKibanaClient(kc),
Expand Down Expand Up @@ -471,12 +482,17 @@ func streamCommandAction(cmd *cobra.Command, args []string) error {
return cobraext.FlagParsingError(err, cobraext.BenchStreamTimestampFieldFlagName)
}

packageRootPath, 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 @@ -581,7 +597,12 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
return cobraext.FlagParsingError(err, cobraext.BenchReindexToMetricstoreFlagName)
}

packageRootPath, 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 @@ -609,6 +630,7 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
}

withOpts := []system.OptionFunc{
system.WithWorkDir(cwd),
system.WithVariant(variant),
system.WithBenchmarkPath(benchPath),
system.WithBenchmarkName(benchName),
Expand Down
14 changes: 10 additions & 4 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,32 @@ 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{
WorkDir: cwd,
PackageRootPath: packageRoot,
BuildDir: buildDir,
CreateZip: createZip,
Expand All @@ -92,7 +98,7 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("building package failed: %w", err)
}

targets, err := docs.UpdateReadmes(repositoryRoot, packageRoot, buildDir)
targets, err := docs.UpdateReadmes(repositoryRoot, cwd, packageRoot, buildDir)
if err != nil {
return fmt.Errorf("updating files failed: %w", err)
}
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
7 changes: 6 additions & 1 deletion 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 Down
7 changes: 6 additions & 1 deletion cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ func editDashboardsCmd(cmd *cobra.Command, args []string) error {
fmt.Printf("Warning: %s\n", message)
}

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

if len(dashboardIDs) == 0 {
// Not mandatory to get the package name here, but it would be helpful for users
// to select by default the package where they are located if any.
defaultPackage := ""
packageRoot, err := packages.MustFindPackageRoot()
packageRoot, err := packages.MustFindPackageRoot(cwd)
if err == nil {
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions cmd/export_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {
fmt.Printf("Warning: %s\n", message)
}

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

// Just query for dashboards if none were provided as flags
if len(dashboardIDs) == 0 {
packageRoot, err := packages.MustFindPackageRoot()
packageRoot, err := packages.MustFindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
Expand All @@ -106,7 +111,7 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {
}
}

err = export.Dashboards(cmd.Context(), kibanaClient, dashboardIDs)
err = export.Dashboards(cmd.Context(), kibanaClient, cwd, dashboardIDs)
if err != nil {
return fmt.Errorf("dashboards export failed: %w", err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/export_ingest_pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func exportIngestPipelinesCmd(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
6 changes: 5 additions & 1 deletion cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ func setupFormatCommand() *cobraext.Command {

func formatCommandAction(cmd *cobra.Command, args []string) error {
cmd.Println("Format the package")
cwd, err := cobraext.Getwd(cmd)
if err != nil {
return err
}

packageRoot, err := packages.FindPackageRoot()
packageRoot, err := packages.FindPackageRoot(cwd)
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,27 @@ func installCommandAction(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not create kibana client: %w", err)
}

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

if zipPathFile == "" && packageRootPath == "" {
var err error
packageRootPath, 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)
}

installer, err := installer.NewForPackage(installer.Options{
Kibana: kibanaClient,
WorkDir: cwd,
PackageRootPath: packageRootPath,
SkipValidation: skipValidation,
ZipPath: zipPathFile,
Expand Down
Loading