Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Pathname

This is a implementation of Ruby's Pathname class.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/mai7star/pathname

go 1.24.4

require github.com/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
126 changes: 126 additions & 0 deletions pathname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package pathname

import (
"os"
"path/filepath"
"slices"
)

type Pathname struct {
path string
}

func NewPathname(path string) Pathname {
return Pathname{
path: path,
}
}

func NewPathnamePtr(path string) *Pathname {
pathname := NewPathname(path)
return &pathname
}

func (p *Pathname) ToPath() string {
if p == nil {
return ""
}
return p.path
}

func (p *Pathname) IsAbsolute() bool {
if p == nil || p.path == "" {
return false
}
return p.path[0] == '/'
}

func (p *Pathname) IsRelative() bool {
if p == nil || p.path == "" {
return false
}
return p.path[0] != '/'
}

func (p *Pathname) IsRoot() bool {
if p == nil || p.path == "" {
return false
}
return p.path == "/"
}

func (p *Pathname) IsExist() bool {
if p == nil {
return false
}

_, err := os.Stat(p.path)
return err == nil
}

func (p *Pathname) IsFile() bool {
if p == nil {
return false
}

info, err := os.Stat(p.path)
if err != nil {
return false
}

return !info.IsDir()
}

func (p *Pathname) IsDirectory() bool {
if p == nil {
return false
}

info, err := os.Stat(p.path)
if err != nil {
return false
}

return info.IsDir()
}

func (p *Pathname) Basename() *Pathname {
if p == nil {
return nil
}

path := filepath.Base(p.path)
return NewPathnamePtr(path)
}

func (p *Pathname) Dirname() *Pathname {
if p == nil {
return nil
}

path := filepath.Dir(p.path)
return NewPathnamePtr(path)
}

func (p *Pathname) Join(parts ...string) *Pathname {
if p == nil {
return nil
}

parts = slices.Concat([]string{p.path}, parts)
path := filepath.Join(parts...)
return NewPathnamePtr(path)
}

func (p *Pathname) OpenFile(flag int, perm os.FileMode) (*os.File, error) {
return os.OpenFile(p.ToPath(), flag, perm)
}

func (p *Pathname) Size() (int, error) {
info, err := os.Stat(p.ToPath())
if err != nil {
return 0, err
}

return int(info.Size()), nil
}
Loading