~hristoast/wem

b211f32ebc0483209861f90c2c92863fee9aeea7 — Hristos N. Triantafillou 11 months ago ba2802b
Don't save envs under a new file when the name is changed (#37)
8 files changed, 34 insertions(+), 27 deletions(-)

M cfg/main.go
M cli/main.go
M cmd/cfg.go
M cmd/cfg_test.go
M cmd/init.go
M cmd/init_test.go
M env/main.go
M env/main_test.go
M cfg/main.go => cfg/main.go +1 -1
@@ 12,7 12,7 @@ import (
)

const WemName = "wem"
const WemVersion = "0.8.1"
const WemVersion = "0.8.2"

type WemConfig struct {
	CacheDir     string

M cli/main.go => cli/main.go +2 -2
@@ 69,7 69,7 @@ func ProcessArgs() {
			}

			a.applyArgs(we)
			err = cmd.Cfg(we, we2, a.Cfg.NoRender, a.Cfg.Save)
			err = cmd.Cfg(we, we2, a.Cfg.NoRender, a.Cfg.Save, envName)
			if err != nil {
				log.Fatal(err.Error())
			}


@@ 104,7 104,7 @@ func ProcessArgs() {
				SysOpts:     &env.SysOpts{},
			}
			a.applyArgs(we)
			err := cmd.Init(c, we)
			err := cmd.Init(c, we, envName)
			if err != nil {
				log.Fatal(err)
			}

M cmd/cfg.go => cmd/cfg.go +3 -3
@@ 8,13 8,13 @@ import (
	"git.sr.ht/~hristoast/wem/env"
)

func Cfg(e *env.WineEnv, old *env.WineEnv, norender, save bool) error {
func Cfg(e *env.WineEnv, old *env.WineEnv, norender, save bool, name string) error {
	// If norender is true, DeepEqual will be false no matter what because
	// the env's internal representation of the values is rendered.
	if !reflect.DeepEqual(e, old) {
		if save {
			log.Printf("Saved: %s\n", e.CfgPath())
			e.Save()
			log.Printf("Saved: %s\n", e.CfgPath(name))
			e.Save(name)

		} else if !norender {
			log.Printf("WARNING: The configuration for this env was changed by CLI arguments!")

M cmd/cfg_test.go => cmd/cfg_test.go +11 -4
@@ 20,6 20,7 @@ func TestCmdCfg(t *testing.T) {
	os.Setenv("HOME", dir)
	os.MkdirAll(filepath.Join(dir, ".config", "wem"), 0755)

	name := "NAMEGOESHERE"
	old := &env.WineEnv{
		Info: &env.Info{
			Date: "ONE TWO THREE",


@@ 64,10 65,10 @@ func TestCmdCfg(t *testing.T) {
			VulkanIcdLoader:    "Eighteen",
		},
	}
	old.Save()
	old.Save(name)

	// Change values
	new, _ := env.FromName(old.Info.Name, true)
	new, _ := env.FromName(name, true)
	new.Info.Date = "New Date"
	new.Info.Name = "New Name"
	new.InstallOpts.GogSilentInstall = true


@@ 104,10 105,16 @@ func TestCmdCfg(t *testing.T) {
	// Save changes via the "cfg" command
	norender := false
	save := true
	_ = cmd.Cfg(new, old, norender, save)
	err = cmd.Cfg(new, old, norender, save, name)
	if err != nil {
		t.Error(err)
	}

	// Load the saved env
	result, _ := env.FromName("New Name", true)
	result, err := env.FromName(name, true)
	if err != nil {
		t.Error(err)
	}

	// Ensure all changes are present
	t.Run("Update: Info.Date", func(t *testing.T) {

M cmd/init.go => cmd/init.go +4 -4
@@ 11,7 11,7 @@ import (
	"git.sr.ht/~hristoast/wem/env"
)

func Init(c *cfg.WemConfig, e *env.WineEnv) error {
func Init(c *cfg.WemConfig, e *env.WineEnv, name string) error {
	_, err := os.Stat(c.WineEnvDir)
	if err != nil {
		err = os.MkdirAll(filepath.Dir(c.WineEnvDir), 0755)


@@ 20,7 20,7 @@ func Init(c *cfg.WemConfig, e *env.WineEnv) error {
		}
	}

	if e.CfgExists() && e.Info.Name != "wem" {
	if e.CfgExists(name) && e.Info.Name != "wem" {
		return fmt.Errorf("an env for \"%s\" already exists, please use the \"cfg\" command to edit it", e.Info.Name)

	} else if e.Info.Name == "wem" {


@@ 32,12 32,12 @@ func Init(c *cfg.WemConfig, e *env.WineEnv) error {
			return err
		}

		err = e.Save()
		err = e.Save(name)
		if err != nil {
			return err
		}
	}

	log.Printf("Env \"%s\" initialized: %s\n", e.Info.Name, e.CfgPath())
	log.Printf("Env \"%s\" initialized: %s\n", e.Info.Name, e.CfgPath(name))
	return nil
}

M cmd/init_test.go => cmd/init_test.go +2 -1
@@ 18,6 18,7 @@ func TestCmdInit(t *testing.T) {
	}
	os.Setenv("HOME", dir)

	name := "NAMEGOESHERE"
	wemCfg, _ := cfg.GetConfig()
	toInit := &env.WineEnv{
		Info: &env.Info{


@@ 65,7 66,7 @@ func TestCmdInit(t *testing.T) {
	}

	// Call init with our crafted WemEnv
	_ = cmd.Init(wemCfg, toInit)
	_ = cmd.Init(wemCfg, toInit, name)
	render := true
	e, _ := env.FromName(toInit.Info.Name, render)


M env/main.go => env/main.go +6 -8
@@ 89,14 89,12 @@ func (e *WineEnv) AsToml() (string, error) {
}

// CfgExists indicates if the env's configuration file exists.
func (e *WineEnv) CfgExists() bool {
	_, err := os.Stat(e.CfgPath())
	return err == nil
func (e *WineEnv) CfgExists(name string) bool {
	return dll.IsFile(e.CfgPath(name)) || dll.IsSymlink(e.CfgPath(name))
}

func (e *WineEnv) CfgPath() string {
	s, _ := e.Slug()
	return filepath.Join(cfg.CfgDir(), fmt.Sprintf("%s.cfg", s))
func (e *WineEnv) CfgPath(name string) string {
	return filepath.Join(cfg.CfgDir(), fmt.Sprintf("%s.cfg", name))
}

// HasInstallOpts indicates whether the minimum required config for installation is present


@@ 584,13 582,13 @@ func (e *WineEnv) RenderString(s string) (string, error) {
}

// Save writes the WineEnv to disk as a toml-formatted cfg file.
func (e *WineEnv) Save() error {
func (e *WineEnv) Save(name string) error {
	t, err := e.AsToml()
	if err != nil {
		return err
	}

	c := e.CfgPath()
	c := e.CfgPath(name)
	f, err := os.Create(c)
	if err != nil {
		return err

M env/main_test.go => env/main_test.go +5 -4
@@ 30,6 30,7 @@ func TestEnv(t *testing.T) {
	os.MkdirAll(prefixPath, 0755)
	os.MkdirAll(vkd3dDir, 0755)

	name := "NAMEGOESHERE"
	e := &env.WineEnv{
		Info: &env.Info{
			Date: "ONE TWO THREE",


@@ 74,7 75,7 @@ func TestEnv(t *testing.T) {
			VulkanIcdLoader:    "Eighteen",
		},
	}
	e.Save()
	e.Save(name)

	// Tests
	t.Run("TestAsToml", func(t *testing.T) {


@@ 122,14 123,14 @@ WinetricksPath = "Seventeen"
	})

	t.Run("CfgExists: does", func(t *testing.T) {
		exists := e.CfgExists()
		exists := e.CfgExists(name)
		if exists != true {
			t.Error("got: false; want: true")
		}
	})

	t.Run("CfgPath", func(t *testing.T) {
		p := e.CfgPath()
		p := e.CfgPath(name)
		expected := filepath.Join(dir, ".config", "wem", "NAMEGOESHERE.cfg")
		if p != expected {
			t.Errorf("got: %s; want: %s", p, expected)


@@ 223,7 224,7 @@ WinetricksPath = "Seventeen"
	os.RemoveAll(dir)

	t.Run("CfgExists: doesn't", func(t *testing.T) {
		exists := e.CfgExists()
		exists := e.CfgExists(name)
		if exists != false {
			t.Error("got: true; want: false")