Skip to content
Merged
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
10 changes: 8 additions & 2 deletions internal/envbuilder/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,21 @@ func VariablesFromLines(lines []string) ([]Variable, string) {

if v.Name != "" && v.Commented {
variables = append(variables, v)

contents.WriteString(line)

continue
}

if v.Name == "" || v.Commented {
contents.WriteString(line)
continue
}

v.Value = variablesConfig[v.Name].value
v.Secret = variablesConfig[v.Name].secret
if varConfig, ok := variablesConfig[v.Name]; ok {
v.Value = varConfig.value
v.Secret = varConfig.secret
}

if v.Value == "" {
contents.WriteString(line)
Expand Down
87 changes: 87 additions & 0 deletions internal/envbuilder/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2025 DataRobot, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package envbuilder

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestVariablesFromLines(t *testing.T) {
t.Run("Returns valid values", func(t *testing.T) {
lines := []string{
"NAME0=\"value0\"\n",
"NAME1=\"value1\"\n",
"#NAME2=\"value2\"\n",
"DATAROBOT_ENDPOINT=\"\"\n",
"DATAROBOT_API_TOKEN=\"\"\n",
}
contentsExpected := "NAME0=\"value0\"\nNAME1=\"value1\"\n#NAME2=\"value2\"\nDATAROBOT_ENDPOINT=\"\"\nDATAROBOT_API_TOKEN=\"\"\n"

variables, contents := VariablesFromLines(lines)

assert.Len(t, variables, 5)

i := 0
assert.Equal(t, "NAME0", variables[i].Name)
assert.Equal(t, "value0", variables[i].Value)
assert.False(t, variables[i].Commented)
assert.False(t, variables[i].Secret)

i++
assert.Equal(t, "NAME1", variables[i].Name)
assert.Equal(t, "value1", variables[i].Value)
assert.False(t, variables[i].Commented)
assert.False(t, variables[i].Secret)

i++
assert.Equal(t, "NAME2", variables[i].Name)
assert.Equal(t, "value2", variables[i].Value)
assert.True(t, variables[i].Commented)
assert.False(t, variables[i].Secret)

i++
assert.Equal(t, "DATAROBOT_ENDPOINT", variables[i].Name)
assert.Empty(t, variables[i].Value)
assert.False(t, variables[i].Commented)
assert.False(t, variables[i].Secret)

i++
assert.Equal(t, "DATAROBOT_API_TOKEN", variables[i].Name)
assert.Empty(t, variables[i].Value)
assert.False(t, variables[i].Commented)
assert.True(t, variables[i].Secret)

assert.Equal(t, contentsExpected, contents)
})
}

func TestNewFromLine(t *testing.T) {
t.Run("Returns valid values", func(t *testing.T) {
unquotedValues := map[string]string{
"NAME1": "value1",
"NAME2": "value2",
}

value1 := NewFromLine("NAME1=\"value1\"\n", unquotedValues)

assert.Equal(t, "value1", value1.Value)

value2 := NewFromLine("NAME2=\"value2\"\n", unquotedValues)

assert.Equal(t, "value2", value2.Value)
})
}
32 changes: 30 additions & 2 deletions internal/repo/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"errors"
"os"
"path/filepath"
"slices"

"github.com/charmbracelet/log"
"github.com/datarobot/cli/internal/fsutil"
)

Expand Down Expand Up @@ -60,9 +62,35 @@ func FindRepoRoot() (string, error) {
}
}

// detectTemplate checks if .datarobot/answers exists in dir directory
// detectTemplate checks if .datarobot/answers or .datarobot/cli exists in dir directory
func detectTemplate(dir string) bool {
return fsutil.DirExists(filepath.Join(dir, DataRobotTemplateDetectPath))
answersDirPresent := fsutil.DirExists(filepath.Join(dir, DataRobotTemplateDetectAnswersPath))
if answersDirPresent {
log.Debugf("Directory %s exists, treating %s as template", DataRobotTemplateDetectAnswersPath, dir)
return true
}

entries, err := os.ReadDir(filepath.Join(dir, DataRobotTemplateDetectCliPath))
if err != nil {
return false
}

if len(entries) == 0 {
log.Debugf("Empty CLI configuration directory %s exists, treating %s as template", DataRobotTemplateDetectCliPath, dir)
}

// Older versions were incorrectly creating state.yaml file outside of template directories
// return true if any file other than state.yaml exists in .datarobot/cli
cliConfigDirPresent := slices.ContainsFunc(entries, func(entry os.DirEntry) bool {
return entry.Name() != "state.yaml"
})

if cliConfigDirPresent {
log.Debugf("CLI configuration files present, treating %s as template", dir)
return true
}

return false
}

// IsInRepo checks if the current directory is inside a DataRobot repository
Expand Down
73 changes: 67 additions & 6 deletions internal/repo/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,40 @@ func (suite *DetectTestSuite) TearDownTest() {
}
}

func (suite *DetectTestSuite) createAnswers(dir string) {
func (suite *DetectTestSuite) createAnswersDir() {
// Create .datarobot/answers directory
err := os.MkdirAll(filepath.Join(dir, ".datarobot", "answers"), 0o755)
err := os.MkdirAll(filepath.Join(suite.tempDir, ".datarobot", "answers"), 0o755)
suite.Require().NoError(err)
}

func (suite *DetectTestSuite) createCliDir() {
// Create .datarobot/cli directory
err := os.MkdirAll(filepath.Join(suite.tempDir, ".datarobot", "cli"), 0o755)
suite.Require().NoError(err)
}

func (suite *DetectTestSuite) createCliVersionsYaml() {
suite.createCliDir()
// Create .datarobot/cli/versions.yaml file
versionsFile := filepath.Join(suite.tempDir, ".datarobot", "cli", "versions.yaml")
file, err := os.OpenFile(versionsFile, os.O_RDONLY|os.O_CREATE, 0o644)
suite.Require().NoError(err)
err = file.Close()
suite.Require().NoError(err)
}

func (suite *DetectTestSuite) createCliStateYaml() {
suite.createCliDir()
// Create .datarobot/cli/state.yaml file
stateFile := filepath.Join(suite.tempDir, ".datarobot", "cli", "state.yaml")
file, err := os.OpenFile(stateFile, os.O_RDONLY|os.O_CREATE, 0o644)
suite.Require().NoError(err)
err = file.Close()
suite.Require().NoError(err)
}

func (suite *DetectTestSuite) TestFindRepoRootFindsDataRobotCLI() {
suite.createAnswers(suite.tempDir)
suite.createAnswersDir()

// Change to temp directory
err := os.Chdir(suite.tempDir)
Expand All @@ -85,7 +111,7 @@ func (suite *DetectTestSuite) TestFindRepoRootFindsDataRobotCLI() {
}

func (suite *DetectTestSuite) TestFindRepoRootFromNestedDirectory() {
suite.createAnswers(suite.tempDir)
suite.createAnswersDir()

// Create nested directory structure
nestedPath := filepath.Join(suite.tempDir, "src", "components", "deep")
Expand Down Expand Up @@ -121,8 +147,32 @@ func (suite *DetectTestSuite) TestFindRepoRootNotInRepo() {
suite.Empty(repoRoot)
}

func (suite *DetectTestSuite) TestIsInRepoReturnsTrueWhenInRepo() {
suite.createAnswers(suite.tempDir)
func (suite *DetectTestSuite) TestIsInRepoReturnsTrueWhenInRepoAnswers() {
suite.createAnswersDir()

// Change to temp directory
err := os.Chdir(suite.tempDir)
suite.Require().NoError(err)

// Should return true
suite.True(repo.IsInRepo())
}

func (suite *DetectTestSuite) TestIsInRepoReturnsTrueWhenInRepoVersions() {
suite.createCliVersionsYaml()

// Change to temp directory
err := os.Chdir(suite.tempDir)
suite.Require().NoError(err)

// Should return true
suite.True(repo.IsInRepo())
}

func (suite *DetectTestSuite) TestIsInRepoReturnsTrueWhenInRepoAll() {
suite.createAnswersDir()
suite.createCliVersionsYaml()
suite.createCliStateYaml()

// Change to temp directory
err := os.Chdir(suite.tempDir)
Expand All @@ -140,3 +190,14 @@ func (suite *DetectTestSuite) TestIsInRepoReturnsFalseWhenNotInRepo() {
// Should return false
suite.False(repo.IsInRepo())
}

func (suite *DetectTestSuite) TestIsInRepoReturnsFalseWhenOnlyStateYaml() {
suite.createCliStateYaml()

// Change to temp directory
err := os.Chdir(suite.tempDir)
suite.Require().NoError(err)

// Should return false
suite.False(repo.IsInRepo())
}
6 changes: 4 additions & 2 deletions internal/repo/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package repo

const (
// DataRobotTemplateDetectPath is the path to the answers folder relative to CWD
DataRobotTemplateDetectPath = ".datarobot/answers"
// DataRobotTemplateDetectAnswersPath is the path to the answers folder relative to CWD
DataRobotTemplateDetectAnswersPath = ".datarobot/answers"
// DataRobotTemplateDetectCliPath path to the CLI config folder relative to CWD
DataRobotTemplateDetectCliPath = ".datarobot/cli"
// QuickstartScriptPath is the path to the quickstart scripts directory relative to CWD
QuickstartScriptPath = ".datarobot/cli/bin"
// LocalPluginDir is the project-local plugin directory relative to CWD
Expand Down
Loading