code.oscarkilo.com/klex-git

Hash:
1eb2ff6179d5c5f5b685fe544350e815c26eb0b0
Author:
Igor Naverniouk <[email protected]>
Date:
Mon Sep 23 13:11:26 2024 -0700
Message:
careful about spaces
diff --git a/config/config.go b/config/config.go
index 1d4e411..16e79b7 100644
--- a/config/config.go
+++ b/config/config.go
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path"
+ "strings"

"oscarkilo.com/klex-git/util"
)
@@ -45,7 +46,7 @@ func ReadConfig() (*Config, error) {
if err != nil {
return nil, err
}
- config.ApiKey = string(buf)
+ config.ApiKey = strings.TrimSpace(string(buf))
}
if os.Getenv("KLEX_API_KEY") != "" {
if config.ApiKey != "" {
a/config/config.go
b/config/config.go
1
// config deals with the klex.json file in the root of the client Git repo.
1
// config deals with the klex.json file in the root of the client Git repo.
2
package config
2
package config
3
3
4
import (
4
import (
5
"fmt"
5
"fmt"
6
"io/ioutil"
6
"io/ioutil"
7
"os"
7
"os"
8
"path"
8
"path"
9
"strings"
9
10
10
"oscarkilo.com/klex-git/util"
11
"oscarkilo.com/klex-git/util"
11
)
12
)
12
13
13
type Config struct {
14
type Config struct {
14
ProjectName string `json:"project_name"`
15
ProjectName string `json:"project_name"`
15
OwnerUsername string `json:"owner_username"`
16
OwnerUsername string `json:"owner_username"`
16
ReaderUsername string `json:"reader_username"`
17
ReaderUsername string `json:"reader_username"`
17
DatasetsDir string `json:"datasets_dir"`
18
DatasetsDir string `json:"datasets_dir"`
18
FunctionsDir string `json:"functions_dir"`
19
FunctionsDir string `json:"functions_dir"`
19
PipelinesDir string `json:"pipelines_dir"`
20
PipelinesDir string `json:"pipelines_dir"`
20
KlexUrl string `json:"klex_url"`
21
KlexUrl string `json:"klex_url"`
21
ApiKeyFile string `json:"api_key_file"`
22
ApiKeyFile string `json:"api_key_file"`
22
23
23
// GitRoot is the parent directory of klex.json.
24
// GitRoot is the parent directory of klex.json.
24
GitRoot string `json:"-"`
25
GitRoot string `json:"-"`
25
26
26
// ApiKey is read from the location specified above.
27
// ApiKey is read from the location specified above.
27
ApiKey string `json:"-"`
28
ApiKey string `json:"-"`
28
}
29
}
29
30
30
// ReadConfig reads the klex.json file from this Git repo's root.
31
// ReadConfig reads the klex.json file from this Git repo's root.
31
func ReadConfig() (*Config, error) {
32
func ReadConfig() (*Config, error) {
32
root, err := util.FindRoot()
33
root, err := util.FindRoot()
33
if err != nil {
34
if err != nil {
34
return nil, err
35
return nil, err
35
}
36
}
36
file := path.Join(root, "klex.json")
37
file := path.Join(root, "klex.json")
37
var config Config
38
var config Config
38
err = util.ReadJsonFile(file, &config)
39
err = util.ReadJsonFile(file, &config)
39
40
40
config.GitRoot = root
41
config.GitRoot = root
41
42
42
if config.ApiKeyFile != "" {
43
if config.ApiKeyFile != "" {
43
apiFile := path.Join(root, config.ApiKeyFile)
44
apiFile := path.Join(root, config.ApiKeyFile)
44
buf, err := ioutil.ReadFile(apiFile)
45
buf, err := ioutil.ReadFile(apiFile)
45
if err != nil {
46
if err != nil {
46
return nil, err
47
return nil, err
47
}
48
}
48
config.ApiKey = string(buf)
49
config.ApiKey = strings.TrimSpace(string(buf))
49
}
50
}
50
if os.Getenv("KLEX_API_KEY") != "" {
51
if os.Getenv("KLEX_API_KEY") != "" {
51
if config.ApiKey != "" {
52
if config.ApiKey != "" {
52
fmt.Printf("KLEX_API_KEY env var overrides api_key_file in klex.json\n")
53
fmt.Printf("KLEX_API_KEY env var overrides api_key_file in klex.json\n")
53
}
54
}
54
config.ApiKey = os.Getenv("KLEX_API_KEY")
55
config.ApiKey = os.Getenv("KLEX_API_KEY")
55
}
56
}
56
if config.ApiKey == "" {
57
if config.ApiKey == "" {
57
return nil, fmt.Errorf("Failed to read api_key_file='%s' and KLEX_API_KEY env var", config.ApiKeyFile)
58
return nil, fmt.Errorf("Failed to read api_key_file='%s' and KLEX_API_KEY env var", config.ApiKeyFile)
58
}
59
}
59
60
60
return &config, err
61
return &config, err
61
}
62
}