mirror of
https://github.com/Quiq/docker-registry-ui.git
synced 2025-07-16 23:31:16 +00:00
Implement purge_tags_keep_from_file option. Refactoring.
This commit is contained in:
parent
6d0fc3d913
commit
b7c5c52fa2
15
config.go
15
config.go
@ -7,7 +7,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/quiq/docker-registry-ui/registry"
|
||||
"github.com/tidwall/gjson"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
@ -31,7 +30,7 @@ type configData struct {
|
||||
PurgeTagsKeepDays int `yaml:"purge_tags_keep_days"`
|
||||
PurgeTagsKeepCount int `yaml:"purge_tags_keep_count"`
|
||||
PurgeTagsKeepRegexp string `yaml:"purge_tags_keep_regexp"`
|
||||
PurgeTagsKeepFile string `yaml:"purge_tags_keep_from_file"`
|
||||
PurgeTagsKeepFromFile string `yaml:"purge_tags_keep_from_file"`
|
||||
PurgeTagsSchedule string `yaml:"purge_tags_schedule"`
|
||||
|
||||
PurgeConfig *registry.PurgeTagsConfig
|
||||
@ -77,17 +76,7 @@ func readConfig(configFile string) *configData {
|
||||
KeepDays: config.PurgeTagsKeepDays,
|
||||
KeepMinCount: config.PurgeTagsKeepCount,
|
||||
KeepTagRegexp: config.PurgeTagsKeepRegexp,
|
||||
KeepFromFile: config.PurgeTagsKeepFromFile,
|
||||
}
|
||||
if config.PurgeTagsKeepFile != "" {
|
||||
if _, err := os.Stat(config.PurgeTagsKeepFile); os.IsNotExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
data, err := ioutil.ReadFile(config.PurgeTagsKeepFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
config.PurgeConfig.KeepTagsFromFile = gjson.ParseBytes(data)
|
||||
}
|
||||
|
||||
return &config
|
||||
}
|
||||
|
@ -2,7 +2,9 @@ package registry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"time"
|
||||
@ -11,11 +13,11 @@ import (
|
||||
)
|
||||
|
||||
type PurgeTagsConfig struct {
|
||||
DryRun bool
|
||||
KeepDays int
|
||||
KeepMinCount int
|
||||
KeepTagRegexp string
|
||||
KeepTagsFromFile gjson.Result
|
||||
DryRun bool
|
||||
KeepDays int
|
||||
KeepMinCount int
|
||||
KeepTagRegexp string
|
||||
KeepFromFile string
|
||||
}
|
||||
|
||||
type tagData struct {
|
||||
@ -48,6 +50,23 @@ func (p timeSlice) Swap(i, j int) {
|
||||
// PurgeOldTags purge old tags.
|
||||
func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
||||
logger := SetupLogging("registry.tasks.PurgeOldTags")
|
||||
|
||||
var keepTagsFromFile gjson.Result
|
||||
if config.KeepFromFile != "" {
|
||||
if _, err := os.Stat(config.KeepFromFile); os.IsNotExist(err) {
|
||||
logger.Warnf("Cannot open %s: %s", config.KeepFromFile, err)
|
||||
logger.Error("Not purging anything!")
|
||||
return
|
||||
}
|
||||
data, err := ioutil.ReadFile(config.KeepFromFile)
|
||||
if err != nil {
|
||||
logger.Warnf("Cannot read %s: %s", config.KeepFromFile, err)
|
||||
logger.Error("Not purging anything!")
|
||||
return
|
||||
}
|
||||
keepTagsFromFile = gjson.ParseBytes(data)
|
||||
}
|
||||
|
||||
dryRunText := ""
|
||||
if config.DryRun {
|
||||
logger.Warn("Dry-run mode enabled.")
|
||||
@ -88,8 +107,8 @@ func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
||||
if config.KeepTagRegexp != "" {
|
||||
logger.Infof("Keeping tags matching regexp: %s", config.KeepTagRegexp)
|
||||
}
|
||||
if config.KeepTagsFromFile.IsObject() {
|
||||
logger.Infof("Keeping tags for repos from the file: %+v", config.KeepTagsFromFile)
|
||||
if config.KeepFromFile != "" {
|
||||
logger.Infof("Keeping tags for repos from the file: %+v", keepTagsFromFile)
|
||||
}
|
||||
purgeTags := map[string][]string{}
|
||||
keepTags := map[string][]string{}
|
||||
@ -100,7 +119,7 @@ func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
||||
|
||||
// Prep the list of tags to preserve if defined in the file
|
||||
tagsFromFile := []string{}
|
||||
for _, i := range config.KeepTagsFromFile.Get(repo).Array() {
|
||||
for _, i := range keepTagsFromFile.Get(repo).Array() {
|
||||
tagsFromFile = append(tagsFromFile, i.String())
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user