hostname.go 547 B

1234567891011121314151617181920212223242526272829
  1. package devnet
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. )
  8. // GetDevnetIndex returns the current host's devnet index (i.e. 0 for guardian-0).
  9. func GetDevnetIndex() (int, error) {
  10. hostname, err := os.Hostname()
  11. if err != nil {
  12. panic(err)
  13. }
  14. h := strings.Split(hostname, "-")
  15. if h[0] != "guardian" {
  16. return 0, fmt.Errorf("hostname %s does not appear to be a devnet host", hostname)
  17. }
  18. i, err := strconv.Atoi(h[1])
  19. if err != nil {
  20. return 0, fmt.Errorf("invalid devnet index %s in hostname %s", h[1], hostname)
  21. }
  22. return i, nil
  23. }