|
| 1 | +package generate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/fs" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/databricks/cli/bundle/config/generate" |
| 12 | + "github.com/databricks/cli/cmd/root" |
| 13 | + "github.com/databricks/cli/libs/cmdio" |
| 14 | + "github.com/databricks/cli/libs/dyn" |
| 15 | + "github.com/databricks/cli/libs/dyn/yamlsaver" |
| 16 | + "github.com/databricks/cli/libs/filer" |
| 17 | + "github.com/databricks/cli/libs/textutil" |
| 18 | + "github.com/databricks/databricks-sdk-go" |
| 19 | + "github.com/databricks/databricks-sdk-go/service/apps" |
| 20 | + "github.com/spf13/cobra" |
| 21 | + |
| 22 | + "gopkg.in/yaml.v3" |
| 23 | +) |
| 24 | + |
| 25 | +func NewGenerateAppCommand() *cobra.Command { |
| 26 | + var configDir string |
| 27 | + var sourceDir string |
| 28 | + var appName string |
| 29 | + var force bool |
| 30 | + |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "app", |
| 33 | + Short: "Generate bundle configuration for a Databricks app", |
| 34 | + } |
| 35 | + |
| 36 | + cmd.Flags().StringVar(&appName, "existing-app-name", "", `App name to generate config for`) |
| 37 | + cmd.MarkFlagRequired("existing-app-name") |
| 38 | + |
| 39 | + cmd.Flags().StringVarP(&configDir, "config-dir", "d", filepath.Join("resources"), `Directory path where the output bundle config will be stored`) |
| 40 | + cmd.Flags().StringVarP(&sourceDir, "source-dir", "s", filepath.Join("src", "app"), `Directory path where the app files will be stored`) |
| 41 | + cmd.Flags().BoolVarP(&force, "force", "f", false, `Force overwrite existing files in the output directory`) |
| 42 | + |
| 43 | + cmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 44 | + ctx := cmd.Context() |
| 45 | + b, diags := root.MustConfigureBundle(cmd) |
| 46 | + if err := diags.Error(); err != nil { |
| 47 | + return diags.Error() |
| 48 | + } |
| 49 | + |
| 50 | + w := b.WorkspaceClient() |
| 51 | + cmdio.LogString(ctx, fmt.Sprintf("Loading app '%s' configuration", appName)) |
| 52 | + app, err := w.Apps.Get(ctx, apps.GetAppRequest{Name: appName}) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + // Making sure the config directory and source directory are absolute paths. |
| 58 | + if !filepath.IsAbs(configDir) { |
| 59 | + configDir = filepath.Join(b.BundleRootPath, configDir) |
| 60 | + } |
| 61 | + |
| 62 | + if !filepath.IsAbs(sourceDir) { |
| 63 | + sourceDir = filepath.Join(b.BundleRootPath, sourceDir) |
| 64 | + } |
| 65 | + |
| 66 | + downloader := newDownloader(w, sourceDir, configDir) |
| 67 | + |
| 68 | + sourceCodePath := app.DefaultSourceCodePath |
| 69 | + err = downloader.markDirectoryForDownload(ctx, &sourceCodePath) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + appConfig, err := getAppConfig(ctx, app, w) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to get app config: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Making sure the source code path is relative to the config directory. |
| 80 | + rel, err := filepath.Rel(configDir, sourceDir) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + v, err := generate.ConvertAppToValue(app, filepath.ToSlash(rel), appConfig) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + appKey := cmd.Flag("key").Value.String() |
| 91 | + if appKey == "" { |
| 92 | + appKey = textutil.NormalizeString(app.Name) |
| 93 | + } |
| 94 | + |
| 95 | + result := map[string]dyn.Value{ |
| 96 | + "resources": dyn.V(map[string]dyn.Value{ |
| 97 | + "apps": dyn.V(map[string]dyn.Value{ |
| 98 | + appKey: v, |
| 99 | + }), |
| 100 | + }), |
| 101 | + } |
| 102 | + |
| 103 | + // If there are app.yaml or app.yml files in the source code path, they will be downloaded but we don't want to include them in the bundle. |
| 104 | + // We include this configuration inline, so we need to remove these files. |
| 105 | + for _, configFile := range []string{"app.yml", "app.yaml"} { |
| 106 | + delete(downloader.files, filepath.Join(sourceDir, configFile)) |
| 107 | + } |
| 108 | + |
| 109 | + err = downloader.FlushToDisk(ctx, force) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + filename := filepath.Join(configDir, fmt.Sprintf("%s.app.yml", appKey)) |
| 115 | + |
| 116 | + saver := yamlsaver.NewSaver() |
| 117 | + err = saver.SaveAsYAML(result, filename, force) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + cmdio.LogString(ctx, fmt.Sprintf("App configuration successfully saved to %s", filename)) |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + return cmd |
| 127 | +} |
| 128 | + |
| 129 | +func getAppConfig(ctx context.Context, app *apps.App, w *databricks.WorkspaceClient) (map[string]any, error) { |
| 130 | + sourceCodePath := app.DefaultSourceCodePath |
| 131 | + |
| 132 | + f, err := filer.NewWorkspaceFilesClient(w, sourceCodePath) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + // The app config is stored in app.yml or app.yaml file in the source code path. |
| 138 | + configFileNames := []string{"app.yml", "app.yaml"} |
| 139 | + for _, configFile := range configFileNames { |
| 140 | + r, err := f.Read(ctx, configFile) |
| 141 | + if err != nil { |
| 142 | + if errors.Is(err, fs.ErrNotExist) { |
| 143 | + continue |
| 144 | + } |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + defer r.Close() |
| 148 | + |
| 149 | + cmdio.LogString(ctx, fmt.Sprintf("Reading app configuration from %s", configFile)) |
| 150 | + content, err := io.ReadAll(r) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + var appConfig map[string]any |
| 156 | + err = yaml.Unmarshal(content, &appConfig) |
| 157 | + if err != nil { |
| 158 | + cmdio.LogString(ctx, fmt.Sprintf("Failed to parse app configuration:\n%s\nerr: %v", string(content), err)) |
| 159 | + return nil, nil |
| 160 | + } |
| 161 | + |
| 162 | + return appConfig, nil |
| 163 | + } |
| 164 | + |
| 165 | + return nil, nil |
| 166 | +} |
0 commit comments