vanity/service/service.go

88 lines
1.8 KiB
Go

package service
import (
"fmt"
"strings"
"go.jolheiser.com/vanity/flags"
)
type Package struct {
Name string
Description string
Branch string
URL string
HTTP string
SSH string
private bool
fork bool
mirror bool
archive bool
}
func (p *Package) Module() string {
return fmt.Sprintf("%s/%s", flags.Config.Domain, strings.ToLower(p.Name))
}
type Service interface {
Packages() (map[string]*Package, error)
GoDir(*Package) string
GoFile(*Package) string
GoMod(*Package) bool
}
func New() Service {
switch strings.ToLower(flags.Config.Service) {
case "gitea":
return NewGitea()
case "github":
return NewGitHub()
case "gitlab":
return NewGitLab()
}
return nil
}
func Check(pkg *Package) error {
// Private
if pkg.private && !flags.Config.Private {
return fmt.Errorf("%s is private and --private wasn't used", pkg.Name)
}
// Forked
if pkg.fork && !flags.Config.Fork {
return fmt.Errorf("%s is a fork and --fork wasn't used", pkg.Name)
}
// Mirrored
if pkg.mirror && !flags.Config.Mirror {
return fmt.Errorf("%s is a mirror and --mirror wasn't used", pkg.Name)
}
// Archived
if pkg.archive && !flags.Config.Archive {
return fmt.Errorf("%s is archived and --archive wasn't used", pkg.Name)
}
// Exclusions
for _, exclude := range flags.Config.Exclude {
if exclude.MatchString(pkg.Name) {
return fmt.Errorf("%s is was excluded by the rule %s", pkg.Name, exclude.String())
}
}
// Inclusions
if len(flags.Config.Include) > 0 {
for _, include := range flags.Config.Include {
if include.MatchString(pkg.Name) {
return fmt.Errorf("%s is was included by the rule %s", pkg.Name, include.String())
}
}
return fmt.Errorf("%s is wasn't included by any existing inclusion rule", pkg.Name)
}
return nil
}