Created
February 5, 2025 05:42
-
-
Save brettinternet/df89215a559c2eca112d8737d72f683d to your computer and use it in GitHub Desktop.
dump output as JSON to file for inspection
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
package logger | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"path" | |
"path/filepath" | |
) | |
func DumpFile(path string, data ...any) error { | |
dir := filepath.Dir(path) | |
if _, err := os.Stat(dir); err != nil { | |
return err | |
} | |
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
for _, d := range data { | |
s, err := json.MarshalIndent(d, "", "\t") | |
if err != nil { | |
return err | |
} | |
if _, err = fmt.Fprintf(f, "%+v\n", string(s)); err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func Dump(data any) error { | |
directory, err := currentExecutableDirectory() | |
if err != nil { | |
return err | |
} | |
p := path.Join(directory, "dump") | |
return DumpFile(p, data) | |
} | |
func currentExecutableDirectory() (string, error) { | |
ex, err := os.Executable() | |
if err != nil { | |
return "", err | |
} | |
return path.Dir(ex), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment