Dan Anglin
dd6c21afe8
All checks were successful
REUSE Compliance Check / check (push) Successful in 6s
- Created generic Error types to remove the need to import the errors package. - Used the generic Error types in place of the single use custom Error types that have no fields. - Created new Error types where necessary. PR: #50
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package utilities
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
func GetFQDN(url string) string {
|
|
r := regexp.MustCompile(`http(s)?:\/\/`)
|
|
|
|
return r.ReplaceAllString(url, "")
|
|
}
|
|
|
|
type UnspecifiedProgramError struct{}
|
|
|
|
func (e UnspecifiedProgramError) Error() string {
|
|
return "the program to view these files is unspecified"
|
|
}
|
|
|
|
func OpenMedia(viewer string, paths []string) error {
|
|
if viewer == "" {
|
|
return UnspecifiedProgramError{}
|
|
}
|
|
|
|
cmd := slices.Concat(strings.Split(viewer, " "), paths)
|
|
|
|
command := exec.Command(cmd[0], cmd[1:]...) //nolint:gosec
|
|
|
|
if err := command.Start(); err != nil {
|
|
return fmt.Errorf("received an error after starting the program: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type UnspecifiedBrowserError struct{}
|
|
|
|
func (e UnspecifiedBrowserError) Error() string {
|
|
return "the browser to view this link is not specified"
|
|
}
|
|
|
|
func OpenLink(browser, url string) error {
|
|
if browser == "" {
|
|
return UnspecifiedBrowserError{}
|
|
}
|
|
|
|
cmd := strings.Split(browser, " ")
|
|
cmd = append(cmd, url)
|
|
|
|
command := exec.Command(cmd[0], cmd[1:]...) //nolint:gosec
|
|
|
|
if err := command.Start(); err != nil {
|
|
return fmt.Errorf("received an error after starting the program to view the link: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|