config.go 3.2 KB

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