config.go 3.3 KB

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