(stderr io.Writer, dir, filename string)
| 65 | } |
| 66 | |
| 67 | func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config, error) { |
| 68 | configPath := "" |
| 69 | if filename != "" { |
| 70 | configPath = filepath.Join(dir, filename) |
| 71 | } else { |
| 72 | var yamlMissing, jsonMissing, ymlMissing bool |
| 73 | yamlPath := filepath.Join(dir, "sqlc.yaml") |
| 74 | ymlPath := filepath.Join(dir, "sqlc.yml") |
| 75 | jsonPath := filepath.Join(dir, "sqlc.json") |
| 76 | |
| 77 | if _, err := os.Stat(yamlPath); os.IsNotExist(err) { |
| 78 | yamlMissing = true |
| 79 | } |
| 80 | if _, err := os.Stat(jsonPath); os.IsNotExist(err) { |
| 81 | jsonMissing = true |
| 82 | } |
| 83 | |
| 84 | if _, err := os.Stat(ymlPath); os.IsNotExist(err) { |
| 85 | ymlMissing = true |
| 86 | } |
| 87 | |
| 88 | if yamlMissing && ymlMissing && jsonMissing { |
| 89 | fmt.Fprintln(stderr, "error parsing configuration files. sqlc.(yaml|yml) or sqlc.json: file does not exist") |
| 90 | return "", nil, errors.New("config file missing") |
| 91 | } |
| 92 | |
| 93 | if (!yamlMissing || !ymlMissing) && !jsonMissing { |
| 94 | fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.(yaml|yml) files present") |
| 95 | return "", nil, errors.New("sqlc.json and sqlc.(yaml|yml) present") |
| 96 | } |
| 97 | |
| 98 | if jsonMissing { |
| 99 | if yamlMissing { |
| 100 | configPath = ymlPath |
| 101 | } else { |
| 102 | configPath = yamlPath |
| 103 | } |
| 104 | } else { |
| 105 | configPath = jsonPath |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | base := filepath.Base(configPath) |
| 110 | file, err := os.Open(configPath) |
| 111 | if err != nil { |
| 112 | fmt.Fprintf(stderr, "error parsing %s: file does not exist\n", base) |
| 113 | return "", nil, err |
| 114 | } |
| 115 | defer file.Close() |
| 116 | |
| 117 | conf, err := config.ParseConfig(file) |
| 118 | if err != nil { |
| 119 | switch err { |
| 120 | case config.ErrMissingVersion: |
| 121 | fmt.Fprint(stderr, errMessageNoVersion) |
| 122 | case config.ErrUnknownVersion: |
| 123 | fmt.Fprint(stderr, errMessageUnknownVersion) |
| 124 | case config.ErrNoPackages: |
no test coverage detected