diff --git a/exec.go b/exec.go index 200359b2..a57da1bf 100644 --- a/exec.go +++ b/exec.go @@ -45,6 +45,7 @@ type ExecRes struct { PollInt int `yaml:"pollint"` // the poll interval for the ifcmd } +// NewExecRes is a constructor for this resource. It also calls Init() for you. func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcmd, ifshell string, pollint int, state string) *ExecRes { obj := &ExecRes{ BaseRes: BaseRes{ @@ -64,6 +65,7 @@ func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcm return obj } +// Init runs some startup code for this resource. func (obj *ExecRes) Init() { obj.BaseRes.kind = "Exec" obj.BaseRes.Init() // call base init, b/c we're overriding @@ -102,7 +104,7 @@ func (obj *ExecRes) BufioChanScanner(scanner *bufio.Scanner) (chan string, chan return ch, errch } -// Exec watcher +// Watch is the primary listener for this resource and it outputs events. func (obj *ExecRes) Watch(processChan chan Event) { if obj.IsWatching() { return @@ -201,6 +203,8 @@ func (obj *ExecRes) Watch(processChan chan Event) { } } +// CheckApply checks the resource state and applies the resource if the bool +// input is true. It returns error info and if the state check passed or not. // TODO: expand the IfCmd to be a list of commands func (obj *ExecRes) CheckApply(apply bool) (checkok bool, err error) { log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply) @@ -371,7 +375,8 @@ func (obj *ExecRes) AutoEdges() AutoEdge { return nil } -// include all params to make a unique identification of this object +// GetUUIDs includes all params to make a unique identification of this object. +// Most resources only return one, although some resources can return multiple. func (obj *ExecRes) GetUUIDs() []ResUUID { x := &ExecUUID{ BaseUUID: BaseUUID{name: obj.GetName(), kind: obj.Kind()}, @@ -382,6 +387,7 @@ func (obj *ExecRes) GetUUIDs() []ResUUID { return []ResUUID{x} } +// GroupCmp returns whether two resources can be grouped together or not. func (obj *ExecRes) GroupCmp(r Res) bool { _, ok := r.(*ExecRes) if !ok { @@ -390,6 +396,7 @@ func (obj *ExecRes) GroupCmp(r Res) bool { return false // not possible atm } +// Compare two resources and return if they are equivalent. func (obj *ExecRes) Compare(res Res) bool { switch res.(type) { case *ExecRes: diff --git a/file.go b/file.go index 9aedc1a4..789bd3cc 100644 --- a/file.go +++ b/file.go @@ -47,6 +47,7 @@ type FileRes struct { sha256sum string } +// NewFileRes is a constructor for this resource. It also calls Init() for you. func NewFileRes(name, path, dirname, basename, content, state string) *FileRes { // FIXME if path = nil, path = name ... obj := &FileRes{ @@ -64,11 +65,14 @@ func NewFileRes(name, path, dirname, basename, content, state string) *FileRes { return obj } +// Init runs some startup code for this resource. func (obj *FileRes) Init() { obj.BaseRes.kind = "File" obj.BaseRes.Init() // call base init, b/c we're overriding } +// GetPath returns the actual path to use for this resource. It computes this +// after analysis of the path, dirname and basename values. func (obj *FileRes) GetPath() string { d := Dirname(obj.Path) b := Basename(obj.Path) @@ -100,8 +104,9 @@ func (obj *FileRes) Validate() bool { return true } -// File watcher for files and directories -// Modify with caution, probably important to write some test cases first! +// Watch is the primary listener for this resource and it outputs events. +// This one is a file watcher for files and directories. +// Modify with caution, it is probably important to write some test cases first! // obj.GetPath(): file or directory func (obj *FileRes) Watch(processChan chan Event) { if obj.IsWatching() { @@ -268,6 +273,8 @@ func (obj *FileRes) Watch(processChan chan Event) { } } +// HashSHA256fromContent computes the hash of the file contents and returns it. +// It also caches the value if it can. func (obj *FileRes) HashSHA256fromContent() string { if obj.sha256sum != "" { // return if already computed return obj.sha256sum @@ -279,6 +286,8 @@ func (obj *FileRes) HashSHA256fromContent() string { return obj.sha256sum } +// FileHashSHA256Check computes the hash of the actual file and compares it to +// the computed hash of the resources file contents. func (obj *FileRes) FileHashSHA256Check() (bool, error) { if PathIsDir(obj.GetPath()) { // assert log.Fatal("This should only be called on a File resource.") @@ -304,6 +313,8 @@ func (obj *FileRes) FileHashSHA256Check() (bool, error) { return false, nil } +// FileApply writes the resource file contents out to the correct path. This +// implementation doesn't try to be particularly clever in any way. func (obj *FileRes) FileApply() error { if PathIsDir(obj.GetPath()) { log.Fatal("This should only be called on a File resource.") @@ -329,6 +340,8 @@ func (obj *FileRes) FileApply() error { return nil // success } +// CheckApply checks the resource state and applies the resource if the bool +// input is true. It returns error info and if the state check passed or not. func (obj *FileRes) CheckApply(apply bool) (checkok bool, err error) { log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply) @@ -398,12 +411,14 @@ func (obj *FileUUID) IFF(uuid ResUUID) bool { return obj.path == res.path } +// FileResAutoEdges holds the state of the auto edge generator. type FileResAutoEdges struct { data []ResUUID pointer int found bool } +// Next returns the next automatic edge. func (obj *FileResAutoEdges) Next() []ResUUID { if obj.found { log.Fatal("Shouldn't be called anymore!") @@ -416,7 +431,7 @@ func (obj *FileResAutoEdges) Next() []ResUUID { return []ResUUID{value} // we return one, even though api supports N } -// get results of the earlier Next() call, return if we should continue! +// Test gets results of the earlier Next() call, & returns if we should continue! func (obj *FileResAutoEdges) Test(input []bool) bool { // if there aren't any more remaining if len(obj.data) <= obj.pointer { @@ -459,6 +474,8 @@ func (obj *FileRes) AutoEdges() AutoEdge { } } +// GetUUIDs includes all params to make a unique identification of this object. +// Most resources only return one, although some resources can return multiple. func (obj *FileRes) GetUUIDs() []ResUUID { x := &FileUUID{ BaseUUID: BaseUUID{name: obj.GetName(), kind: obj.Kind()}, @@ -467,6 +484,7 @@ func (obj *FileRes) GetUUIDs() []ResUUID { return []ResUUID{x} } +// GroupCmp returns whether two resources can be grouped together or not. func (obj *FileRes) GroupCmp(r Res) bool { _, ok := r.(*FileRes) if !ok { @@ -477,6 +495,7 @@ func (obj *FileRes) GroupCmp(r Res) bool { return false // not possible atm } +// Compare two resources and return if they are equivalent. func (obj *FileRes) Compare(res Res) bool { switch res.(type) { case *FileRes: @@ -503,6 +522,7 @@ func (obj *FileRes) Compare(res Res) bool { return true } +// CollectPattern applies the pattern for collection resources. func (obj *FileRes) CollectPattern(pattern string) { // XXX: currently the pattern for files can only override the Dirname variable :P obj.Dirname = pattern // XXX: simplistic for now diff --git a/noop.go b/noop.go index 231c852e..b1547ed0 100644 --- a/noop.go +++ b/noop.go @@ -32,6 +32,7 @@ type NoopRes struct { Comment string `yaml:"comment"` // extra field for example purposes } +// NewNoopRes is a constructor for this resource. It also calls Init() for you. func NewNoopRes(name string) *NoopRes { obj := &NoopRes{ BaseRes: BaseRes{ @@ -43,6 +44,7 @@ func NewNoopRes(name string) *NoopRes { return obj } +// Init runs some startup code for this resource. func (obj *NoopRes) Init() { obj.BaseRes.kind = "Noop" obj.BaseRes.Init() // call base init, b/c we're overriding @@ -54,6 +56,7 @@ func (obj *NoopRes) Validate() bool { return true } +// Watch is the primary listener for this resource and it outputs events. func (obj *NoopRes) Watch(processChan chan Event) { if obj.IsWatching() { return @@ -109,8 +112,8 @@ func (obj *NoopRes) AutoEdges() AutoEdge { return nil } -// include all params to make a unique identification of this object -// most resources only return one, although some resources return multiple +// GetUUIDs includes all params to make a unique identification of this object. +// Most resources only return one, although some resources can return multiple. func (obj *NoopRes) GetUUIDs() []ResUUID { x := &NoopUUID{ BaseUUID: BaseUUID{name: obj.GetName(), kind: obj.Kind()}, @@ -119,6 +122,7 @@ func (obj *NoopRes) GetUUIDs() []ResUUID { return []ResUUID{x} } +// GroupCmp returns whether two resources can be grouped together or not. func (obj *NoopRes) GroupCmp(r Res) bool { _, ok := r.(*NoopRes) if !ok { @@ -131,6 +135,7 @@ func (obj *NoopRes) GroupCmp(r Res) bool { return true // noop resources can always be grouped together! } +// Compare two resources and return if they are equivalent. func (obj *NoopRes) Compare(res Res) bool { switch res.(type) { // we can only compare NoopRes to others of the same resource diff --git a/pkg.go b/pkg.go index d5400687..c84e7f02 100644 --- a/pkg.go +++ b/pkg.go @@ -42,7 +42,7 @@ type PkgRes struct { fileList []string // FIXME: update if pkg changes } -// NewPkgRes is a helper function for creating new resources that call Init(). +// NewPkgRes is a constructor for this resource. It also calls Init() for you. func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupported bool) *PkgRes { obj := &PkgRes{ BaseRes: BaseRes{ @@ -57,6 +57,7 @@ func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupporte return obj } +// Init runs some startup code for this resource. func (obj *PkgRes) Init() { obj.BaseRes.kind = "Pkg" obj.BaseRes.Init() // call base init, b/c we're overriding @@ -94,8 +95,8 @@ func (obj *PkgRes) Init() { } } +// Validate checks if the resource data structure was populated correctly. func (obj *PkgRes) Validate() bool { - if obj.State == "" { return false } @@ -103,7 +104,8 @@ func (obj *PkgRes) Validate() bool { return true } -// use UpdatesChanged signal to watch for changes +// Watch is the primary listener for this resource and it outputs events. +// It uses the PackageKit UpdatesChanged signal to watch for changes. // TODO: https://github.com/hughsie/PackageKit/issues/109 // TODO: https://github.com/hughsie/PackageKit/issues/110 func (obj *PkgRes) Watch(processChan chan Event) { @@ -243,6 +245,8 @@ func (obj *PkgRes) pkgMappingHelper(bus *Conn) (map[string]*PkPackageIDActionDat return result, nil } +// CheckApply checks the resource state and applies the resource if the bool +// input is true. It returns error info and if the state check passed or not. func (obj *PkgRes) CheckApply(apply bool) (checkok bool, err error) { log.Printf("%v: CheckApply(%t)", obj.fmtNames(obj.getNames()), apply) @@ -359,6 +363,7 @@ func (obj *PkgUUID) IFF(uuid ResUUID) bool { return obj.name == res.name } +// PkgResAutoEdges holds the state of the auto edge generator. type PkgResAutoEdges struct { fileList []string svcUUIDs []ResUUID @@ -367,6 +372,7 @@ type PkgResAutoEdges struct { kind string } +// Next returns the next automatic edge. func (obj *PkgResAutoEdges) Next() []ResUUID { if obj.testIsNext { log.Fatal("Expecting a call to Test()") @@ -394,6 +400,7 @@ func (obj *PkgResAutoEdges) Next() []ResUUID { return result } +// Test gets results of the earlier Next() call, & returns if we should continue! func (obj *PkgResAutoEdges) Test(input []bool) bool { if !obj.testIsNext { log.Fatal("Expecting a call to Next()") @@ -474,7 +481,8 @@ func (obj *PkgRes) AutoEdges() AutoEdge { } } -// include all params to make a unique identification of this object +// GetUUIDs includes all params to make a unique identification of this object. +// Most resources only return one, although some resources can return multiple. func (obj *PkgRes) GetUUIDs() []ResUUID { x := &PkgUUID{ BaseUUID: BaseUUID{name: obj.GetName(), kind: obj.Kind()}, @@ -485,6 +493,7 @@ func (obj *PkgRes) GetUUIDs() []ResUUID { return result } +// GroupCmp returns whether two resources can be grouped together or not. // can these two resources be merged ? // (aka does this resource support doing so?) // will resource allow itself to be grouped _into_ this obj? @@ -506,6 +515,7 @@ func (obj *PkgRes) GroupCmp(r Res) bool { return true } +// Compare two resources and return if they are equivalent. func (obj *PkgRes) Compare(res Res) bool { switch res.(type) { case *PkgRes: diff --git a/svc.go b/svc.go index 0fda08a2..19546a91 100644 --- a/svc.go +++ b/svc.go @@ -40,6 +40,7 @@ type SvcRes struct { Startup string `yaml:"startup"` // enabled, disabled, undefined } +// NewSvcRes is a constructor for this resource. It also calls Init() for you. func NewSvcRes(name, state, startup string) *SvcRes { obj := &SvcRes{ BaseRes: BaseRes{ @@ -52,11 +53,13 @@ func NewSvcRes(name, state, startup string) *SvcRes { return obj } +// Init runs some startup code for this resource. func (obj *SvcRes) Init() { obj.BaseRes.kind = "Svc" obj.BaseRes.Init() // call base init, b/c we're overriding } +// Validate checks if the resource data structure was populated correctly. func (obj *SvcRes) Validate() bool { if obj.State != "running" && obj.State != "stopped" && obj.State != "" { return false @@ -67,7 +70,7 @@ func (obj *SvcRes) Validate() bool { return true } -// Service watcher +// Watch is the primary listener for this resource and it outputs events. func (obj *SvcRes) Watch(processChan chan Event) { if obj.IsWatching() { return @@ -228,6 +231,8 @@ func (obj *SvcRes) Watch(processChan chan Event) { } } +// CheckApply checks the resource state and applies the resource if the bool +// input is true. It returns error info and if the state check passed or not. func (obj *SvcRes) CheckApply(apply bool) (checkok bool, err error) { log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply) @@ -341,12 +346,14 @@ func (obj *SvcUUID) IFF(uuid ResUUID) bool { return obj.name == res.name } +// SvcResAutoEdges holds the state of the auto edge generator. type SvcResAutoEdges struct { data []ResUUID pointer int found bool } +// Next returns the next automatic edge. func (obj *SvcResAutoEdges) Next() []ResUUID { if obj.found { log.Fatal("Shouldn't be called anymore!") @@ -359,7 +366,7 @@ func (obj *SvcResAutoEdges) Next() []ResUUID { return []ResUUID{value} // we return one, even though api supports N } -// get results of the earlier Next() call, return if we should continue! +// Test gets results of the earlier Next() call, & returns if we should continue! func (obj *SvcResAutoEdges) Test(input []bool) bool { // if there aren't any more remaining if len(obj.data) <= obj.pointer { @@ -403,7 +410,8 @@ func (obj *SvcRes) AutoEdges() AutoEdge { } } -// include all params to make a unique identification of this object +// GetUUIDs includes all params to make a unique identification of this object. +// Most resources only return one, although some resources can return multiple. func (obj *SvcRes) GetUUIDs() []ResUUID { x := &SvcUUID{ BaseUUID: BaseUUID{name: obj.GetName(), kind: obj.Kind()}, @@ -412,6 +420,7 @@ func (obj *SvcRes) GetUUIDs() []ResUUID { return []ResUUID{x} } +// GroupCmp returns whether two resources can be grouped together or not. func (obj *SvcRes) GroupCmp(r Res) bool { _, ok := r.(*SvcRes) if !ok { @@ -423,6 +432,7 @@ func (obj *SvcRes) GroupCmp(r Res) bool { return false // not possible atm } +// Compare two resources and return if they are equivalent. func (obj *SvcRes) Compare(res Res) bool { switch res.(type) { case *SvcRes: diff --git a/timer.go b/timer.go index 4b602cce..689c2246 100644 --- a/timer.go +++ b/timer.go @@ -39,7 +39,7 @@ type TimerUUID struct { name string } -// NewTimerRes creates a new TimerRes. +// NewTimerRes is a constructor for this resource. It also calls Init() for you. func NewTimerRes(name string, interval int) *TimerRes { obj := &TimerRes{ BaseRes: BaseRes{ @@ -51,6 +51,7 @@ func NewTimerRes(name string, interval int) *TimerRes { return obj } +// Init runs some startup code for this resource. func (obj *TimerRes) Init() { obj.BaseRes.kind = "Timer" obj.BaseRes.Init() // call base init, b/c we're overrriding @@ -63,6 +64,7 @@ func (obj *TimerRes) Validate() bool { return true } +// Watch is the primary listener for this resource and it outputs events. func (obj *TimerRes) Watch(processChan chan Event) { if obj.IsWatching() { return @@ -104,6 +106,8 @@ func (obj *TimerRes) Watch(processChan chan Event) { } } +// GetUUIDs includes all params to make a unique identification of this object. +// Most resources only return one, although some resources can return multiple. func (obj *TimerRes) GetUUIDs() []ResUUID { x := &TimerUUID{ BaseUUID: BaseUUID{ @@ -120,6 +124,7 @@ func (obj *TimerRes) AutoEdges() AutoEdge { return nil } +// Compare two resources and return if they are equivalent. func (obj *TimerRes) Compare(res Res) bool { switch res.(type) { case *TimerRes: @@ -144,7 +149,3 @@ func (obj *TimerRes) CheckApply(apply bool) (bool, error) { log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply) return true, nil // state is always okay } - -func (obj *TimerRes) CollectPatten(pattern string) { - return -}