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

import (
	"fmt"
	"time"

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

type Type int

const (
	Unknown = iota
	Channel
	ChannelNixpkgs
	DownloadOptions
)

func (f Type) String() string {
	switch f {
	case Channel:
		return "channel"
	case ChannelNixpkgs:
		return "channel-nixpkgs"
	case DownloadOptions:
		return "download-options"
	}

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

func parseType(name string) (Type, error) {
	switch strcase.KebabCase(name) {
	case "channel":
		return Channel, nil
	case "channel-nixpkgs":
		return ChannelNixpkgs, nil
	case "download-options":
		return DownloadOptions, nil
	default:
		return Unknown, fmt.Errorf("unsupported fetcher %s", name)
	}
}

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

	return err
}

type Source struct {
	Name          string
	Key           string
	Enable        bool
	Type          Type
	Channel       string
	URL           string
	Attribute     string
	ImportPath    string        `toml:"import-path"`
	FetchTimeout  time.Duration `toml:"fetch-timeout"`
	ImportTimeout time.Duration `toml:"import-timeout"`
	OutputPath    string        `toml:"output-path"`
	Repo          Repository
}