github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/imagetool/matchTriggers.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"sort"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/filesystem"
    10  	"github.com/Cloud-Foundations/Dominator/lib/json"
    11  	"github.com/Cloud-Foundations/Dominator/lib/log"
    12  	"github.com/Cloud-Foundations/Dominator/lib/triggers"
    13  )
    14  
    15  var quitEarly = errors.New("quit early")
    16  
    17  func matchTriggersSubcommand(args []string, logger log.DebugLogger) error {
    18  	if err := matchTriggers(args[0], args[1]); err != nil {
    19  		return fmt.Errorf("Error matching triggers: %s", err)
    20  	}
    21  	return nil
    22  }
    23  
    24  func matchTriggers(image string, triggersFile string) error {
    25  	fs, err := getTypedImage(image)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	trig, err := triggers.Load(triggersFile)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	fs.ForEachFile(
    34  		func(name string, inodeNumber uint64,
    35  			inode filesystem.GenericInode) error {
    36  			if _, nUnmatched := trig.GetMatchStatistics(); nUnmatched < 1 {
    37  				return quitEarly
    38  			}
    39  			trig.Match(name)
    40  			return nil
    41  		})
    42  	trig = &triggers.Triggers{Triggers: trig.GetMatchedTriggers()}
    43  	sort.Sort(trig)
    44  	return json.WriteWithIndent(os.Stdout, "    ", trig.Triggers)
    45  }