Skip to content

Commit 1ad0d69

Browse files
authored
Resolve directories passed to flow init (#2055)
1 parent b3f1a5d commit 1ad0d69

2 files changed

Lines changed: 64 additions & 11 deletions

File tree

internal/super/init.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,36 @@ func validateCurrentDirectoryForInit() error {
133133
}
134134

135135
// resolveTargetDirectory determines the target directory for the Flow project
136-
// based on user input. Empty input means current directory.
136+
// based on user input by resolving a candidate path and checking if it equals
137+
// the current working directory.
137138
func resolveTargetDirectory(userInput string) (string, error) {
138-
if strings.TrimSpace(userInput) == "" {
139-
// Validate current directory for Flow project conflicts
139+
pwd, err := os.Getwd()
140+
if err != nil {
141+
return "", fmt.Errorf("failed to get current working directory: %w", err)
142+
}
143+
144+
trimmed := strings.TrimSpace(userInput)
145+
146+
// Build a candidate absolute path for comparison
147+
var candidate string
148+
if trimmed == "" {
149+
candidate = pwd
150+
} else if filepath.IsAbs(trimmed) {
151+
candidate = filepath.Clean(trimmed)
152+
} else {
153+
candidate = filepath.Clean(filepath.Join(pwd, trimmed))
154+
}
155+
156+
// If candidate resolves to current directory, validate and use it
157+
if candidate == filepath.Clean(pwd) {
140158
if err := validateCurrentDirectoryForInit(); err != nil {
141159
return "", err
142160
}
143-
144-
// Use current directory
145-
pwd, err := os.Getwd()
146-
if err != nil {
147-
return "", fmt.Errorf("failed to get current working directory: %w", err)
148-
}
149161
return pwd, nil
150162
}
151163

152-
// Use provided name to create new directory
153-
return getTargetDirectory(userInput)
164+
// Otherwise, use provided name/path to create or validate new directory
165+
return getTargetDirectory(trimmed)
154166
}
155167

156168
func updateGitignore(targetDir string, readerWriter flowkit.ReaderWriter) error {

internal/super/init_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Flow CLI
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package super
20+
21+
import (
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func Test_ResolveTargetDirectory_CurrentDirCases(t *testing.T) {
29+
wd, err := filepath.Abs(".")
30+
assert.NoError(t, err)
31+
32+
// Case 1: Empty input should resolve to current directory
33+
cur, err := resolveTargetDirectory("")
34+
assert.NoError(t, err)
35+
assert.Equal(t, filepath.Clean(wd), filepath.Clean(cur))
36+
37+
// Case 2: '.' should resolve to current directory
38+
dot, err := resolveTargetDirectory(".")
39+
assert.NoError(t, err)
40+
assert.Equal(t, filepath.Clean(wd), filepath.Clean(dot))
41+
}

0 commit comments

Comments
 (0)