Skip to content

Implement Create Directory Function #12965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions directories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package koii

import (
"fmt"
"os"
"path/filepath"
"strings"
)

// CreateDirectory creates a new directory with the given path
// It returns an error if the directory cannot be created
func CreateDirectory(path string) error {
path = filepath.Clean(path)

if !isDirPathValid(path) {
return fmt.Errorf("invalid directory path: %s", path)
}

err := os.MkdirAll(path, 0755)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

return nil
}

// isDirPathValid checks if the given path is a valid directory path
func isDirPathValid(path string) bool {
if strings.ContainsAny(path, "\\/?%*:|\"<>") {
return false
}

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

return info.IsDir()
}