config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package config
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "gopkg.in/yaml.v3"
  8. )
  9. // Context maintains configuration for interaction with Netmaker API
  10. type Context struct {
  11. Endpoint string `yaml:"endpoint"`
  12. Username string `yaml:"username,omitempty"`
  13. Password string `yaml:"password,omitempty"`
  14. MasterKey string `yaml:"masterkey,omitempty"`
  15. Current bool `yaml:"current,omitempty"`
  16. AuthToken string `yaml:"auth_token,omitempty"`
  17. }
  18. var (
  19. contextMap = map[string]Context{}
  20. configFilePath string
  21. filename string
  22. )
  23. func createConfigPathIfNotExists() {
  24. homeDir, err := os.UserHomeDir()
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. configFilePath = filepath.Join(homeDir, ".netmaker")
  29. // create directory if not exists
  30. if err := os.MkdirAll(configFilePath, os.ModePerm); err != nil {
  31. log.Fatal(err)
  32. }
  33. filename = filepath.Join(configFilePath, "config.yml")
  34. // create file if not exists
  35. if _, err := os.Stat(filename); err != nil {
  36. if os.IsNotExist(err) {
  37. if _, err := os.Create(filename); err != nil {
  38. log.Fatalf("Unable to create file filename: %s", err)
  39. }
  40. } else {
  41. log.Fatal(err)
  42. }
  43. }
  44. }
  45. func loadConfig() {
  46. content, err := os.ReadFile(filename)
  47. if err != nil {
  48. log.Fatalf("Error reading config file: %s", err)
  49. }
  50. if err := yaml.Unmarshal(content, &contextMap); err != nil {
  51. log.Fatalf("Unable to decode YAML into struct: %s", err)
  52. }
  53. }
  54. func saveContext() {
  55. bodyBytes, err := yaml.Marshal(&contextMap)
  56. if err != nil {
  57. log.Fatalf("Error marshalling into YAML %s", err)
  58. }
  59. file, err := os.Create(filename)
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. if _, err := file.Write(bodyBytes); err != nil {
  64. log.Fatal(err)
  65. }
  66. if err := file.Close(); err != nil {
  67. log.Fatal(err)
  68. }
  69. }
  70. // GetCurrentContext - returns current set context
  71. func GetCurrentContext() (name string, ctx Context) {
  72. for n, c := range contextMap {
  73. if c.Current {
  74. name, ctx = n, c
  75. return
  76. }
  77. }
  78. log.Fatalf("No current context set, do so via `netmaker context use <name>`")
  79. return
  80. }
  81. // SetCurrentContext - sets a given context as current context
  82. func SetCurrentContext(ctxName string) {
  83. if _, ok := contextMap[ctxName]; !ok {
  84. log.Fatalf("No such context %s", ctxName)
  85. }
  86. for key, ctx := range contextMap {
  87. ctx.Current = key == ctxName
  88. contextMap[key] = ctx
  89. }
  90. saveContext()
  91. }
  92. // SetContext - updates an existing context or creates a new one
  93. func SetContext(ctxName string, ctx Context) {
  94. if oldCtx, ok := contextMap[ctxName]; ok && oldCtx.Current {
  95. ctx.Current = true
  96. }
  97. contextMap[ctxName] = ctx
  98. saveContext()
  99. }
  100. // SetAuthToken - saves the auth token
  101. func SetAuthToken(authToken string) {
  102. ctxName, _ := GetCurrentContext()
  103. if ctx, ok := contextMap[ctxName]; ok {
  104. ctx.AuthToken = authToken
  105. contextMap[ctxName] = ctx
  106. saveContext()
  107. }
  108. }
  109. // DeleteContext - deletes a context
  110. func DeleteContext(ctxName string) {
  111. if _, ok := contextMap[ctxName]; ok {
  112. delete(contextMap, ctxName)
  113. saveContext()
  114. } else {
  115. log.Fatalf("No such context %s", ctxName)
  116. }
  117. }
  118. // ListAll - lists all contexts
  119. func ListAll() {
  120. for key, ctx := range contextMap {
  121. fmt.Print("\n", key, " -> ", ctx.Endpoint)
  122. if ctx.Current {
  123. fmt.Print(" (current)")
  124. }
  125. }
  126. }
  127. func init() {
  128. createConfigPathIfNotExists()
  129. loadConfig()
  130. }