Skip to content

Commit

Permalink
feat: Support custom storage path#5
Browse files Browse the repository at this point in the history
The default cache path is under the home directory ($HOME/.version-fox/cache). Installing numerous SDKs can consume significant space in this directory. Allowing customization of the storage path can help manage this issue.

close #5
---------

Co-authored-by: Xuzhe Cheung <[email protected]>
  • Loading branch information
aooohan and ShizheChang committed Jan 29, 2024
1 parent 6bc8164 commit 627330f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
9 changes: 7 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
)

type Config struct {
Proxy *Proxy `yaml:"proxy"`
Proxy *Proxy `yaml:"proxy"`
Storage *Storage `yaml:"storage"`
}

const filename = "config.yaml"

var (
defaultConfig = &Config{
Proxy: EmptyProxy,
Proxy: EmptyProxy,
Storage: EmptyStorage,
}
)

Expand All @@ -55,6 +57,9 @@ func NewConfigWithPath(p string) (*Config, error) {
if config.Proxy == nil {
config.Proxy = EmptyProxy
}
if config.Storage == nil {
config.Storage = EmptyStorage
}
return config, nil

}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ proxy:
url: http://test

storage:
path: /tmp
sdkPath: /tmp
33 changes: 33 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config_test

import (
"github.com/version-fox/vfox/internal/config"
"os"
"testing"
)

Expand Down Expand Up @@ -52,3 +53,35 @@ func TestConfigWithEmptyProxy(t *testing.T) {
t.Fatal("proxy enable must be false")
}
}

func TestConfigWithStorage(t *testing.T) {
c, err := config.NewConfig("")
if err != nil {
t.Fatal(err)
}
if c.Storage.SdkPath != "/tmp" {
t.Fatal("storage sdk path is invalid")
}
}

func TestStorageWithWritePermission(t *testing.T) {
dir, err := os.UserHomeDir()
if err != nil {
t.Fatal(err)
}
s := &config.Storage{
SdkPath: dir,
}
if err = s.Validate(); err != nil {
t.Fatal(err)
}
}
func TestConfigWithEmptyStorage(t *testing.T) {
c, err := config.NewConfigWithPath("empty_test.yaml")
if err != nil {
t.Fatal(err)
}
if c.Storage.SdkPath != "" {
t.Fatal("proxy url must be empty")
}
}
52 changes: 52 additions & 0 deletions internal/config/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"fmt"
"os"
"path/filepath"
)

type Storage struct {
SdkPath string `yaml:"sdkPath"`
}

var EmptyStorage = &Storage{
SdkPath: "",
}

func (s *Storage) Validate() error {
if s.SdkPath == "" {
return nil
}
stat, err := os.Stat(s.SdkPath)
if err != nil {
return err
}
if !stat.IsDir() {
return fmt.Errorf("%s is not a directory", s.SdkPath)
}
tmpfn := filepath.Join(s.SdkPath, ".tmpfile")
f, err := os.OpenFile(tmpfn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return err
}
defer os.Remove(tmpfn)
defer f.Close()
return nil
}
9 changes: 9 additions & 0 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ func newSdkManager(record env.Record, meta *PathMeta) *Manager {
if err != nil {
panic(fmt.Errorf("init Config error: %w", err))
}

// custom sdk path first
if len(c.Storage.SdkPath) > 0 {
err = c.Storage.Validate()
if err != nil {
panic(fmt.Errorf("validate storage error: %w", err))
}
meta.SdkCachePath = c.Storage.SdkPath
}
manager := &Manager{
PathMeta: meta,
EnvManager: envManger,
Expand Down

0 comments on commit 627330f

Please sign in to comment.