nak/helpers.go

52 lines
953 B
Go
Raw Normal View History

2023-10-20 19:57:29 -04:00
package main
import (
"bytes"
2023-10-20 20:01:11 -04:00
"fmt"
2023-10-20 19:57:29 -04:00
"io"
2023-10-20 20:01:11 -04:00
"net/url"
2023-10-20 19:57:29 -04:00
"os"
2023-10-23 07:04:28 -04:00
"strings"
2023-10-20 19:57:29 -04:00
"github.com/urfave/cli/v2"
)
func getStdin() string {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
read := bytes.NewBuffer(make([]byte, 0, 1000))
_, err := io.Copy(read, os.Stdin)
if err == nil {
2023-10-23 07:04:28 -04:00
return strings.TrimSpace(read.String())
2023-10-20 19:57:29 -04:00
}
}
return ""
}
func getStdinOrFirstArgument(c *cli.Context) string {
target := c.Args().First()
if target != "" {
return target
}
return getStdin()
}
2023-10-20 20:01:11 -04:00
func validateRelayURLs(wsurls []string) error {
for _, wsurl := range wsurls {
u, err := url.Parse(wsurl)
if err != nil {
return fmt.Errorf("invalid relay url '%s': %s", wsurl, err)
}
if u.Scheme != "ws" && u.Scheme != "wss" {
return fmt.Errorf("relay url must use wss:// or ws:// schemes, got '%s'", wsurl)
}
if u.Host == "" {
return fmt.Errorf("relay url '%s' is missing the hostname", wsurl)
}
}
return nil
}