aboutsummaryrefslogtreecommitdiff
path: root/configuration.go
blob: b00d7787746557bacba64c71814000febb4e6a6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
)

type Configuration struct {
	GroupSingleLineFunctions bool   `json:"group_single_line_functions"`
	CommentMode             string `json:"comment_mode"`
}

func (configuration Configuration) commentMode() (CommentMode, error) {
	switch strings.ToLower(configuration.CommentMode) {
	case "", "follow":
		return CommentsFollow, nil
	case "precede":
		return CommentsPrecede, nil
	case "standalone":
		return CommentsStandalone, nil
	default:
		return 0, fmt.Errorf("invalid comment_mode: %q (use follow, precede, or standalone)", configuration.CommentMode)
	}
}

func loadConfiguration() Configuration {
	var configuration Configuration

	for _, fileName := range []string{".iku.json", "iku.json"} {
		fileData, readError := os.ReadFile(fileName)

		if readError != nil {
			continue
		}

		_ = json.Unmarshal(fileData, &configuration)

		break
	}

	return configuration
}