common.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "os/exec"
  20. "os/signal"
  21. "strings"
  22. "syscall"
  23. "github.com/spf13/cobra"
  24. )
  25. func printDNSSuffixList(cmd *cobra.Command, args []string) {
  26. dnsSuffixList := getDNSSuffixList()
  27. fmt.Println(strings.Join(dnsSuffixList, ","))
  28. }
  29. func printDNSServerList(cmd *cobra.Command, args []string) {
  30. dnsServerList := getDNSServerList()
  31. fmt.Println(strings.Join(dnsServerList, ","))
  32. }
  33. func printHostsFile(cmd *cobra.Command, args []string) {
  34. fmt.Println(readFile(etcHostsFile))
  35. }
  36. func pause(cmd *cobra.Command, args []string) {
  37. sigCh := make(chan os.Signal)
  38. done := make(chan int, 1)
  39. signal.Notify(sigCh, syscall.SIGINT)
  40. signal.Notify(sigCh, syscall.SIGTERM)
  41. signal.Notify(sigCh, syscall.SIGKILL)
  42. go func() {
  43. sig := <-sigCh
  44. switch sig {
  45. case syscall.SIGINT:
  46. done <- 1
  47. os.Exit(1)
  48. case syscall.SIGTERM:
  49. done <- 2
  50. os.Exit(2)
  51. case syscall.SIGKILL:
  52. done <- 0
  53. os.Exit(0)
  54. }
  55. }()
  56. result := <-done
  57. fmt.Printf("exiting %d\n", result)
  58. }
  59. func readFile(fileName string) string {
  60. fileData, err := ioutil.ReadFile(fileName)
  61. if err != nil {
  62. panic(err)
  63. }
  64. return string(fileData)
  65. }
  66. func runCommand(name string, arg ...string) string {
  67. var out bytes.Buffer
  68. cmd := exec.Command(name, arg...)
  69. cmd.Stdout = &out
  70. err := cmd.Run()
  71. if err != nil {
  72. panic(err)
  73. }
  74. return strings.TrimSpace(out.String())
  75. }