Skip to content

Commit 796807b

Browse files
author
Kai Kummerer
committed
cache: call Init func directly and return err
1 parent f5977f9 commit 796807b

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

internal/cmd/ske/kubeconfig/login/login.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
5757
RunE: func(cmd *cobra.Command, args []string) error {
5858
ctx := context.Background()
5959

60+
if err := cache.Init(); err != nil {
61+
return fmt.Errorf("cache init failed: %s", err)
62+
}
63+
6064
env := os.Getenv("KUBERNETES_EXEC_INFO")
6165
if env == "" {
6266
return fmt.Errorf("%s\n%s\n%s", "KUBERNETES_EXEC_INFO env var is unset or empty.",

internal/pkg/cache/cache.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ var (
1515
ErrorInvalidCacheIdentifier = fmt.Errorf("invalid cache identifier")
1616
)
1717

18-
func init() {
18+
func Init() error {
1919
cacheDir, err := os.UserCacheDir()
2020
if err != nil {
21-
panic(fmt.Errorf("get user cache dir: %w", err))
21+
return fmt.Errorf("get user cache dir: %w", err)
2222
}
2323
cacheFolderPath = filepath.Join(cacheDir, "stackit")
24+
return nil
2425
}
2526

2627
func GetObject(identifier string) ([]byte, error) {
28+
if err := validateCacheFolderPath(); err != nil {
29+
return nil, err
30+
}
2731
if !identifierRegex.MatchString(identifier) {
2832
return nil, ErrorInvalidCacheIdentifier
2933
}
@@ -32,6 +36,9 @@ func GetObject(identifier string) ([]byte, error) {
3236
}
3337

3438
func PutObject(identifier string, data []byte) error {
39+
if err := validateCacheFolderPath(); err != nil {
40+
return err
41+
}
3542
if !identifierRegex.MatchString(identifier) {
3643
return ErrorInvalidCacheIdentifier
3744
}
@@ -45,6 +52,9 @@ func PutObject(identifier string, data []byte) error {
4552
}
4653

4754
func DeleteObject(identifier string) error {
55+
if err := validateCacheFolderPath(); err != nil {
56+
return err
57+
}
4858
if !identifierRegex.MatchString(identifier) {
4959
return ErrorInvalidCacheIdentifier
5060
}
@@ -67,3 +77,10 @@ func createFolderIfNotExists(folderPath string) error {
6777
}
6878
return nil
6979
}
80+
81+
func validateCacheFolderPath() error {
82+
if cacheFolderPath == "" {
83+
return errors.New("cacheFolderPath not set. Forgot to call Init()?")
84+
}
85+
return nil
86+
}

internal/pkg/cache/cache_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
)
1111

1212
func TestGetObject(t *testing.T) {
13+
if err := Init(); err != nil {
14+
t.Fatalf("cache init failed: %s", err)
15+
}
16+
1317
tests := []struct {
1418
description string
1519
identifier string
@@ -71,7 +75,10 @@ func TestGetObject(t *testing.T) {
7175
}
7276
}
7377
func TestPutObject(t *testing.T) {
74-
// Also test createFolderIfNotExists here
78+
if err := Init(); err != nil {
79+
t.Fatalf("cache init failed: %s", err)
80+
}
81+
7582
tests := []struct {
7683
description string
7784
identifier string
@@ -142,6 +149,10 @@ func TestPutObject(t *testing.T) {
142149
}
143150

144151
func TestDeleteObject(t *testing.T) {
152+
if err := Init(); err != nil {
153+
t.Fatalf("cache init failed: %s", err)
154+
}
155+
145156
tests := []struct {
146157
description string
147158
identifier string

0 commit comments

Comments
 (0)