Implement cli argument for file path, path validation, file type validation
This commit is contained in:
parent
1667fb10ef
commit
26adec4a97
6 changed files with 111 additions and 2 deletions
33
internal/util/path.go
Normal file
33
internal/util/path.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Package util provides general utilities.
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// CheckExtension checks if the file located at path (string)
|
||||
// matches the provided extension type
|
||||
func CheckExtension(path, ext string) error {
|
||||
if filepath.Ext(path) == ext {
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("Invalid extension.")
|
||||
}
|
||||
}
|
||||
|
||||
// PathIsValid checks if a path is valid.
|
||||
// If requireFile is set, directories are not considered valid.
|
||||
func PathIsValid(path string, requireFile bool) bool {
|
||||
s, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
} else if requireFile {
|
||||
fmt.Printf("Directory status: %s\n", strconv.FormatBool(s.IsDir()))
|
||||
return !s.IsDir()
|
||||
}
|
||||
return err == nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue