mirror of
				https://github.com/cheat/cheat.git
				synced 2025-11-04 07:45:28 +01:00 
			
		
		
		
	fix: Windows support
Fix an issue whereby the installer installed cheatsheets into the wrong directory on Windows. This occurred because previously `path.Join` was used where `path/filepath.Join` should have been used. This matters, because the former always uses `/` as the path separator, whereas the latter will use `/` or `\` as is appropriate for the runtime environment. This should resolve bullet point 4 in #665.
This commit is contained in:
		@@ -39,17 +39,17 @@ func TestConfigSuccessful(t *testing.T) {
 | 
			
		||||
	// assert that the cheatpaths are correct
 | 
			
		||||
	want := []cheatpath.Cheatpath{
 | 
			
		||||
		cheatpath.Cheatpath{
 | 
			
		||||
			Path:     filepath.Join(home, ".dotfiles/cheat/community"),
 | 
			
		||||
			Path:     filepath.Join(home, ".dotfiles", "cheat", "community"),
 | 
			
		||||
			ReadOnly: true,
 | 
			
		||||
			Tags:     []string{"community"},
 | 
			
		||||
		},
 | 
			
		||||
		cheatpath.Cheatpath{
 | 
			
		||||
			Path:     filepath.Join(home, ".dotfiles/cheat/work"),
 | 
			
		||||
			Path:     filepath.Join(home, ".dotfiles", "cheat", "work"),
 | 
			
		||||
			ReadOnly: false,
 | 
			
		||||
			Tags:     []string{"work"},
 | 
			
		||||
		},
 | 
			
		||||
		cheatpath.Cheatpath{
 | 
			
		||||
			Path:     filepath.Join(home, ".dotfiles/cheat/personal"),
 | 
			
		||||
			Path:     filepath.Join(home, ".dotfiles", "cheat", "personal"),
 | 
			
		||||
			ReadOnly: false,
 | 
			
		||||
			Tags:     []string{"personal"},
 | 
			
		||||
		},
 | 
			
		||||
 
 | 
			
		||||
@@ -33,20 +33,20 @@ func Paths(
 | 
			
		||||
 | 
			
		||||
		// don't include the `XDG_CONFIG_HOME` path if that envvar is not set
 | 
			
		||||
		if xdgpath, ok := envvars["XDG_CONFIG_HOME"]; ok {
 | 
			
		||||
			paths = append(paths, filepath.Join(xdgpath, "/cheat/conf.yml"))
 | 
			
		||||
			paths = append(paths, filepath.Join(xdgpath, "cheat", "conf.yml"))
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		paths = append(paths, []string{
 | 
			
		||||
			filepath.Join(home, ".config/cheat/conf.yml"),
 | 
			
		||||
			filepath.Join(home, ".cheat/conf.yml"),
 | 
			
		||||
			filepath.Join(home, ".config", "cheat", "conf.yml"),
 | 
			
		||||
			filepath.Join(home, ".cheat", "conf.yml"),
 | 
			
		||||
			"/etc/cheat/conf.yml",
 | 
			
		||||
		}...)
 | 
			
		||||
 | 
			
		||||
		return paths, nil
 | 
			
		||||
	case "windows":
 | 
			
		||||
		return []string{
 | 
			
		||||
			filepath.Join(envvars["APPDATA"], "/cheat/conf.yml"),
 | 
			
		||||
			filepath.Join(envvars["PROGRAMDATA"], "/cheat/conf.yml"),
 | 
			
		||||
			filepath.Join(envvars["APPDATA"], "cheat", "conf.yml"),
 | 
			
		||||
			filepath.Join(envvars["PROGRAMDATA"], "cheat", "conf.yml"),
 | 
			
		||||
		}, nil
 | 
			
		||||
	default:
 | 
			
		||||
		return []string{}, fmt.Errorf("unsupported os: %s", sys)
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ package installer
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/cheat/cheat/internal/config"
 | 
			
		||||
@@ -14,11 +14,11 @@ func Run(configs string, confpath string) error {
 | 
			
		||||
 | 
			
		||||
	// determine the appropriate paths for config data and (optional) community
 | 
			
		||||
	// cheatsheets based on the user's platform
 | 
			
		||||
	confdir := path.Dir(confpath)
 | 
			
		||||
	confdir := filepath.Dir(confpath)
 | 
			
		||||
 | 
			
		||||
	// create paths for community and personal cheatsheets
 | 
			
		||||
	community := path.Join(confdir, "/cheatsheets/community")
 | 
			
		||||
	personal := path.Join(confdir, "/cheatsheets/personal")
 | 
			
		||||
	community := filepath.Join(confdir, "cheatsheets", "community")
 | 
			
		||||
	personal := filepath.Join(confdir, "cheatsheets", "personal")
 | 
			
		||||
 | 
			
		||||
	// template the above paths into the default configs
 | 
			
		||||
	configs = strings.Replace(configs, "COMMUNITY_PATH", community, -1)
 | 
			
		||||
@@ -36,11 +36,13 @@ func Run(configs string, confpath string) error {
 | 
			
		||||
	// clone the community cheatsheets if so instructed
 | 
			
		||||
	if yes {
 | 
			
		||||
		// clone the community cheatsheets
 | 
			
		||||
		fmt.Printf("Cloning community cheatsheets to %s.\n", community)
 | 
			
		||||
		if err := clone(community); err != nil {
 | 
			
		||||
			return fmt.Errorf("failed to clone cheatsheets: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// also create a directory for personal cheatsheets
 | 
			
		||||
		fmt.Printf("Cloning personal cheatsheets to %s.\n", personal)
 | 
			
		||||
		if err := os.MkdirAll(personal, os.ModePerm); err != nil {
 | 
			
		||||
			return fmt.Errorf("failed to create directory: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Copy copies a cheatsheet to a new location
 | 
			
		||||
@@ -22,7 +22,7 @@ func (s *Sheet) Copy(dest string) error {
 | 
			
		||||
	defer infile.Close()
 | 
			
		||||
 | 
			
		||||
	// create any necessary subdirectories
 | 
			
		||||
	dirs := path.Dir(dest)
 | 
			
		||||
	dirs := filepath.Dir(dest)
 | 
			
		||||
	if dirs != "." {
 | 
			
		||||
		if err := os.MkdirAll(dirs, 0755); err != nil {
 | 
			
		||||
			return fmt.Errorf("failed to create directory: %s, %v", dirs, err)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user