path -> filepath

This commit is contained in:
Vilsol 2022-06-05 04:56:46 +03:00
parent 4e9d5b010f
commit ac1dfb6148
7 changed files with 41 additions and 39 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -39,7 +38,7 @@ type Installation struct {
func InitInstallations() (*Installations, error) { func InitInstallations() (*Installations, error) {
localDir := viper.GetString("local-dir") localDir := viper.GetString("local-dir")
installationsFile := path.Join(localDir, viper.GetString("installations-file")) installationsFile := filepath.Join(localDir, viper.GetString("installations-file"))
_, err := os.Stat(installationsFile) _, err := os.Stat(installationsFile)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
@ -90,7 +89,7 @@ func (i *Installations) Save() error {
return nil return nil
} }
installationsFile := path.Join(viper.GetString("local-dir"), viper.GetString("installations-file")) installationsFile := filepath.Join(viper.GetString("local-dir"), viper.GetString("installations-file"))
log.Info().Str("path", installationsFile).Msg("saving installations") log.Info().Str("path", installationsFile).Msg("saving installations")
@ -192,7 +191,7 @@ func (i *Installation) Validate(ctx *GlobalContext) error {
foundExecutable := false foundExecutable := false
_, err := os.Stat(path.Join(i.Path, "FactoryGame.exe")) _, err := os.Stat(filepath.Join(i.Path, "FactoryGame.exe"))
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return errors.Wrap(err, "failed reading FactoryGame.exe") return errors.Wrap(err, "failed reading FactoryGame.exe")
@ -201,7 +200,7 @@ func (i *Installation) Validate(ctx *GlobalContext) error {
foundExecutable = true foundExecutable = true
} }
_, err = os.Stat(path.Join(i.Path, "FactoryServer.sh")) _, err = os.Stat(filepath.Join(i.Path, "FactoryServer.sh"))
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return errors.Wrap(err, "failed reading FactoryServer.sh") return errors.Wrap(err, "failed reading FactoryServer.sh")
@ -210,7 +209,7 @@ func (i *Installation) Validate(ctx *GlobalContext) error {
foundExecutable = true foundExecutable = true
} }
_, err = os.Stat(path.Join(i.Path, "FactoryServer.exe")) _, err = os.Stat(filepath.Join(i.Path, "FactoryServer.exe"))
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return errors.Wrap(err, "failed reading FactoryServer.exe") return errors.Wrap(err, "failed reading FactoryServer.exe")
@ -244,7 +243,7 @@ func (i *Installation) LockFilePath(ctx *GlobalContext) (string, error) {
lockFileName = lockFileCleaner.ReplaceAllLiteralString(lockFileName, "-") lockFileName = lockFileCleaner.ReplaceAllLiteralString(lockFileName, "-")
lockFileName = strings.ToLower(lockFileName) + "-lock.json" lockFileName = strings.ToLower(lockFileName) + "-lock.json"
return path.Join(i.Path, platform.LockfilePath, lockFileName), nil return filepath.Join(i.Path, platform.LockfilePath, lockFileName), nil
} }
func (i *Installation) LockFile(ctx *GlobalContext) (*LockFile, error) { func (i *Installation) LockFile(ctx *GlobalContext) (*LockFile, error) {
@ -317,7 +316,7 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
return errors.Wrap(err, "could not resolve mods") return errors.Wrap(err, "could not resolve mods")
} }
modsDirectory := path.Join(i.Path, "FactoryGame", "Mods") modsDirectory := filepath.Join(i.Path, "FactoryGame", "Mods")
if err := os.MkdirAll(modsDirectory, 0777); err != nil { if err := os.MkdirAll(modsDirectory, 0777); err != nil {
return errors.Wrap(err, "failed creating Mods directory") return errors.Wrap(err, "failed creating Mods directory")
} }
@ -330,7 +329,7 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
for _, entry := range dir { for _, entry := range dir {
if entry.IsDir() { if entry.IsDir() {
if _, ok := lockfile[entry.Name()]; !ok { if _, ok := lockfile[entry.Name()]; !ok {
if err := os.RemoveAll(path.Join(modsDirectory, entry.Name())); err != nil { if err := os.RemoveAll(filepath.Join(modsDirectory, entry.Name())); err != nil {
return errors.Wrap(err, "failed to delete mod directory") return errors.Wrap(err, "failed to delete mod directory")
} }
} }
@ -385,7 +384,7 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
downloading = false downloading = false
log.Info().Str("mod_reference", modReference).Str("version", version.Version).Str("link", version.Link).Msg("extracting mod") log.Info().Str("mod_reference", modReference).Str("version", version.Version).Str("link", version.Link).Msg("extracting mod")
if err := utils.ExtractMod(reader, size, path.Join(modsDirectory, modReference), genericUpdates); err != nil { if err := utils.ExtractMod(reader, size, filepath.Join(modsDirectory, modReference), genericUpdates); err != nil {
return errors.Wrap(err, "could not extract "+modReference) return errors.Wrap(err, "could not extract "+modReference)
} }
@ -444,7 +443,7 @@ func (i *Installation) GetGameVersion(ctx *GlobalContext) (int, error) {
return 0, err return 0, err
} }
fullPath := path.Join(i.Path, platform.VersionPath) fullPath := filepath.Join(i.Path, platform.VersionPath)
file, err := os.ReadFile(fullPath) file, err := os.ReadFile(fullPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -467,7 +466,7 @@ func (i *Installation) GetPlatform(ctx *GlobalContext) (*Platform, error) {
} }
for _, platform := range platforms { for _, platform := range platforms {
fullPath := path.Join(i.Path, platform.VersionPath) fullPath := filepath.Join(i.Path, platform.VersionPath)
_, err := os.Stat(fullPath) _, err := os.Stat(fullPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {

View file

@ -1,6 +1,6 @@
package cli package cli
import "path" import "path/filepath"
type Platform struct { type Platform struct {
VersionPath string VersionPath string
@ -9,15 +9,15 @@ type Platform struct {
var platforms = []Platform{ var platforms = []Platform{
{ {
VersionPath: path.Join("Engine", "Binaries", "Linux", "UE4Server-Linux-Shipping.version"), VersionPath: filepath.Join("Engine", "Binaries", "Linux", "UE4Server-Linux-Shipping.version"),
LockfilePath: path.Join("FactoryGame", "Mods"), LockfilePath: filepath.Join("FactoryGame", "Mods"),
}, },
{ {
VersionPath: path.Join("Engine", "Binaries", "Win64", "UE4Server-Win64-Shipping.version"), VersionPath: filepath.Join("Engine", "Binaries", "Win64", "UE4Server-Win64-Shipping.version"),
LockfilePath: path.Join("FactoryGame", "Mods"), LockfilePath: filepath.Join("FactoryGame", "Mods"),
}, },
{ {
VersionPath: path.Join("Engine", "Binaries", "Win64", "FactoryGame-Win64-Shipping.version"), VersionPath: filepath.Join("Engine", "Binaries", "Win64", "FactoryGame-Win64-Shipping.version"),
LockfilePath: path.Join("FactoryGame", "Mods"), LockfilePath: filepath.Join("FactoryGame", "Mods"),
}, },
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path" "path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -54,7 +54,7 @@ type ProfileMod struct {
func InitProfiles() (*Profiles, error) { func InitProfiles() (*Profiles, error) {
localDir := viper.GetString("local-dir") localDir := viper.GetString("local-dir")
profilesFile := path.Join(localDir, viper.GetString("profiles-file")) profilesFile := filepath.Join(localDir, viper.GetString("profiles-file"))
_, err := os.Stat(profilesFile) _, err := os.Stat(profilesFile)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
@ -78,14 +78,14 @@ func InitProfiles() (*Profiles, error) {
} }
// Import profiles from SMM if already exists // Import profiles from SMM if already exists
smmProfilesDir := path.Join(viper.GetString("base-local-dir"), "SatisfactoryModManager", "profiles") smmProfilesDir := filepath.Join(viper.GetString("base-local-dir"), "SatisfactoryModManager", "profiles")
_, err = os.Stat(smmProfilesDir) _, err = os.Stat(smmProfilesDir)
if err == nil { if err == nil {
dir, err := os.ReadDir(smmProfilesDir) dir, err := os.ReadDir(smmProfilesDir)
if err == nil { if err == nil {
for _, entry := range dir { for _, entry := range dir {
if entry.IsDir() { if entry.IsDir() {
manifestFile := path.Join(smmProfilesDir, entry.Name(), "manifest.json") manifestFile := filepath.Join(smmProfilesDir, entry.Name(), "manifest.json")
_, err := os.Stat(manifestFile) _, err := os.Stat(manifestFile)
if err == nil { if err == nil {
manifestBytes, err := os.ReadFile(manifestFile) manifestBytes, err := os.ReadFile(manifestFile)
@ -169,7 +169,7 @@ func (p *Profiles) Save() error {
return nil return nil
} }
profilesFile := path.Join(viper.GetString("local-dir"), viper.GetString("profiles-file")) profilesFile := filepath.Join(viper.GetString("local-dir"), viper.GetString("profiles-file"))
log.Info().Str("path", profilesFile).Msg("saving profiles") log.Info().Str("path", profilesFile).Msg("saving profiles")

View file

@ -3,7 +3,6 @@ package cmd
import ( import (
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"time" "time"
@ -96,7 +95,7 @@ func init() {
case "windows": case "windows":
baseLocalDir = os.Getenv("APPDATA") baseLocalDir = os.Getenv("APPDATA")
case "linux": case "linux":
baseLocalDir = path.Join(os.Getenv("HOME"), ".local", "share") baseLocalDir = filepath.Join(os.Getenv("HOME"), ".local", "share")
default: default:
panic("unsupported platform: " + runtime.GOOS) panic("unsupported platform: " + runtime.GOOS)
} }

View file

@ -167,9 +167,13 @@ func (m mainMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m mainMenu) View() string { func (m mainMenu) View() string {
banner := lipgloss.NewStyle().Margin(2, 0, 0, 2).Render(m.banner) header := m.root.View()
header := lipgloss.JoinVertical(lipgloss.Left, banner, m.root.View()) banner := lipgloss.NewStyle().Margin(2, 0, 0, 2).Render(m.banner)
totalHeight := m.root.Height() + len(m.list.Items()) + lipgloss.Height(banner) + 4
if totalHeight < m.root.Size().Height {
header = lipgloss.JoinVertical(lipgloss.Left, banner, m.root.View())
}
if m.error != nil { if m.error != nil {
err := (*m.error).View() err := (*m.error).View()

View file

@ -2,7 +2,7 @@ package scenes
import ( import (
"os" "os"
"path" "path/filepath"
"time" "time"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
@ -101,9 +101,9 @@ func (m newInstallation) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
newPath := "" newPath := ""
_, err := os.ReadDir(m.input.Value()) _, err := os.ReadDir(m.input.Value())
if err == nil { if err == nil {
newPath = path.Join(m.input.Value(), newDir) newPath = filepath.Join(m.input.Value(), newDir)
} else { } else {
newPath = path.Join(path.Dir(m.input.Value()), newDir) newPath = filepath.Join(filepath.Dir(m.input.Value()), newDir)
} }
m.input.SetValue(newPath + string(os.PathSeparator)) m.input.SetValue(newPath + string(os.PathSeparator))
@ -150,7 +150,7 @@ func (m newInstallation) View() string {
} }
if len(m.dirList.Items()) > 0 { if len(m.dirList.Items()) > 0 {
m.dirList.SetSize(m.dirList.Width(), m.root.Size().Height-lipgloss.Height(mandatory)) m.dirList.SetSize(m.dirList.Width(), m.root.Size().Height-lipgloss.Height(mandatory)-1)
return lipgloss.JoinVertical(lipgloss.Left, mandatory, m.dirList.View()) return lipgloss.JoinVertical(lipgloss.Left, mandatory, m.dirList.View())
} }
@ -162,9 +162,9 @@ func getDirItems(inputValue string) []list.Item {
filter := "" filter := ""
dir, err := os.ReadDir(inputValue) dir, err := os.ReadDir(inputValue)
if err != nil { if err != nil {
dir, err = os.ReadDir(path.Dir(inputValue)) dir, err = os.ReadDir(filepath.Dir(inputValue))
if err == nil { if err == nil {
filter = path.Base(inputValue) filter = filepath.Base(inputValue)
} }
} }

View file

@ -8,7 +8,7 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"path" "path/filepath"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -46,14 +46,14 @@ type GenericUpdate struct {
} }
func DownloadOrCache(cacheKey string, hash string, url string, updates chan GenericUpdate) (r io.ReaderAt, size int64, err error) { func DownloadOrCache(cacheKey string, hash string, url string, updates chan GenericUpdate) (r io.ReaderAt, size int64, err error) {
downloadCache := path.Join(viper.GetString("cache-dir"), "downloadCache") downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
if err := os.MkdirAll(downloadCache, 0777); err != nil { if err := os.MkdirAll(downloadCache, 0777); err != nil {
if !os.IsExist(err) { if !os.IsExist(err) {
return nil, 0, errors.Wrap(err, "failed creating download cache") return nil, 0, errors.Wrap(err, "failed creating download cache")
} }
} }
location := path.Join(downloadCache, cacheKey) location := filepath.Join(downloadCache, cacheKey)
stat, err := os.Stat(location) stat, err := os.Stat(location)
if err == nil { if err == nil {
@ -162,9 +162,9 @@ func ExtractMod(f io.ReaderAt, size int64, location string, updates chan Generic
for i, file := range reader.File { for i, file := range reader.File {
if !file.FileInfo().IsDir() { if !file.FileInfo().IsDir() {
outFileLocation := path.Join(location, file.Name) outFileLocation := filepath.Join(location, file.Name)
if err := os.MkdirAll(path.Dir(outFileLocation), 0777); err != nil { if err := os.MkdirAll(filepath.Dir(outFileLocation), 0777); err != nil {
return errors.Wrap(err, "failed to create mod directory: "+location) return errors.Wrap(err, "failed to create mod directory: "+location)
} }