Skip to content
Open
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
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ header:
- 'main.go'

paths-ignore:
- 'cmd/task/run/testdata/**'
- 'internal/task/Taskfile.tmpl.yaml'
- 'internal/copier/readme/*.md'
- 'internal/workload/wapi/wapiignore.tmpl'
Expand Down
16 changes: 11 additions & 5 deletions cmd/dotenv/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ func setupTestRepo(t *testing.T) string {
err := cmd.Run()
require.NoError(t, err, "Failed to initialize git repository")

// Create .datarobot directory
datarobotDir := filepath.Join(repoDir, ".datarobot")
// Create .datarobot/answers directory to make IsTemplateDir return true
answersDir := filepath.Join(repoDir, ".datarobot", "answers")

err = os.MkdirAll(datarobotDir, 0o755)
require.NoError(t, err, "Failed to create .datarobot directory")
err = os.MkdirAll(answersDir, 0o755)
require.NoError(t, err, "Failed to create .datarobot/answers directory")

// Create .datarobot/cli directory for parakeet.yaml
cliDir := filepath.Join(repoDir, ".datarobot", "cli")

err = os.MkdirAll(cliDir, 0o755)
require.NoError(t, err, "Failed to create .datarobot/cli directory")

// Create parakeet.yaml with basic configuration
parakeetYaml := `root:
Expand All @@ -52,7 +58,7 @@ func setupTestRepo(t *testing.T) string {
optional: true
help: "A test variable"
`
parakeetPath := filepath.Join(datarobotDir, "parakeet.yaml")
parakeetPath := filepath.Join(cliDir, "parakeet.yaml")

err = os.WriteFile(parakeetPath, []byte(parakeetYaml), 0o600)
require.NoError(t, err, "Failed to create parakeet.yaml")
Expand Down
4 changes: 2 additions & 2 deletions cmd/dotenv/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func (suite *DotenvModelTestSuite) SetupTest() {
dir, _ := os.MkdirTemp("", "datarobot-config-test")
suite.tempDir = dir

datarobotDir := filepath.Join(dir, ".datarobot")
datarobotDir := filepath.Join(dir, ".datarobot", "cli")

err := os.MkdirAll(datarobotDir, os.ModePerm)
if err != nil {
suite.T().Errorf("Failed to create .datarobot directory: %v", err)
suite.T().Errorf("Failed to create .datarobot/cli directory: %v", err)
}

parakeetYamlName := filepath.Join(datarobotDir, "parakeet.yaml")
Expand Down
40 changes: 1 addition & 39 deletions cmd/task/compose/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/datarobot/cli/internal/cli"
Expand All @@ -37,7 +36,7 @@ var templatePath string
func RunE(_ *cobra.Command, _ []string) error {
taskfileName, ignoreTaskfile := detectExistingTaskfile()

discovery, err := createDiscovery(taskfileName)
discovery, err := task.NewDiscovery(taskfileName, templatePath)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)

Expand Down Expand Up @@ -89,43 +88,6 @@ func RunE(_ *cobra.Command, _ []string) error {
return nil
}

func createDiscovery(taskfileName string) (*task.Discovery, error) {
// Check for .Taskfile.template in the root directory if no template specified
autoTemplatePath := ".Taskfile.template"

if templatePath == "" {
if _, err := os.Stat(autoTemplatePath); err == nil {
templatePath = autoTemplatePath
fmt.Printf("Using auto-discovered template: %s\n", autoTemplatePath)
}
}

// If template is specified or found, use compose mode
if templatePath != "" {
absPath, err := validateTemplatePath(templatePath)
if err != nil {
return nil, fmt.Errorf("invalid template: %w", err)
}

return task.NewComposeDiscovery(taskfileName, absPath), nil
}

return task.NewTaskDiscovery(taskfileName), nil
}

func validateTemplatePath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("resolving template path: %w", err)
}

if _, err := os.Stat(absPath); os.IsNotExist(err) {
return "", fmt.Errorf("template file not found: %s", absPath)
}

return absPath, nil
}

// detectExistingTaskfile checks for existing Taskfile.yaml or Taskfile.yml
// and returns the name of the existing one, or defaults to Taskfile.yaml
func detectExistingTaskfile() (inUse, notInUse string) {
Expand Down
37 changes: 33 additions & 4 deletions cmd/task/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type taskRunOptions struct {
taskOpts task.RunOpts
}

const taskRunFromRootEnv = "DATAROBOT_CLI_TASK_RUN_FROM_ROOT"

// splitTaskArgs separates task names from additional arguments.
// Supports: dr run task1 task2 -- -flag1 -flag2
// Also auto-detects flags after task names if no explicit -- separator is present.
Expand Down Expand Up @@ -68,6 +70,32 @@ func splitTaskArgs(args []string) (taskNames []string, taskArgs []string) {
return taskNames, taskArgs
}

func taskfileForRun(dir string) (string, bool, error) {
if os.Getenv(taskRunFromRootEnv) != "" {
discovery := task.NewTaskDiscovery("Taskfile.gen.yaml")

rootTaskfile, err := discovery.Discover(dir, 2)

return rootTaskfile, false, err
}

discovery, err := task.NewDiscovery("Taskfile.gen.yaml", "")
if err != nil {
return "", false, err
}

discovery.PreferRootTaskfile = true

rootTaskfile, err := discovery.Discover(dir, 2)
if err != nil {
return "", false, err
}

usingRootTaskfile := filepath.Base(rootTaskfile) != "Taskfile.gen.yaml"

return rootTaskfile, usingRootTaskfile, nil
}

func Cmd() *cobra.Command {
var opts taskRunOptions

Expand Down Expand Up @@ -97,9 +125,9 @@ Examples:
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
binaryName := "task"
discovery := task.NewTaskDiscovery("Taskfile.gen.yaml")
taskNames, taskArgs := splitTaskArgs(args)

rootTaskfile, err := discovery.Discover(opts.Dir, 2)
rootTaskfile, usingRootTaskfile, err := taskfileForRun(opts.Dir)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, task.FormatDiscoveryError(err))

Expand Down Expand Up @@ -129,13 +157,14 @@ Examples:
return cli.ErrSilent
}

taskNames, taskArgs := splitTaskArgs(args)

if !opts.taskOpts.Silent {
log.Printf("Running task(s): %s\n", strings.Join(taskNames, ", "))
}

opts.taskOpts.TaskArgs = taskArgs
if usingRootTaskfile {
opts.taskOpts.Env = append(opts.taskOpts.Env, taskRunFromRootEnv+"=1")
}

err = runner.Run(taskNames, opts.taskOpts)
if err != nil { //nolint: nestif
Expand Down
Loading
Loading