about summary refs log tree commit diff stats
path: root/internal/config/importer-type.go
blob: 6003d7dfd010bca298f00b5a6e0197d9f9b222b8 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package config

import (
	"fmt"

	"github.com/stoewer/go-strcase"
)

type ImporterType int

const (
	All = iota - 1
	UnknownType
	Packages
	Options
)

func (i ImporterType) String() string {
	switch i {
	case All:
		return "combined"
	case Packages:
		return "packages"
	case Options:
		return "options"
	}

	return fmt.Sprintf("Type(%d)", i)
}

func (i ImporterType) Singular() string {
	switch i {
	case All:
		return "combined"
	case Packages:
		return "package"
	case Options:
		return "option"
	}

	return fmt.Sprintf("Type(%d)", i)
}

func ParseImporterType(name string) (ImporterType, error) {
	switch strcase.KebabCase(name) {
	case "packages":
		return Packages, nil
	case "options":
		return Options, nil
	default:
		return UnknownType, fmt.Errorf("unsupported importer %s", name)
	}
}

func (i *ImporterType) UnmarshalText(text []byte) error {
	var err error
	*i, err = ParseImporterType(string(text))

	return err
}

func (i *ImporterType) MarshalText() ([]byte, error) {
	return []byte(i.String()), nil
}