a command line interface for elastic (work in progress)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.1 KiB

4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "fmt"
  4. toml "github.com/pelletier/go-toml"
  5. "io/ioutil"
  6. "os"
  7. )
  8. type ESConfiguration struct {
  9. Elastic ElasticConfiguration
  10. }
  11. type ElasticConfiguration struct {
  12. URL string
  13. User string
  14. Pass string
  15. }
  16. func LoadConfig(cfgpath string) *ESConfiguration {
  17. cfg := ESConfiguration{}
  18. if cfgpath != "" {
  19. cfgfile, err := ioutil.ReadFile(cfgpath)
  20. if err != nil {
  21. fmt.Fprintf(os.Stderr, "Read error: %s\n", err.Error())
  22. os.Exit(1)
  23. }
  24. err = toml.Unmarshal(cfgfile, &cfg)
  25. if err != nil {
  26. fmt.Fprintf(os.Stderr, "Parse error: %s\n", err.Error())
  27. os.Exit(1)
  28. }
  29. return &cfg
  30. }
  31. paths := []string{}
  32. homedir, _ := os.UserHomeDir()
  33. if homedir != "" {
  34. paths = append(paths, homedir+"/.es.toml")
  35. }
  36. paths = append(paths, "/etc/es.toml")
  37. for _, path := range paths {
  38. cfgfile, err := ioutil.ReadFile(path)
  39. if err != nil {
  40. continue
  41. }
  42. err = toml.Unmarshal(cfgfile, &cfg)
  43. if err != nil {
  44. continue
  45. }
  46. return &cfg
  47. }
  48. fmt.Fprintf(os.Stderr, "No valid configuration found in:\n")
  49. for _, path := range paths {
  50. fmt.Fprintf(os.Stderr, " %s\n", path)
  51. }
  52. os.Exit(1)
  53. return nil
  54. }