Skip to content

Commit

Permalink
remove xdg, use $HOME/.config and $HOME/.cache (#641)
Browse files Browse the repository at this point in the history
* remove xdg, use /home/keming/.config and /home/keming/.cache

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* wrap to a func

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix cache dir

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* use mkdirall to ignore exist

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix mkdirifnotexist param

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy authored Jul 21, 2022
1 parent 9ee48d8 commit 42e7531
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 28 deletions.
2 changes: 1 addition & 1 deletion e2e/testdata/custom-image-test/build.envd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def build():
base(language="python", image="python:3.8")
install.python_packages(name=[
"via",
"via",
])
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/tensorchord/envd
go 1.18

require (
github.com/adrg/xdg v0.4.0
github.com/alessio/shellescape v1.4.1
github.com/cockroachdb/errors v1.9.0
github.com/containerd/console v1.0.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
16 changes: 8 additions & 8 deletions pkg/home/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package home
import (
"encoding/gob"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"

"github.com/tensorchord/envd/pkg/util/fileutil"
)

type cacheManager interface {
Expand All @@ -32,14 +32,14 @@ type cacheManager interface {
}

func (m *generalManager) initCache() error {
// Create $XDG_CACHE_HOME/envd
_, err := xdg.CacheFile("envd/cache")
// Create $HOME/.cache/envd/
m.cacheDir = fileutil.DefaultCacheDir

cacheStatusFile, err := fileutil.CacheFile("cache.status")
if err != nil {
return errors.Wrap(err, "failed to get cache")
return errors.Wrap(err, "failed to get cache.status file path")
}
m.cacheDir = filepath.Join(xdg.CacheHome, "envd")

m.cacheStatusFile = filepath.Join(m.cacheDir, "cache.status")
m.cacheStatusFile = cacheStatusFile
_, err = os.Stat(m.cacheStatusFile)
if err != nil {
if os.IsNotExist(err) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package home

import (
"github.com/adrg/xdg"
"github.com/cockroachdb/errors"

"github.com/tensorchord/envd/pkg/util/fileutil"
Expand All @@ -26,8 +25,8 @@ type configManager interface {
}

func (m *generalManager) initConfig() error {
// Create $XDG_CONFIG_HOME/envd/config.envd
config, err := xdg.ConfigFile("envd/config.envd")
// Create $HOME/.config/envd/config.envd
config, err := fileutil.ConfigFile("config.envd")
if err != nil {
return errors.Wrap(err, "failed to get config file")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/home/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"encoding/gob"
"os"

"github.com/adrg/xdg"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"

"github.com/tensorchord/envd/pkg/types"
"github.com/tensorchord/envd/pkg/util/fileutil"
)

type contextManager interface {
Expand All @@ -36,11 +36,11 @@ type contextManager interface {
}

func (m *generalManager) initContext() error {
contextfile, err := xdg.ConfigFile("envd/contexts")
contextFile, err := fileutil.ConfigFile("contexts")
if err != nil {
return errors.Wrap(err, "failed to get context file")
}
m.contextFile = contextfile
m.contextFile = contextFile

// Create default context.

Expand Down
10 changes: 5 additions & 5 deletions pkg/home/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"os"
"path/filepath"

"github.com/adrg/xdg"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/tensorchord/envd/pkg/types"
"github.com/tensorchord/envd/pkg/util/fileutil"
)

var _ = Describe("home manager", func() {
Expand All @@ -43,16 +43,16 @@ var _ = Describe("home manager", func() {
}
Expect(defaultManager.init()).NotTo(HaveOccurred())
m := GetManager()
Expect(m.CacheDir()).To(Equal(filepath.Join(xdg.CacheHome, "envd")))
Expect(m.ConfigFile()).To(Equal(filepath.Join(xdg.ConfigHome, "envd/config.envd")))
Expect(m.ContextFile()).To(Equal(filepath.Join(xdg.ConfigHome, "envd/contexts")))
Expect(m.CacheDir()).To(Equal(filepath.Join(fileutil.DefaultCacheDir)))
Expect(m.ConfigFile()).To(Equal(filepath.Join(fileutil.DefaultConfigDir, "config.envd")))
Expect(m.ContextFile()).To(Equal(filepath.Join(fileutil.DefaultConfigDir, "contexts")))
driver, socket, err := m.ContextGetCurrent()
Expect(err).NotTo(HaveOccurred())
Expect(driver).To(Equal(types.BuilderTypeDocker))
Expect(socket).To(Equal("envd_buildkitd"))
})
It("should return the cache status", func() {
Expect(os.RemoveAll(filepath.Join(xdg.CacheHome, "envd/cache.status"))).NotTo(HaveOccurred())
Expect(os.RemoveAll(filepath.Join(fileutil.DefaultCacheDir, "cache.status"))).NotTo(HaveOccurred())
Expect(Initialize()).NotTo(HaveOccurred())
m := GetManager()
m.(*generalManager).cacheMap = make(map[string]bool)
Expand Down
3 changes: 1 addition & 2 deletions pkg/shell/zsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"os"
"path/filepath"

"github.com/adrg/xdg"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -34,7 +33,7 @@ var _ = Describe("zsh manager", Serial, func() {
AfterEach(func() {
// Cleanup the home cache.
Expect(home.Initialize()).NotTo(HaveOccurred())
Expect(os.RemoveAll(filepath.Join(xdg.CacheHome, "envd/cache.status"))).NotTo(HaveOccurred())
Expect(os.RemoveAll(filepath.Join(fileutil.DefaultCacheDir, "cache.status"))).NotTo(HaveOccurred())
})
When("cached", func() {
It("should skip", func() {
Expand Down
5 changes: 2 additions & 3 deletions pkg/ssh/config/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/pem"
"os"

"github.com/adrg/xdg"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -135,12 +134,12 @@ func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) {
}

func getDefaultKeyPaths() (string, string, error) {
public, err := xdg.ConfigFile("envd/" + config.PublicKeyFile)
public, err := fileutil.ConfigFile(config.PublicKeyFile)
if err != nil {
return "", "", errors.Wrap(err, "Cannot get public key path")
}

private, err := xdg.ConfigFile("envd/" + config.PrivateKeyFile)
private, err := fileutil.ConfigFile(config.PrivateKeyFile)
if err != nil {
return "", "", errors.Wrap(err, "Cannot get private key path")
}
Expand Down
53 changes: 53 additions & 0 deletions pkg/util/fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@
package fileutil

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

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
)

var (
DefaultConfigDir string
DefaultCacheDir string
)

func init() {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
DefaultConfigDir = path.Join(home, ".config/envd")
DefaultCacheDir = path.Join(home, ".cache/envd")
}

// FileExists returns true if the file exists
func FileExists(filename string) (bool, error) {
info, err := os.Stat(filename)
Expand Down Expand Up @@ -82,3 +99,39 @@ func RootDir() (string, error) {
func Base(dir string) string {
return filepath.Base(dir)
}

func MkdirIfNotExist(filepath string) error {
exist, err := DirExists(filepath)
if err != nil {
return err
}
if !exist {
err = os.MkdirAll(filepath, os.ModeDir|0700)
if err != nil {
return errors.Wrap(err, "failed to create the dir")
}
}
return nil
}

// ConfigFile returns the location for the specified envd config file
func ConfigFile(filename string) (string, error) {
if strings.ContainsRune(filename, os.PathSeparator) {
return "", fmt.Errorf("filename %s should not contain any path separator", filename)
}
if err := MkdirIfNotExist(DefaultConfigDir); err != nil {
return "", err
}
return path.Join(DefaultConfigDir, filename), nil
}

// CacheFile returns the location for the specified envd cache file
func CacheFile(filename string) (string, error) {
if strings.ContainsRune(filename, os.PathSeparator) {
return "", fmt.Errorf("filename %s should not contain any path separator", filename)
}
if err := MkdirIfNotExist(DefaultCacheDir); err != nil {
return "", err
}
return path.Join(DefaultCacheDir, filename), nil
}

0 comments on commit 42e7531

Please sign in to comment.