config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://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 scaleway
  14. import (
  15. "fmt"
  16. "os"
  17. )
  18. type config struct {
  19. apiUrl *string
  20. region string
  21. projectId string
  22. accessKey string
  23. secretKey string
  24. }
  25. func loadConfigFromEnv() (*config, error) {
  26. var cfg config
  27. var err error
  28. if apiUrl, ok := os.LookupEnv("SCALEWAY_API_URL"); ok {
  29. cfg.apiUrl = &apiUrl
  30. }
  31. cfg.region, err = getEnv("SCALEWAY_REGION")
  32. if err != nil {
  33. return nil, err
  34. }
  35. cfg.projectId, err = getEnv("SCALEWAY_PROJECT_ID")
  36. if err != nil {
  37. return nil, err
  38. }
  39. cfg.accessKey, err = getEnv("SCALEWAY_ACCESS_KEY")
  40. if err != nil {
  41. return nil, err
  42. }
  43. cfg.secretKey, err = getEnv("SCALEWAY_SECRET_KEY")
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &cfg, nil
  48. }
  49. func getEnv(name string) (string, error) {
  50. value, ok := os.LookupEnv(name)
  51. if !ok {
  52. return "", fmt.Errorf("environment variable %q is not set", name)
  53. }
  54. return value, nil
  55. }