main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. ini "gopkg.in/ini.v1"
  8. "io/ioutil"
  9. "os"
  10. "reflect"
  11. "etcd/common.in/clinit"
  12. "etcd/common.in/config"
  13. client "go.etcd.io/etcd/client"
  14. "strings"
  15. )
  16. var (
  17. configFile = flag.String("config", "./config", "config file location")
  18. set string
  19. )
  20. func PathExists(path string) bool {
  21. _, err := os.Stat(path)
  22. if err == nil {
  23. return true
  24. }
  25. return false
  26. }
  27. func Usage() {
  28. fmt.Println("etcd:")
  29. fmt.Println("subcommand:")
  30. fmt.Println(" set-from-file filename")
  31. fmt.Println(" set key value")
  32. fmt.Println(" get [key]")
  33. os.Exit(1)
  34. }
  35. func setKey(key, value, encryptKey string) {
  36. fmt.Println(key," ",value)
  37. if set == "no"{
  38. return
  39. }
  40. keyApi := client.NewKeysAPI(clinit.GetEtcdClient())
  41. opts := &client.SetOptions{PrevExist: client.PrevIgnore}
  42. if encryptKey != "" {
  43. aesString, err := config.AesEncrypt(value, encryptKey)
  44. if err != nil {
  45. fmt.Printf("AesEncrypt failed. error:%s", err)
  46. os.Exit(1)
  47. }
  48. baseString := config.Base64UrlSafeEncode([]byte(aesString))
  49. resp, err := keyApi.Set(context.Background(), key, baseString, opts)
  50. if err != nil {
  51. fmt.Printf("set etcd failed. response :%s ,error:%s", resp, err)
  52. os.Exit(1)
  53. }
  54. } else {
  55. resp, err := keyApi.Set(context.Background(), key, value, opts)
  56. if err != nil {
  57. fmt.Printf("set etcd failed. response :%s ,error:%s", resp, err)
  58. os.Exit(1)
  59. }
  60. }
  61. }
  62. func SetLine(strList []string, encryptKey string) {
  63. for _, value := range strList {
  64. if len(value) == 0 || value[0] == '#' || len(strings.Fields(value)) != 2 {
  65. continue
  66. }
  67. str := strings.Fields(value)
  68. setKey(str[0], str[1], encryptKey)
  69. }
  70. }
  71. func setData(basePath string,confData map[string] interface{},encryptKey string){
  72. for k,v := range confData{
  73. key := fmt.Sprintf("%s/%s",basePath,k)
  74. if reflect.ValueOf(v).Kind() == reflect.Map{
  75. setData(key,v.(map[string]interface{}),encryptKey)
  76. }else if reflect.ValueOf(v).Kind() == reflect.String{
  77. setKey(key, v.(string), encryptKey)
  78. }else{
  79. fmt.Println(key,":is not map or string")
  80. }
  81. }
  82. }
  83. func SetLineNew(basePath string,data []byte, encryptKey string) {
  84. confData:= make(map[string] interface{})
  85. e:= json.Unmarshal(data,&confData)
  86. if e!= nil {
  87. panic(e)
  88. }
  89. setData(basePath,confData,encryptKey)
  90. }
  91. func getNodeData(key string, head *client.Node) (value interface{}) {
  92. s0 := strings.Split(head.Key, "/")
  93. len0 := len(s0)
  94. if len0 == 0 {
  95. return
  96. }
  97. if head.Dir {
  98. mapData := map[string]interface{}{}
  99. for _, node := range head.Nodes {
  100. s1 := strings.Split(node.Key, "/")
  101. len1 := len(s1)
  102. if len1 == 0 {
  103. break
  104. }
  105. mapData[s1[len1-1]] = getNodeData(key, node)
  106. }
  107. value = mapData
  108. } else {
  109. if key != "" && head.Value != "" {
  110. if bytesData, err := config.Base64URLDecode(head.Value); err != nil {
  111. fmt.Printf("Base64URLDecode(%s) failed. error:%s", head.Value, err)
  112. os.Exit(1)
  113. } else {
  114. if data, err := config.AesDecrypt(bytesData, []byte(key)); err != nil {
  115. fmt.Printf("AesDecrypt failed. error:%s", err)
  116. os.Exit(1)
  117. } else {
  118. value = string(data)
  119. }
  120. }
  121. } else {
  122. value = head.Value
  123. }
  124. fmt.Println(head.Key, value)
  125. }
  126. return
  127. }
  128. func GetConfig(runmode, key string, cli client.Client) {
  129. keysAPI := client.NewKeysAPI(cli)
  130. basePath := fmt.Sprintf("/%s/config", runmode)
  131. if resp, err := keysAPI.Get(context.Background(), basePath, &client.GetOptions{
  132. Recursive: true,
  133. }); err == nil && resp != nil && resp.Node != nil {
  134. getNodeData(key, resp.Node)
  135. } else {
  136. fmt.Printf("get %s failed. error:%s", basePath, err)
  137. os.Exit(1)
  138. }
  139. return
  140. }
  141. func main() {
  142. flag.Parse()
  143. cfg, err := ini.Load("./app.conf")
  144. if err != nil {
  145. fmt.Printf("Fail to read file: %v\n\n", err)
  146. os.Exit(1)
  147. }
  148. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  149. runmode := cfg.Section("").Key("runmode").String()
  150. encryptKey := cfg.Section("").Key("encrypt_key").String()
  151. projectName := cfg.Section("").Key("project_name").String()
  152. set = cfg.Section("").Key("set").String()
  153. clinit.InitEtcd(etcdAddrs)
  154. if len(os.Args) >= 2 {
  155. if os.Args[1] == "set-from-file" && len(os.Args) == 3 {
  156. isExist := PathExists(os.Args[2])
  157. if !isExist {
  158. Usage()
  159. os.Exit(1)
  160. }
  161. b, err := ioutil.ReadFile(os.Args[2])
  162. if err != nil {
  163. fmt.Print(err)
  164. os.Exit(1)
  165. }
  166. basePath := fmt.Sprintf("/%s/%s/config",projectName,runmode)
  167. SetLineNew(basePath,b, encryptKey)
  168. //SetLine(strings.Split(string(b), "\n"), encryptKey)
  169. } else if os.Args[1] == "set" {
  170. if len(os.Args) != 4 {
  171. Usage()
  172. os.Exit(1)
  173. }
  174. setKey(os.Args[2], os.Args[3], encryptKey)
  175. } else if os.Args[1] == "get" {
  176. opts := &client.GetOptions{Recursive: true}
  177. if len(os.Args) == 3 {
  178. keyApi := client.NewKeysAPI(clinit.GetEtcdClient())
  179. resp, err := keyApi.Get(context.Background(), os.Args[2], opts)
  180. if err == nil && resp != nil && resp.Node != nil {
  181. getNodeData(encryptKey, resp.Node)
  182. } else {
  183. fmt.Printf("get %s failed. error:%s", os.Args[2], err)
  184. os.Exit(1)
  185. }
  186. } else if len(os.Args) == 2 {
  187. GetConfig(runmode, encryptKey, clinit.GetEtcdClient())
  188. } else {
  189. Usage()
  190. os.Exit(1)
  191. }
  192. } else {
  193. fmt.Println("not support subcommand: " + os.Args[1])
  194. Usage()
  195. }
  196. } else {
  197. Usage()
  198. }
  199. }