aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/config.go
blob: c91ba58051f5ee90d9d5631407e309073de19bc4 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package filesystem

import (
	"fmt"
	"io"

	"gopkg.in/gcfg.v1"
	"gopkg.in/src-d/go-git.v4/config"
	"gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit"
)

type ConfigStorage struct {
	dir *dotgit.DotGit
}

func (c *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) {
	file, err := c.read()
	if err != nil {
		return nil, err
	}

	r, ok := file.Remotes[name]
	if ok {
		return r, nil
	}

	return nil, config.ErrRemoteConfigNotFound
}

func (c *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) {
	file, err := c.read()
	if err != nil {
		return nil, err
	}

	remotes := make([]*config.RemoteConfig, len(file.Remotes))

	var i int
	for _, r := range file.Remotes {
		remotes[i] = r
	}

	return remotes, nil
}

func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error {
	return fmt.Errorf("not implemented yet")
}

func (c *ConfigStorage) DeleteRemote(name string) error {
	return fmt.Errorf("not implemented yet")
}

func (c *ConfigStorage) read() (*ConfigFile, error) {
	f, err := c.dir.Config()
	if err != nil {
		return nil, err
	}

	defer f.Close()

	config := &ConfigFile{}
	return config, config.Decode(f)
}

type ConfigFile struct {
	Remotes map[string]*config.RemoteConfig `gcfg:"remote"`
}

// Decode decode a git config file intro the ConfigStore
func (c *ConfigFile) Decode(r io.Reader) error {
	return gcfg.FatalOnly(gcfg.ReadInto(c, r))
}