MCPcopy Create free account
hub / github.com/amitsaha/gitbackup / handleValidateConfig

Function handleValidateConfig

config_file.go:167–237  ·  view source on GitHub ↗

handleValidateConfig reads the config file and validates its contents. If configPath is empty, the OS-specific default location is used.

(configPath string)

Source from the content-addressed store, hash-verified

165// handleValidateConfig reads the config file and validates its contents.
166// If configPath is empty, the OS-specific default location is used.
167func handleValidateConfig(configPath string) error {
168 path, err := resolveConfigPath(configPath)
169 if err != nil {
170 return err
171 }
172
173 cfg, err := loadConfigFile(configPath)
174 if err != nil {
175 return err
176 }
177
178 var errors []string
179
180 // Validate service
181 if _, ok := knownServices[cfg.Service]; !ok {
182 errors = append(errors, fmt.Sprintf("invalid service: %q (must be github, gitlab, bitbucket, or forgejo)", cfg.Service))
183 }
184
185 // Validate service-specific field values
186 switch cfg.Service {
187 case "github":
188 if !contains([]string{"all", "owner", "member", "starred"}, cfg.GitHub.RepoType) {
189 errors = append(errors, fmt.Sprintf("invalid github.repo_type: %q (must be all, owner, member, or starred)", cfg.GitHub.RepoType))
190 }
191 case "gitlab":
192 if !contains([]string{"internal", "public", "private"}, cfg.GitLab.ProjectVisibility) {
193 errors = append(errors, fmt.Sprintf("invalid gitlab.project_visibility: %q (must be internal, public, or private)", cfg.GitLab.ProjectVisibility))
194 }
195 if !validGitlabProjectMembership(cfg.GitLab.ProjectMembershipType) {
196 errors = append(errors, fmt.Sprintf("invalid gitlab.project_membership_type: %q (must be all, owner, member, or starred)", cfg.GitLab.ProjectMembershipType))
197 }
198 case "forgejo":
199 if !contains([]string{"user", "starred"}, cfg.Forgejo.RepoType) {
200 errors = append(errors, fmt.Sprintf("invalid forgejo.repo_type: %q (must be user or starred)", cfg.Forgejo.RepoType))
201 }
202 }
203
204 // Validate required environment variables
205 switch cfg.Service {
206 case "github":
207 if os.Getenv("GITHUB_TOKEN") == "" {
208 errors = append(errors, "GITHUB_TOKEN environment variable not set")
209 }
210 case "gitlab":
211 if os.Getenv("GITLAB_TOKEN") == "" {
212 errors = append(errors, "GITLAB_TOKEN environment variable not set")
213 }
214 case "bitbucket":
215 if os.Getenv("BITBUCKET_USERNAME") == "" {
216 errors = append(errors, "BITBUCKET_USERNAME environment variable not set")
217 }
218 if os.Getenv("BITBUCKET_TOKEN") == "" && os.Getenv("BITBUCKET_PASSWORD") == "" {
219 errors = append(errors, "BITBUCKET_TOKEN or BITBUCKET_PASSWORD environment variable must be set")
220 }
221 case "forgejo":
222 if os.Getenv("FORGEJO_TOKEN") == "" {
223 errors = append(errors, "FORGEJO_TOKEN environment variable not set")
224 }

Calls 4

resolveConfigPathFunction · 0.85
loadConfigFileFunction · 0.85
containsFunction · 0.85