about summary refs log tree commit diff stats
path: root/internal/config/fetcher.go
blob: fd95b3234cd00ef47330d50f44db115028497a28 (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
package config

import (
	"fmt"

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

type Fetcher int

const (
	UnknownFetcher = iota
	Channel
	ChannelNixpkgs
	Download
)

func (f Fetcher) String() string {
	switch f {
	case Channel:
		return "channel"
	case ChannelNixpkgs:
		return "channel-nixpkgs"
	case Download:
		return "download"
	}

	return fmt.Sprintf("Fetcher(%d)", f)
}

func ParseFetcher(name string) (Fetcher, error) {
	switch strcase.KebabCase(name) {
	case "channel":
		return Channel, nil
	case "channel-nixpkgs":
		return ChannelNixpkgs, nil
	case "download":
		return Download, nil
	default:
		return UnknownFetcher, fmt.Errorf("unsupported fetcher %s", name)
	}
}

func (f *Fetcher) UnmarshalText(text []byte) error {
	var err error
	*f, err = ParseFetcher(string(text))

	return err
}

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