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
34 changes: 23 additions & 11 deletions internal/super/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,36 @@ func validateCurrentDirectoryForInit() error {
}

// resolveTargetDirectory determines the target directory for the Flow project
// based on user input. Empty input means current directory.
// based on user input by resolving a candidate path and checking if it equals
// the current working directory.
func resolveTargetDirectory(userInput string) (string, error) {
if strings.TrimSpace(userInput) == "" {
// Validate current directory for Flow project conflicts
pwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get current working directory: %w", err)
}

trimmed := strings.TrimSpace(userInput)

// Build a candidate absolute path for comparison
var candidate string
if trimmed == "" {
candidate = pwd
} else if filepath.IsAbs(trimmed) {
candidate = filepath.Clean(trimmed)
} else {
candidate = filepath.Clean(filepath.Join(pwd, trimmed))
}

// If candidate resolves to current directory, validate and use it
if candidate == filepath.Clean(pwd) {
if err := validateCurrentDirectoryForInit(); err != nil {
return "", err
}

// Use current directory
pwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get current working directory: %w", err)
}
return pwd, nil
}

// Use provided name to create new directory
return getTargetDirectory(userInput)
// Otherwise, use provided name/path to create or validate new directory
return getTargetDirectory(trimmed)
}

func updateGitignore(targetDir string, readerWriter flowkit.ReaderWriter) error {
Expand Down
41 changes: 41 additions & 0 deletions internal/super/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Flow CLI
*
* Copyright Flow Foundation
*
* 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 super

import (
"path/filepath"
"testing"

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

func Test_ResolveTargetDirectory_CurrentDirCases(t *testing.T) {
wd, err := filepath.Abs(".")
assert.NoError(t, err)

// Case 1: Empty input should resolve to current directory
cur, err := resolveTargetDirectory("")
assert.NoError(t, err)
assert.Equal(t, filepath.Clean(wd), filepath.Clean(cur))

// Case 2: '.' should resolve to current directory
dot, err := resolveTargetDirectory(".")
assert.NoError(t, err)
assert.Equal(t, filepath.Clean(wd), filepath.Clean(dot))
}
Loading