Skip to content

Commit

Permalink
#59: added -run option for filtering tasks to run
Browse files Browse the repository at this point in the history
  • Loading branch information
xelalexv committed Jun 3, 2022
1 parent 5ebf064 commit e6258f8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ If you want to use *GCR* or artifact registry as the source for a public image,
## Usage

```bash
dregsy -config={path to config file}
dregsy -config={path to config file} [-run={task name regexp}]
```

If there are any periodic sync tasks defined (see *Configuration* above), *dregsy* remains running indefinitely. Otherwise, it will return once all one-off tasks have been processed.
If there are any periodic sync tasks defined (see *Configuration* above), *dregsy* remains running indefinitely. Otherwise, it will return once all one-off tasks have been processed. With the `-run` argument you can filter tasks. Only those tasks for which the task name matches the given regular expression will be run. Note that the regular expression performs a line match, so you don't need to place the expression in `^...$` to get an exact match. For example, `-run=task-a` will only select `task-a`, but not `task-abc`.

### Logging
Logging behavior can be changed with these environment variables:
Expand Down
6 changes: 4 additions & 2 deletions cmd/dregsy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func main() {

fs := flag.NewFlagSet("dregsy", flag.ContinueOnError)
configFile := fs.String("config", "", "path to config file")
taskFilter := fs.String("run", "", "task filter regex")

if testRound {
if len(testArgs) > 0 {
Expand All @@ -94,7 +95,8 @@ func main() {

if len(*configFile) == 0 {
version()
fmt.Println("synopsis: dregsy -config={config file}")
fmt.Println(
"synopsis: dregsy -config={config file} [-run {task name regex}]")
exit(1)
}

Expand All @@ -110,7 +112,7 @@ func main() {
testSync <- s
}

err = s.SyncFromConfig(conf)
err = s.SyncFromConfig(conf, *taskFilter)
s.Dispose()

log.Debug("exit main")
Expand Down
16 changes: 13 additions & 3 deletions internal/pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/xelalexv/dregsy/internal/pkg/relays"
"github.com/xelalexv/dregsy/internal/pkg/relays/docker"
"github.com/xelalexv/dregsy/internal/pkg/relays/skopeo"
"github.com/xelalexv/dregsy/internal/pkg/util"
)

//
Expand Down Expand Up @@ -106,15 +107,24 @@ func (s *Sync) Dispose() {
}

//
func (s *Sync) SyncFromConfig(conf *SyncConfig) error {
func (s *Sync) SyncFromConfig(conf *SyncConfig, taskFilter string) error {

if taskFilter == "" {
taskFilter = ".*"
}

tf, err := util.NewRegex(taskFilter)
if err != nil {
return fmt.Errorf("invalid task filter: %v", err)
}

if err := s.relay.Prepare(); err != nil {
return err
}

// one-off tasks
for _, t := range conf.Tasks {
if t.Interval == 0 {
if t.Interval == 0 && tf.Matches(t.Name) {
s.syncTask(t)
}
}
Expand All @@ -124,7 +134,7 @@ func (s *Sync) SyncFromConfig(conf *SyncConfig) error {
ticking := false

for _, t := range conf.Tasks {
if t.Interval > 0 {
if t.Interval > 0 && tf.Matches(t.Name) {
t.startTicking(c)
ticking = true
}
Expand Down
38 changes: 3 additions & 35 deletions internal/pkg/tags/tagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package tags

import (
"fmt"
"regexp"
"sort"
"strings"

Expand All @@ -45,7 +44,7 @@ func NewTagSet(tags []string) (*TagSet, error) {
type TagSet struct {
verbatim []string
semver []semver.Range
regex []*regex
regex []*util.Regex
}

//
Expand Down Expand Up @@ -86,7 +85,7 @@ func (ts *TagSet) addSemver(s string) error {

//
func (ts *TagSet) addRegex(r string) error {
reg, err := newRegex(strings.TrimSpace(r[len(RegexpPrefix):]))
reg, err := util.NewRegex(strings.TrimSpace(r[len(RegexpPrefix):]))
if err != nil {
return err
}
Expand Down Expand Up @@ -194,7 +193,7 @@ func (ts *TagSet) expandRegex(tags []string) []string {
var ret []string
for _, t := range tags {
for _, regex := range ts.regex {
if regex.matches(t) {
if regex.Matches(t) {
ret = append(ret, t)
break
}
Expand All @@ -221,34 +220,3 @@ func isSemver(tag string) bool {
func isRegex(tag string) bool {
return strings.HasPrefix(tag, RegexpPrefix)
}

//
func newRegex(r string) (*regex, error) {

r = strings.TrimSpace(r)
inverted := strings.HasPrefix(r, "!")
if inverted {
r = r[1:]
}

reg, err := util.CompileRegex(r, true)
if err != nil {
return nil, err
}

return &regex{expr: reg, inverted: inverted}, nil
}

//
type regex struct {
expr *regexp.Regexp
inverted bool
}

//
func (r *regex) matches(s string) bool {
if r.inverted {
return !r.expr.MatchString(s)
}
return r.expr.MatchString(s)
}
67 changes: 67 additions & 0 deletions internal/pkg/util/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2022 Alexander Vollschwitz <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"fmt"
"regexp"
"strings"
)

//
func CompileRegex(v string, lineMatch bool) (*regexp.Regexp, error) {
if lineMatch {
if !strings.HasPrefix(v, "^") {
v = fmt.Sprintf("^%s", v)
}
if !strings.HasSuffix(v, "$") {
v = fmt.Sprintf("%s$", v)
}
}
return regexp.Compile(v)
}

//
func NewRegex(r string) (*Regex, error) {

r = strings.TrimSpace(r)
inverted := strings.HasPrefix(r, "!")
if inverted {
r = r[1:]
}

reg, err := CompileRegex(r, true)
if err != nil {
return nil, err
}

return &Regex{expr: reg, inverted: inverted}, nil
}

//
type Regex struct {
expr *regexp.Regexp
inverted bool
}

//
func (r *Regex) Matches(s string) bool {
if r.inverted {
return !r.expr.MatchString(s)
}
return r.expr.MatchString(s)
}
14 changes: 0 additions & 14 deletions internal/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"regexp"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -72,19 +71,6 @@ func SplitPlatform(p string) (os, arch, variant string) {
return
}

//
func CompileRegex(v string, lineMatch bool) (*regexp.Regexp, error) {
if lineMatch {
if !strings.HasPrefix(v, "^") {
v = fmt.Sprintf("^%s", v)
}
if !strings.HasSuffix(v, "$") {
v = fmt.Sprintf("%s$", v)
}
}
return regexp.Compile(v)
}

//
type creds struct {
Username string
Expand Down

0 comments on commit e6258f8

Please sign in to comment.