Skip to content

Commit

Permalink
feat(CLI): Replace .midi with XDG base dir (#129)
Browse files Browse the repository at this point in the history
* feat(CLI): Replace .midi with XDG base dir

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Fix test cases

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Remove unused var

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege authored May 10, 2022
1 parent 008dd5e commit 20054a7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 71 deletions.
17 changes: 1 addition & 16 deletions cmd/midi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ func main() {
Name: "debug",
Usage: "enable debug output in logs",
},
&cli.PathFlag{
Name: flag.FlagConfig,
Usage: "path to config file",
Value: "~/.midi/config.MIDI",
},
&cli.PathFlag{
Name: flag.FlagHomeDir,
Usage: "path to midi home",
Value: "~/.midi",
},
&cli.StringFlag{
Name: flag.FlagBuildkitdImage,
Usage: "docker image to use for buildkitd",
Expand Down Expand Up @@ -94,12 +84,7 @@ func main() {
logrus.SetLevel(logrus.DebugLevel)
}

// Get the config file.
configFile := context.Path(flag.FlagConfig)

homeDir := context.Path(flag.FlagHomeDir)

if err := home.Initialize(homeDir, configFile); err != nil {
if err := home.Initialize(); err != nil {
return errors.Wrap(err, "failed to initialize home manager")
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ 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
2 changes: 1 addition & 1 deletion pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var _ = Describe("Builder", func() {
b.Interpreter.(*mockstarlark.MockInterpreter).EXPECT().ExecFile(
gomock.Eq(b.manifestFilePath),
).Return(nil, nil).Times(1)
err := home.Initialize("/tmp/midi", configFilePath)
err := home.Initialize()
Expect(err).ToNot(HaveOccurred())
close(b.Writer.Status())

Expand Down
2 changes: 0 additions & 2 deletions pkg/flag/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package flag
const (
FlagCacheDir = "cache-dir"
FlagContextDir = "context-dir"
FlagConfig = "config"
FlagBuildkitdImage = "buildkitd-image"
FlagBuildkitdContainer = "buildkitd-container-name"
FlagHomeDir = "home-dir"
FlagSSHImage = "midi-ssh-image"
)
68 changes: 18 additions & 50 deletions pkg/home/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@ package home

import (
"os"
"os/user"
"path/filepath"
"strings"
"sync"

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

const (
cacheDirName = "cache"
)

type Manager interface {
HomeDir() string
CacheDir() string
ConfigFile() string
}

type generalManager struct {
homeDir string
cacheDir string
configFile string

Expand All @@ -48,11 +41,11 @@ var (
once sync.Once
)

func Initialize(homeDir, configFile string) error {
func Initialize() error {
once.Do(func() {
defaultManager = &generalManager{}
})
if err := defaultManager.init(homeDir, configFile); err != nil {
if err := defaultManager.init(); err != nil {
return err
}
return nil
Expand All @@ -70,63 +63,38 @@ func (m generalManager) ConfigFile() string {
return m.configFile
}

func (m generalManager) HomeDir() string {
return m.homeDir
}

func (m *generalManager) init(homeDir, configFile string) error {
expandedDir, err := expandHome(homeDir)
if err != nil {
return errors.Wrap(err, "failed to expand home dir")
}
if err := os.MkdirAll(expandedDir, 0755); err != nil {
return errors.Wrap(err, "failed to create MIDI home directory")
}
m.homeDir = expandedDir

cacheDir := filepath.Join(expandedDir, cacheDirName)
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return errors.Wrap(err, "failed to create MIDI cache directory")
}
m.cacheDir = cacheDir

expandedFilePath, err := expandHome(configFile)
func (m *generalManager) init() error {
// Create $XDG_CONFIG_HOME/midi/config.MIDI
config, err := xdg.ConfigFile("midi/config.MIDI")
if err != nil {
return errors.Wrap(err, "failed to expand config file path")
return errors.Wrap(err, "failed to get config file")
}

_, err = os.Stat(expandedFilePath)
_, err = os.Stat(config)
if err != nil {
if os.IsNotExist(err) {
logrus.WithField("config", expandedFilePath).Info("Creating config file")
if _, err := os.Create(expandedFilePath); err != nil {
logrus.WithField("config", config).Info("Creating config file")
if _, err := os.Create(config); err != nil {
return errors.Wrap(err, "failed to create config file")
}
} else {
return errors.Wrap(err, "failed to stat config file")
}
}
m.configFile = expandedFilePath
m.configFile = config

// Create $XDG_CACHE_HOME/midi
_, err = xdg.CacheFile("midi/cache")
if err != nil {
return errors.Wrap(err, "failed to get cache")
}
m.cacheDir = filepath.Join(xdg.CacheHome, "midi")

m.logger = logrus.WithFields(logrus.Fields{
"homeDir": m.homeDir,
"cacheDir": m.cacheDir,
"config": m.configFile,
})

m.logger.Debug("home manager initialized")
return nil
}

func expandHome(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
usr, _ := user.Current()
dir := usr.HomeDir
path = filepath.Join(dir, path[2:])
}
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
return absPath, nil
}
14 changes: 12 additions & 2 deletions pkg/shell/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"path/filepath"

"github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
"github.com/tensorchord/MIDI/pkg/home"
"github.com/tensorchord/MIDI/pkg/util/fileutil"
)
Expand Down Expand Up @@ -47,15 +48,24 @@ func (m generalManager) DownloadOrCache() error {
if ok, err := fileutil.DirExists(m.OHMyZSHDir()); err != nil {
return err
} else if ok {
logrus.WithFields(logrus.Fields{
"cache-dir": m.OHMyZSHDir(),
}).Debug("found cached oh-my-zsh")
return nil
}
url := "https://github.com/ohmyzsh/ohmyzsh"
l := logrus.WithFields(logrus.Fields{
"cache-dir": m.OHMyZSHDir(),
"URL": url,
})
l.Debug("downloading oh-my-zsh")
_, err := git.PlainClone(m.OHMyZSHDir(), false, &git.CloneOptions{
URL: "https://github.com/ohmyzsh/ohmyzsh",
URL: url,
})
if err != nil {
return err
}

l.Debug("oh-my-zsh is downloaded")
return nil
}

Expand Down

0 comments on commit 20054a7

Please sign in to comment.