badger/internal/executables/third_party.go
2023-03-14 09:53:33 +01:00

42 lines
1.1 KiB
Go

package executables
import (
"embed"
"os"
"path"
"velvettear/badger/internal/config"
"velvettear/badger/internal/log"
"velvettear/badger/internal/tools"
)
func exportThirdPartyExecutable(embeddedFS *embed.FS, name string) (Executable, error) {
timestamp := tools.LogTimestamp()
executable := Executable{
Name: name,
Path: path.Join(config.HomeDirectory(), "third_party", name),
}
var msg string
if tools.Exists(executable.Path) {
msg = "binary '" + executable.Path + "' already exists, skipping export"
} else {
file, error := embeddedFS.ReadFile(path.Join("third_party", name))
if error != nil {
return Executable{}, error
}
error = tools.MkDirs(path.Dir(executable.Path))
if error != nil {
return Executable{}, error
}
error = os.WriteFile(executable.Path, file, os.ModeAppend)
if error != nil {
return Executable{}, error
}
msg = "exported binary '" + executable.Path + "'"
}
error := os.Chmod(executable.Path, 0700)
if error != nil {
return Executable{}, error
}
log.DebugTimed(msg, timestamp)
return executable, nil
}