move filemode parsing to its own package
why doesn't the std lib have this???
This commit is contained in:
parent
a5e0a2546c
commit
095f59babb
25
internal/filemode/filemode.go
Normal file
25
internal/filemode/filemode.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package filemode
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Parse parses a string of 3 or 4 numbers as a *NIX filesystem permission.
|
||||
//
|
||||
// "0777" or "777" -> fs.FileMode(0777)
|
||||
//
|
||||
// "0644" or "644" -> fs.FileMode(0644)
|
||||
func Parse(in string) (fs.FileMode, error) {
|
||||
if in == "" {
|
||||
return fs.FileMode(0), nil
|
||||
}
|
||||
if len(in) == 3 {
|
||||
in = "0" + in
|
||||
}
|
||||
md, e := strconv.ParseUint(in, 8, 64)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
return fs.FileMode(md), nil
|
||||
}
|
21
main.go
21
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.burning.moe/celediel/gt/internal/filemode"
|
||||
"git.burning.moe/celediel/gt/internal/files"
|
||||
"git.burning.moe/celediel/gt/internal/filter"
|
||||
"git.burning.moe/celediel/gt/internal/prompt"
|
||||
|
@ -90,7 +91,7 @@ var (
|
|||
)
|
||||
|
||||
if f == nil {
|
||||
md, e := parseMode(m)
|
||||
md, e := filemode.Parse(m)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
@ -118,7 +119,7 @@ var (
|
|||
beforeCommands = func(ctx *cli.Context) (err error) {
|
||||
// setup filter
|
||||
if f == nil {
|
||||
md, e := parseMode(m)
|
||||
md, e := filemode.Parse(m)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
@ -130,7 +131,7 @@ var (
|
|||
|
||||
beforeTrash = func(_ *cli.Context) (err error) {
|
||||
if f == nil {
|
||||
md, e := parseMode(m)
|
||||
md, e := filemode.Parse(m)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
@ -558,17 +559,3 @@ func confirmTrash(confirm bool, fs files.Files) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseMode(in string) (fs.FileMode, error) {
|
||||
if in == "" {
|
||||
return fs.FileMode(0), nil
|
||||
}
|
||||
if len(m) == 3 {
|
||||
in = "0" + in
|
||||
}
|
||||
md, e := strconv.ParseUint(in, 8, 64)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
return fs.FileMode(md), nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue