mirror of https://github.com/dapr/cli.git
find dapr.yaml in dirpath when dapr run -f <dirpath> is executed (#1166)
* find dapr.yaml in dirpath when dapr run -f <dirpath> is executed Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> * modtidy Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> * get rid of afero Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> * review comments Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> * review comments Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> * merge fixes Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> Co-authored-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
This commit is contained in:
parent
44d8dcb752
commit
9d18f86768
30
cmd/run.go
30
cmd/run.go
|
@ -62,6 +62,7 @@ var (
|
|||
)
|
||||
|
||||
const (
|
||||
defaultRunTemplateFileName = "dapr.yaml"
|
||||
runtimeWaitTimeoutInSeconds = 60
|
||||
)
|
||||
|
||||
|
@ -96,7 +97,12 @@ dapr run --app-id myapp --dapr-path /usr/local/dapr
|
|||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(runFilePath) > 0 {
|
||||
executeRunWithAppsConfigFile(runFilePath)
|
||||
runConfigFilePath, err := getRunFilePath(runFilePath)
|
||||
if err != nil {
|
||||
print.FailureStatusEvent(os.Stderr, "Failed to get run file path: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
executeRunWithAppsConfigFile(runConfigFilePath)
|
||||
return
|
||||
}
|
||||
if len(args) == 0 {
|
||||
|
@ -441,3 +447,25 @@ func executeRunWithAppsConfigFile(runFilePath string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// getRunFilePath returns the path to the run file.
|
||||
// If the provided path is a path to a YAML file then return the same.
|
||||
// Else it returns the path of "dapr.yaml" in the provided directory.
|
||||
func getRunFilePath(path string) (string, error) {
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error getting file info for %s: %w", path, err)
|
||||
}
|
||||
if fileInfo.IsDir() {
|
||||
filePath, err := utils.FindFileInDir(path, defaultRunTemplateFileName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filePath, nil
|
||||
}
|
||||
hasYAMLExtension := strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")
|
||||
if !hasYAMLExtension {
|
||||
return "", fmt.Errorf("file %q is not a YAML file", path)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
|
|
@ -156,7 +156,7 @@ func (a *RunFileConfig) resolvePathToAbsAndValidate(baseDir string, paths ...*st
|
|||
return err
|
||||
}
|
||||
*path = absPath
|
||||
if err = utils.ValidateFilePaths(*path); err != nil {
|
||||
if err = utils.ValidateFilePath(*path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -204,7 +204,7 @@ func (a *RunFileConfig) resolveResourcesFilePath(app *Apps) error {
|
|||
return nil
|
||||
}
|
||||
localResourcesDir := filepath.Join(app.AppDirPath, standalone.DefaultDaprDirName, standalone.DefaultResourcesDirName)
|
||||
if err := utils.ValidateFilePaths(localResourcesDir); err == nil {
|
||||
if err := utils.ValidateFilePath(localResourcesDir); err == nil {
|
||||
app.ResourcesPath = localResourcesDir
|
||||
} else if len(strings.TrimSpace(a.Common.ResourcesPath)) > 0 {
|
||||
app.ResourcesPath = a.Common.ResourcesPath
|
||||
|
@ -228,7 +228,7 @@ func (a *RunFileConfig) resolveConfigFilePath(app *Apps) error {
|
|||
return nil
|
||||
}
|
||||
localConfigFile := filepath.Join(app.AppDirPath, standalone.DefaultDaprDirName, standalone.DefaultConfigFileName)
|
||||
if err := utils.ValidateFilePaths(localConfigFile); err == nil {
|
||||
if err := utils.ValidateFilePath(localConfigFile); err == nil {
|
||||
app.ConfigFile = localConfigFile
|
||||
} else if len(strings.TrimSpace(a.Common.ConfigFile)) > 0 {
|
||||
app.ConfigFile = a.Common.ConfigFile
|
||||
|
|
|
@ -324,7 +324,7 @@ func GetVersionAndImageVariant(imageTag string) (string, string) {
|
|||
}
|
||||
|
||||
// Returns true if the given file path is valid.
|
||||
func ValidateFilePaths(filePath string) error {
|
||||
func ValidateFilePath(filePath string) error {
|
||||
if filePath != "" {
|
||||
if _, err := os.Stat(filePath); err != nil {
|
||||
return fmt.Errorf("error in getting the file info for %s: %w", filePath, err)
|
||||
|
@ -352,3 +352,12 @@ func ReadFile(filePath string) ([]byte, error) {
|
|||
}
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
// FindFileInDir finds and returns the path of the given file name in the given directory.
|
||||
func FindFileInDir(dirPath, fileName string) (string, error) {
|
||||
filePath := filepath.Join(dirPath, fileName)
|
||||
if err := ValidateFilePath(filePath); err != nil {
|
||||
return "", fmt.Errorf("error in validating the file path %q: %w", filePath, err)
|
||||
}
|
||||
return filePath, nil
|
||||
}
|
||||
|
|
|
@ -194,7 +194,7 @@ func TestValidateFilePaths(t *testing.T) {
|
|||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actual := ValidateFilePaths(tc.input)
|
||||
actual := ValidateFilePath(tc.input)
|
||||
assert.Equal(t, tc.expectedErr, actual != nil)
|
||||
})
|
||||
}
|
||||
|
@ -267,6 +267,68 @@ func TestReadFile(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Following is the directory and file structure created for this test in the os's default temp directory:
|
||||
// test_find_file_in_dir/valid_dir/dapr.yaml.
|
||||
// test_find_file_in_dir/valid_dir/test1.yaml.
|
||||
// test_find_file_in_dir/valid_dir_no_dapr_yaml.
|
||||
func TestFindFileInDir(t *testing.T) {
|
||||
nonExistentDirName := "invalid_dir"
|
||||
validDirNameWithDaprYAMLFile := "valid_dir"
|
||||
validDirWithNoDaprYAML := "valid_dir_no_dapr_yaml"
|
||||
|
||||
dirName := createTempDir(t, "test_find_file_in_dir")
|
||||
t.Cleanup(func() {
|
||||
cleanupTempDir(t, dirName)
|
||||
})
|
||||
|
||||
err := os.Mkdir(filepath.Join(dirName, validDirNameWithDaprYAMLFile), 0o755)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.Mkdir(filepath.Join(dirName, validDirWithNoDaprYAML), 0o755)
|
||||
assert.NoError(t, err)
|
||||
|
||||
fl, err := os.Create(filepath.Join(dirName, validDirNameWithDaprYAMLFile, "dapr.yaml"))
|
||||
assert.NoError(t, err)
|
||||
fl.Close()
|
||||
|
||||
fl, err = os.Create(filepath.Join(dirName, validDirNameWithDaprYAMLFile, "test1.yaml"))
|
||||
assert.NoError(t, err)
|
||||
fl.Close()
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
input string
|
||||
expectedErr bool
|
||||
expectedFilePath string
|
||||
}{
|
||||
{
|
||||
name: "valid directory path with dapr.yaml file",
|
||||
input: filepath.Join(dirName, validDirNameWithDaprYAMLFile),
|
||||
expectedErr: false,
|
||||
expectedFilePath: filepath.Join(dirName, validDirNameWithDaprYAMLFile, "dapr.yaml"),
|
||||
},
|
||||
{
|
||||
name: "valid directory path with no dapr.yaml file",
|
||||
input: filepath.Join(dirName, validDirWithNoDaprYAML),
|
||||
expectedErr: true,
|
||||
expectedFilePath: "",
|
||||
},
|
||||
{
|
||||
name: "non existent dir",
|
||||
input: nonExistentDirName,
|
||||
expectedErr: true,
|
||||
expectedFilePath: "",
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
filePath, err := FindFileInDir(tc.input, "dapr.yaml")
|
||||
assert.Equal(t, tc.expectedErr, err != nil)
|
||||
assert.Equal(t, tc.expectedFilePath, filePath)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func createTempDir(t *testing.T, tempDirName string) string {
|
||||
dirName, err := os.MkdirTemp("", tempDirName)
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Reference in New Issue