Skip to content

Commit

Permalink
👔 up: all - use path/filepath instead of path for support Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 7, 2024
1 parent 342cd12 commit ba0623b
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 29 deletions.
6 changes: 3 additions & 3 deletions cflag/cflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"flag"
"io"
"os"
"path"
"path/filepath"
"strings"

"github.com/gookit/color"
Expand Down Expand Up @@ -399,7 +399,7 @@ func (c *CFlags) RemainArgs() []string { return c.remainArgs }

// Name for command
func (c *CFlags) Name() string {
return path.Base(c.FlagSet.Name())
return filepath.Base(c.FlagSet.Name())
}

// BinFile path for command
Expand Down Expand Up @@ -497,7 +497,7 @@ func (c *CFlags) renderOptionsHelp(buf *strutil.Buffer) {
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
b.WriteString("\n \t")
b.WriteString("\n \t")
}
b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))

Expand Down
6 changes: 3 additions & 3 deletions cliutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cliutil

import (
"os"
"path"
"path/filepath"

"golang.org/x/term"
)
Expand All @@ -15,7 +15,7 @@ func Workdir() string {

// BinDir get
func BinDir() string {
return path.Dir(os.Args[0])
return filepath.Dir(os.Args[0])
}

// BinFile get
Expand All @@ -25,7 +25,7 @@ func BinFile() string {

// BinName get
func BinName() string {
return path.Base(os.Args[0])
return filepath.Base(os.Args[0])
}

// exec: `stty -a 2>&1`
Expand Down
6 changes: 6 additions & 0 deletions comdef/consts_nonwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !windows

package comdef

// Newline string for non-windows
const Newline = "\n"
4 changes: 4 additions & 0 deletions comdef/consts_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package comdef

// Newline string for windows
const Newline = "\r\n"
4 changes: 2 additions & 2 deletions dump/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strconv"
Expand Down Expand Up @@ -149,7 +149,7 @@ func (d *Dumper) printCaller(pc uintptr, file string, line int) {
case Ffile: // full file path
nodes = append(nodes, file)
case Ffname: // only file name
fName := path.Base(file) // file name
fName := filepath.Base(file) // file name
nodes = append(nodes, fName)
default: // Fline
nodes = append(nodes, ":", lineS)
Expand Down
4 changes: 2 additions & 2 deletions errorx/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"io"
"path"
"path/filepath"
"runtime"
"strconv"
)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (f *Func) FileLine() (file string, line int) {
func (f *Func) Location() string {
file, line := f.FileLine()

return f.Name() + "(), " + path.Base(file) + ":" + strconv.Itoa(line)
return f.Name() + "(), " + filepath.Base(file) + ":" + strconv.Itoa(line)
}

// String of the func
Expand Down
15 changes: 7 additions & 8 deletions fsutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fsutil

import (
"os"
"path"
"path/filepath"

"github.com/gookit/goutil/internal/comfunc"
Expand All @@ -15,7 +14,7 @@ func DirPath(fpath string) string { return filepath.Dir(fpath) }
func Dir(fpath string) string { return filepath.Dir(fpath) }

// PathName get file/dir name from full path
func PathName(fpath string) string { return path.Base(fpath) }
func PathName(fpath string) string { return filepath.Base(fpath) }

// Name get file/dir name from full path.
//
Expand All @@ -27,25 +26,25 @@ func Name(fpath string) string {
return filepath.Base(fpath)
}

// FileExt get filename ext. alias of path.Ext()
// FileExt get filename ext. alias of filepath.Ext()
//
// eg: path/to/main.go => ".go"
func FileExt(fpath string) string { return path.Ext(fpath) }
func FileExt(fpath string) string { return filepath.Ext(fpath) }

// Extname get filename ext. alias of path.Ext()
// Extname get filename ext. alias of filepath.Ext()
//
// eg: path/to/main.go => "go"
func Extname(fpath string) string {
if ext := path.Ext(fpath); len(ext) > 0 {
if ext := filepath.Ext(fpath); len(ext) > 0 {
return ext[1:]
}
return ""
}

// Suffix get filename ext. alias of path.Ext()
// Suffix get filename ext. alias of filepath.Ext()
//
// eg: path/to/main.go => ".go"
func Suffix(fpath string) string { return path.Ext(fpath) }
func Suffix(fpath string) string { return filepath.Ext(fpath) }

// Expand will parse first `~` as user home dir path.
func Expand(pathStr string) string {
Expand Down
5 changes: 2 additions & 3 deletions fsutil/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -121,7 +120,7 @@ const (
//
// file, err := OpenFile("path/to/file.txt", FsCWFlags, 0666)
func OpenFile(filePath string, flag int, perm os.FileMode) (*os.File, error) {
fileDir := path.Dir(filePath)
fileDir := filepath.Dir(filePath)
if err := os.MkdirAll(fileDir, DefaultDirPerm); err != nil {
return nil, err
}
Expand Down Expand Up @@ -177,7 +176,7 @@ func OpenReadFile(filepath string) (*os.File, error) {
//
// CreateFile("path/to/file.txt", 0664, 0666)
func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error) {
dirPath := path.Dir(fpath)
dirPath := filepath.Dir(fpath)
if !IsDir(dirPath) {
err := os.MkdirAll(dirPath, dirPerm)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions goinfo/stack.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package goinfo

import (
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -91,7 +91,7 @@ func GetCallersInfo(skip, max int) []string {

if strings.ContainsRune(file, '/') {
name = fc.Name()
file = path.Base(file)
file = filepath.Base(file)
// eg: github.com/gookit/goutil/goinfo_test.someFunc2(),stack_test.go:26
callers = append(callers, name+"(),"+file+":"+strconv.Itoa(line))
}
Expand Down
5 changes: 2 additions & 3 deletions internal/gendoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -87,9 +86,9 @@ func (o genOptsSt) tplFilename() string {

func (o genOptsSt) tplFilepath(givePath string) string {
if givePath != "" {
return path.Join(o.tplDir, givePath)
return filepath.Join(o.tplDir, givePath)
}
return path.Join(o.tplDir, o.tplFilename())
return filepath.Join(o.tplDir, o.tplFilename())
}

var (
Expand Down
6 changes: 3 additions & 3 deletions testutil/fakeobj/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fakeobj
import (
"io"
"io/fs"
"path"
"path/filepath"
"time"

"github.com/gookit/goutil/basefn"
Expand All @@ -21,7 +21,7 @@ type DirEntry struct {
// NewDirEntry create a fs.DirEntry
func NewDirEntry(fpath string, isDir ...bool) *DirEntry {
isd := basefn.FirstOr(isDir, false)
return &DirEntry{Nam: path.Base(fpath), Dir: isd, Mod: fs.ModePerm}
return &DirEntry{Nam: filepath.Base(fpath), Dir: isd, Mod: fs.ModePerm}
}

// Name get
Expand Down Expand Up @@ -76,7 +76,7 @@ func NewFile(fpath string) *FileInfo {
func NewFileInfo(fpath string, isDir ...bool) *FileInfo {
return &FileInfo{
Dir: basefn.FirstOr(isDir, false),
Nam: path.Base(fpath),
Nam: filepath.Base(fpath),
Mod: fs.ModePerm,
Path: fpath,
}
Expand Down

0 comments on commit ba0623b

Please sign in to comment.