All checks were successful
go-generate-check / go-generate-check (pull_request) Successful in 8s
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"slices"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
)
|
|
|
|
func WriteNewMaintainershipFile(m *common.MaintainershipMap, filename string) {
|
|
f, err := os.Create(filename + ".new")
|
|
common.PanicOnError(err)
|
|
common.PanicOnError(m.WriteMaintainershipFile(f))
|
|
common.PanicOnError(f.Sync())
|
|
common.PanicOnError(f.Close())
|
|
common.PanicOnError(os.Rename(filename+".new", filename))
|
|
}
|
|
|
|
func run() error {
|
|
pkg := flag.String("package", "", "Package to modify")
|
|
rm := flag.Bool("rm", false, "Remove maintainer from package")
|
|
add := flag.Bool("add", false, "Add maintainer to package")
|
|
lint := flag.Bool("lint-only", false, "Reformat entire _maintainership.json only")
|
|
flag.Parse()
|
|
|
|
if (*add == *rm) && !*lint {
|
|
return fmt.Errorf("Need to either add or remove a maintainer, or lint")
|
|
}
|
|
|
|
filename := common.MaintainershipFile
|
|
if *lint {
|
|
if len(flag.Args()) > 0 {
|
|
filename = flag.Arg(0)
|
|
}
|
|
}
|
|
|
|
data, err := os.ReadFile(filename)
|
|
if os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m, err := common.ParseMaintainershipData(data)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to parse JSON: %w", err)
|
|
}
|
|
|
|
if *lint {
|
|
m.Raw = nil // forces a rewrite
|
|
} else {
|
|
users := flag.Args()
|
|
if len(users) > 0 {
|
|
maintainers, ok := m.Data[*pkg]
|
|
if !ok && !*add {
|
|
return fmt.Errorf("No package %s and not adding one.", *pkg)
|
|
}
|
|
|
|
if *add {
|
|
for _, u := range users {
|
|
if !slices.Contains(maintainers, u) {
|
|
maintainers = append(maintainers, u)
|
|
}
|
|
}
|
|
}
|
|
|
|
if *rm {
|
|
newMaintainers := make([]string, 0, len(maintainers))
|
|
for _, m := range maintainers {
|
|
if !slices.Contains(users, m) {
|
|
newMaintainers = append(newMaintainers, m)
|
|
}
|
|
}
|
|
maintainers = newMaintainers
|
|
}
|
|
|
|
if len(maintainers) > 0 {
|
|
slices.Sort(maintainers)
|
|
m.Data[*pkg] = maintainers
|
|
} else {
|
|
delete(m.Data, *pkg)
|
|
}
|
|
}
|
|
}
|
|
|
|
WriteNewMaintainershipFile(m, filename)
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
common.LogError(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|