MCPcopy Create free account
hub / github.com/crossjoin-io/crossjoin / LoadConfig

Method LoadConfig

api/config.go:15–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13)
14
15func (api *API) LoadConfig() error {
16 conf := &config.Config{}
17
18 switch api.configSource {
19 case "file":
20 log.Println("using config file", api.configPath)
21
22 configFileContent, err := ioutil.ReadFile(api.configPath)
23 if err != nil {
24 return fmt.Errorf("read file: %w", err)
25 }
26 absPath, err := filepath.Abs(api.configPath)
27 if err != nil {
28 return fmt.Errorf("get abs path: %w", err)
29 }
30 err = conf.Parse(configFileContent, filepath.Dir(absPath))
31 if err != nil {
32 return fmt.Errorf("parse config: %w", err)
33 }
34 case "github":
35 parsedURL, err := url.Parse(api.configPath)
36 if err != nil {
37 return fmt.Errorf("parse config path: %w", err)
38 }
39 basePath := parsedURL
40 basePath.Path = path.Dir(basePath.Path)
41 log.Println("using GitHub file", api.configPath)
42 configFileContent, err := api.fetchGitHubFile(api.configPath)
43 if err != nil {
44 return fmt.Errorf("read file from github: %w", err)
45 }
46 err = conf.Parse(configFileContent, basePath.String())
47 if err != nil {
48 return fmt.Errorf("parse config: %w", err)
49 }
50 }
51
52 hash := conf.Hash()
53 var x int
54 err := api.db.QueryRow("SELECT 1 FROM configs WHERE hash = $1", hash).Scan(&x)
55 if err != nil {
56 if err != sql.ErrNoRows {
57 return fmt.Errorf("query config: %w", err)
58 }
59
60 // Config doesn't exist.
61 _, err = api.db.Exec("INSERT INTO configs (loaded_at, hash, config) VALUES (datetime('now'), $1, $2)", hash, conf.JSON())
62 if err != nil {
63 return fmt.Errorf("store config: %w", err)
64 }
65
66 // Load the config
67 for _, workflow := range conf.Workflows {
68 err := api.StoreWorkflow(hash, workflow)
69 if err != nil {
70 return err
71 }
72 }

Callers 2

NewAPIFunction · 0.95
postConfigReloadMethod · 0.95

Calls 8

ParseMethod · 0.95
fetchGitHubFileMethod · 0.95
HashMethod · 0.95
JSONMethod · 0.95
StoreWorkflowMethod · 0.95
StoreDataConnectionMethod · 0.95
StoreDatasetMethod · 0.95
StringMethod · 0.45

Tested by

no test coverage detected