summaryrefslogtreecommitdiffstats
path: root/internal/config/config.go
blob: 8651aa826ee6e07de8e1a06d0c35b91c86d7e08b (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 (
	"bufio"
	"os"
	"path/filepath"
	"strings"
)

// Config represents the user's configuration
type Config struct {
	Host string
}

// Load reads config from ~/.config/deploy/config
func Load() (*Config, error) {
	path := configPath()

	// If file doesn't exist, return empty config
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return &Config{}, nil
	}

	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	cfg := &Config{}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if line == "" || strings.HasPrefix(line, "#") {
			continue
		}

		parts := strings.SplitN(line, ":", 2)
		if len(parts) != 2 {
			continue
		}

		key := strings.TrimSpace(parts[0])
		value := strings.TrimSpace(parts[1])

		switch key {
		case "host":
			cfg.Host = value
		}
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return cfg, nil
}

func configPath() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return ".deploy-config"
	}
	return filepath.Join(home, ".config", "deploy", "config")
}