addroot.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package cli
  14. import (
  15. "encoding/base64"
  16. "fmt"
  17. "io/ioutil"
  18. "net/url"
  19. "os"
  20. "strings"
  21. "zerotier/pkg/zerotier"
  22. )
  23. // AddRoot CLI command
  24. func AddRoot(basePath, authToken string, args []string) {
  25. if len(args) == 0 {
  26. Help()
  27. os.Exit(0)
  28. }
  29. locData, err := ioutil.ReadFile(args[0])
  30. if err != nil {
  31. locData, err2 := base64.StdEncoding.DecodeString(strings.TrimSpace(args[0]))
  32. if err2 != nil || len(locData) == 0 {
  33. fmt.Printf("ERROR: unable to read locator: %s\n", err.Error())
  34. os.Exit(1)
  35. }
  36. }
  37. loc, err := zerotier.NewLocatorFromBytes(locData)
  38. if err != nil {
  39. fmt.Printf("ERROR: invalid locator '%s' (tried as file and base64 literal): %s\n", args[0], err.Error())
  40. os.Exit(1)
  41. }
  42. var name string
  43. if len(args) > 1 {
  44. if len(args) > 2 {
  45. Help()
  46. os.Exit(1)
  47. }
  48. name = strings.TrimSpace(args[1])
  49. }
  50. var result zerotier.Root
  51. apiPost(basePath, authToken, "/root/"+url.PathEscape(name), &zerotier.Root{
  52. Name: name,
  53. Locator: loc,
  54. }, &result)
  55. fmt.Println(jsonDump(&result))
  56. os.Exit(0)
  57. }