-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for repository based schema loading
- Loading branch information
Showing
29 changed files
with
1,661 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
output | ||
bin | ||
/output | ||
/bin | ||
ocsf-schema.json | ||
ocsf-schema-*.json | ||
download/ocsf-tool/ | ||
config.yaml | ||
config.yaml | ||
/schema | ||
/schema/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package commons | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// EnsureDirExists checks if a directory exists at the given path, and creates it if it doesn't exist. | ||
func EnsureDirExists(path string) error { | ||
|
||
path = CleanPath(path) | ||
path = Dir(path) | ||
|
||
if !PathExists(path) { | ||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Extract Dir path from file path | ||
func Dir(filePath string) string { | ||
return filepath.Dir(filePath) | ||
} | ||
|
||
// Path Exists | ||
func PathExists(path string) bool { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// CreateFile creates a file at the given path and writes the contents to the file. | ||
// It overwrites the file if it already exists. | ||
func CreateFile(path string, contents []byte) error { | ||
|
||
// Ensure the directory exists | ||
err := EnsureDirExists(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write contents to the file | ||
err = os.WriteFile(path, contents, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// PathPrepare replaces $HOME, $TMP, $CWD with their respective paths | ||
func PathPrepare(path string) string { | ||
|
||
// replace $HOME with Home path if path contains $HOME is present | ||
if strings.HasPrefix(path, "$HOME") { | ||
path = strings.Replace(path, "$HOME", os.Getenv("HOME"), 1) | ||
} | ||
|
||
// replace $TMP with Temp path if path contains $TMP is present | ||
if strings.HasPrefix(path, "$TMP") { | ||
path = strings.Replace(path, "$TMP", os.Getenv("TMP"), 1) | ||
} | ||
|
||
// replace $CWD with current working path if path contains $CWD is present | ||
if strings.HasPrefix(path, "$CWD") { | ||
path = strings.Replace(path, "$CWD", os.Getenv("PWD"), 1) | ||
} | ||
|
||
// remove double slashes | ||
path = CleanPath(path) | ||
|
||
return path | ||
} | ||
|
||
// clean Path string | ||
func CleanPath(path string) string { | ||
// remove double slashes | ||
path = strings.Replace(path, "//", "/", 1) | ||
|
||
return path | ||
} | ||
|
||
// Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. | ||
func Walk(root string, walkFn filepath.WalkFunc) error { | ||
return filepath.Walk(root, walkFn) | ||
} | ||
|
||
// FileName from path | ||
func FileName(path string) string { | ||
return filepath.Base(path) | ||
} | ||
|
||
// FilenameWithoutExtension from path | ||
func FilenameWithoutExtension(path string) string { | ||
return strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package commons | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestPathPrepare(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
expected string | ||
}{ | ||
{ | ||
name: "Replace $HOME", | ||
path: "$HOME/test", | ||
expected: os.Getenv("HOME") + "/test", | ||
}, | ||
{ | ||
name: "Replace $TMP", | ||
path: "$TMP/test", | ||
expected: os.Getenv("TMP") + "/test", | ||
}, | ||
{ | ||
name: "Replace $CWD", | ||
path: "$CWD/test", | ||
expected: os.Getenv("PWD") + "/test", | ||
}, | ||
{ | ||
name: "No replacements", | ||
path: "/test/path", | ||
expected: "/test/path", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result := PathPrepare(test.path) | ||
if result != test.expected { | ||
t.Errorf("Expected %s, but got %s", test.expected, result) | ||
} else { | ||
t.Logf("Passed Test %s, Input %s, Expected %s, Result %s", test.name, test.path, test.expected, result) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.