-
Notifications
You must be signed in to change notification settings - Fork 117
/
add.go
101 lines (85 loc) · 2.89 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package source
import (
"fmt"
"os"
"path/filepath"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/local"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/compilers/rillv1beta"
"github.com/rilldata/rill/runtime/pkg/fileutil"
"github.com/rilldata/rill/runtime/services/catalog/artifacts"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
)
// addCmd represents the add command, it requires min 1 args as source name
func AddCmd(cfg *config.Config) *cobra.Command {
var olapDriver string
var olapDSN string
var projectPath string
var sourceName string
var force bool
var verbose bool
addCmd := &cobra.Command{
Use: "add <file>",
Short: "Add a local file source",
Long: "Add a local file source. Supported file types include .parquet, .csv, .tsv.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dataPath := args[0]
if !filepath.IsAbs(dataPath) {
relPath, err := filepath.Rel(projectPath, dataPath)
if err != nil {
return err
}
dataPath = relPath
}
app, err := local.NewApp(cmd.Context(), cfg.Version, verbose, olapDriver, olapDSN, projectPath, local.LogFormatConsole, nil)
if err != nil {
return err
}
defer app.Close()
if !app.IsProjectInit() {
return fmt.Errorf("not a valid Rill project")
}
if sourceName == "" {
sourceName = fileutil.Stem(dataPath)
}
props := map[string]any{"path": dataPath}
propsPB, err := structpb.NewStruct(props)
if err != nil {
return fmt.Errorf("can't serialize artifact: %w", err)
}
src := &runtimev1.Source{
Name: artifacts.SanitizedName(sourceName),
Connector: "local_file",
Properties: propsPB,
}
repo, err := app.Runtime.Repo(cmd.Context(), app.Instance.ID)
if err != nil {
panic(err) // Should never happen
}
c := rillv1beta.New(repo, app.Instance.ID)
sourcePath, err := c.PutSource(cmd.Context(), repo, app.Instance.ID, src, force)
if err != nil {
if os.IsExist(err) {
return fmt.Errorf("source already exists (pass --force to overwrite)")
}
return fmt.Errorf("write source: %w", err)
}
err = app.ReconcileSource(sourcePath)
if err != nil {
return fmt.Errorf("reconcile source: %w", err)
}
return nil
},
}
addCmd.Flags().SortFlags = false
addCmd.Flags().StringVar(&sourceName, "name", "", "Source name (defaults to file name)")
addCmd.Flags().BoolVarP(&force, "force", "f", false, "Overwrite the source if it already exists")
addCmd.Flags().StringVar(&projectPath, "path", ".", "Project directory")
addCmd.Flags().StringVar(&olapDSN, "db", local.DefaultOLAPDSN, "Database DSN")
addCmd.Flags().StringVar(&olapDriver, "db-driver", local.DefaultOLAPDriver, "Database driver")
addCmd.Flags().BoolVar(&verbose, "verbose", false, "Sets the log level to debug")
return addCmd
}