summaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 48e6d5f7282e1028922e50c045ecf3cb6bb37513 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package config

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// Organization represents a git organization with its host and name
type Organization struct {
	Host           string `json:"host"`
	Name           string `json:"name"`
	GitHubToken    string `json:"github_token,omitempty"`
	CodebergToken  string `json:"codeberg_token,omitempty"`
	BackupLocation bool   `json:"backupLocation,omitempty"` // Mark this as a backup-only destination
}

// Config holds the application configuration
type Config struct {
	Organizations       []Organization `json:"organizations"`
	Repositories        []string       `json:"repositories,omitempty"`
	ExcludeBranches     []string       `json:"exclude_branches,omitempty"`      // Regex patterns for branches to exclude
	WorkDir             string         `json:"work_dir,omitempty"`              // Working directory for cloning repositories
	ExcludeFromShowcase []string       `json:"exclude_from_showcase,omitempty"` // Repository names to exclude from showcase
	// SkipReleases maps a repository name to a list of tag names for which
	// releases should NOT be created on any platform (GitHub/Codeberg)
	SkipReleases map[string][]string `json:"skip_releases,omitempty"`
}

// Load reads and parses the configuration file
func Load(path string) (*Config, error) {
	// If no path provided, use default
	if path == "" {
		home, err := os.UserHomeDir()
		if err != nil {
			return nil, fmt.Errorf("failed to get home directory: %w", err)
		}
		// Use XDG config directory with .json extension
		path = filepath.Join(home, ".config", "gitsyncer", "config.json")
	} else if len(path) >= 2 && path[:2] == "~/" {
		// Expand home directory if needed
		home, err := os.UserHomeDir()
		if err != nil {
			return nil, fmt.Errorf("failed to get home directory: %w", err)
		}
		path = filepath.Join(home, path[2:])
	}

	// Read config file
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("failed to read config file: %w", err)
	}

	// Parse JSON
	var cfg Config
	if err := json.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("failed to parse config: %w", err)
	}

	// Validate configuration
	if err := cfg.Validate(); err != nil {
		return nil, fmt.Errorf("invalid configuration: %w", err)
	}

	// Set default WorkDir if not specified
	if cfg.WorkDir == "" {
		home, err := os.UserHomeDir()
		if err != nil {
			return nil, fmt.Errorf("failed to get home directory: %w", err)
		}
		cfg.WorkDir = filepath.Join(home, "git", "gitsyncer-workdir")
	}

	// Expand home directory in WorkDir if needed
	if strings.HasPrefix(cfg.WorkDir, "~/") {
		home, err := os.UserHomeDir()
		if err != nil {
			return nil, fmt.Errorf("failed to get home directory: %w", err)
		}
		cfg.WorkDir = filepath.Join(home, cfg.WorkDir[2:])
	}

	return &cfg, nil
}

// Validate checks if the configuration is valid
func (c *Config) Validate() error {
	if len(c.Organizations) == 0 {
		return fmt.Errorf("no organizations configured")
	}

	for i, org := range c.Organizations {
		if org.Host == "" {
			return fmt.Errorf("organization %d: missing host", i)
		}
		// Name can be empty for file:// URLs or SSH backup locations
		if org.Name == "" && !strings.HasPrefix(org.Host, "file://") && !org.IsSSH() {
			return fmt.Errorf("organization %d: missing name", i)
		}
	}

	return nil
}

// ShouldSkipRelease returns true if the configuration specifies that
// the given repo/tag combination should not have a release created.
func (c *Config) ShouldSkipRelease(repo, tag string) bool {
	if c == nil || c.SkipReleases == nil {
		return false
	}
	tags, ok := c.SkipReleases[repo]
	if !ok {
		return false
	}
	for _, t := range tags {
		if t == tag {
			return true
		}
	}
	return false
}

// GetGitURL returns the git URL for an organization
func (o *Organization) GetGitURL() string {
	// For SSH backup locations with empty name, just return the host
	if o.IsSSH() && o.Name == "" {
		return o.Host
	}
	return fmt.Sprintf("%s:%s", o.Host, o.Name)
}

// FindOrganization finds an organization by host
func (c *Config) FindOrganization(host string) *Organization {
	for _, org := range c.Organizations {
		if org.Host == host {
			return &org
		}
	}
	return nil
}

// IsCodeberg checks if the organization is Codeberg
func (o *Organization) IsCodeberg() bool {
	return o.Host == "git@codeberg.org" || strings.Contains(o.Host, "codeberg.org")
}

// FindCodebergOrg finds the first Codeberg organization
func (c *Config) FindCodebergOrg() *Organization {
	for i := range c.Organizations {
		if c.Organizations[i].IsCodeberg() {
			return &c.Organizations[i]
		}
	}
	return nil
}

// IsGitHub checks if the organization is GitHub
func (o *Organization) IsGitHub() bool {
	return o.Host == "git@github.com" || strings.Contains(o.Host, "github.com")
}

// FindGitHubOrg finds the first GitHub organization
func (c *Config) FindGitHubOrg() *Organization {
	for i := range c.Organizations {
		if c.Organizations[i].IsGitHub() {
			return &c.Organizations[i]
		}
	}
	return nil
}

// IsSSH checks if the organization is a plain SSH location
func (o *Organization) IsSSH() bool {
	// Check if it's not a known git hosting service and contains SSH-like syntax
	return !o.IsGitHub() && !o.IsCodeberg() && !strings.HasPrefix(o.Host, "file://") &&
		(strings.Contains(o.Host, "@") || strings.Contains(o.Host, ":"))
}