2024-12-05 14:40:34 +00:00
package apps
import (
"context"
"fmt"
2024-12-10 16:11:41 +00:00
"path"
2024-12-23 15:12:00 +00:00
"strings"
2024-12-05 14:40:34 +00:00
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
)
2024-12-16 12:42:12 +00:00
type validate struct { }
2024-12-05 14:40:34 +00:00
func ( v * validate ) Apply ( ctx context . Context , b * bundle . Bundle ) diag . Diagnostics {
var diags diag . Diagnostics
2024-12-10 16:11:41 +00:00
possibleConfigFiles := [ ] string { "app.yml" , "app.yaml" }
2024-12-19 13:00:06 +00:00
usedSourceCodePaths := make ( map [ string ] string )
for key , app := range b . Config . Resources . Apps {
if _ , ok := usedSourceCodePaths [ app . SourceCodePath ] ; ok {
diags = append ( diags , diag . Diagnostic {
Severity : diag . Error ,
Summary : "Duplicate app source code path" ,
2024-12-23 15:12:00 +00:00
Detail : fmt . Sprintf ( "app resource '%s' has the same source code path as app resource '%s', this will lead to the app configuration being overriden by each other" , key , usedSourceCodePaths [ app . SourceCodePath ] ) ,
2024-12-19 13:00:06 +00:00
Locations : b . Config . GetLocations ( fmt . Sprintf ( "resources.apps.%s.source_code_path" , key ) ) ,
} )
}
usedSourceCodePaths [ app . SourceCodePath ] = key
2024-12-10 16:11:41 +00:00
2024-12-05 14:40:34 +00:00
for _ , configFile := range possibleConfigFiles {
2024-12-30 11:44:09 +00:00
appPath := strings . TrimPrefix ( app . SourceCodePath , b . Config . Workspace . FilePath )
cf := path . Join ( appPath , configFile )
2024-12-10 16:11:41 +00:00
if _ , err := b . SyncRoot . Stat ( cf ) ; err == nil {
2024-12-05 14:40:34 +00:00
diags = append ( diags , diag . Diagnostic {
Severity : diag . Error ,
2024-12-10 16:11:41 +00:00
Summary : fmt . Sprintf ( "%s detected" , configFile ) ,
2024-12-05 14:40:34 +00:00
Detail : fmt . Sprintf ( "remove %s and use 'config' property for app resource '%s' instead" , cf , app . Name ) ,
} )
}
}
}
return diags
}
func ( v * validate ) Name ( ) string {
return "apps.Validate"
}
func Validate ( ) bundle . Mutator {
return & validate { }
}