-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Components interface to common package
This patch moves the components interface to the common package Also, it get rids of the GetAndApply function that passes a callback This patch does not unexport all the functions because the workspace interface has not been created yet. Will come in another PR
- Loading branch information
James C. Scott
committed
Aug 19, 2016
1 parent
a0746d3
commit c1ba001
Showing
15 changed files
with
280 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package common | ||
|
||
import "github.com/blang/semver" | ||
|
||
// Component is the basic building block for all systems. | ||
// Schema info: https://github.com/opencontrol/schemas#component-yaml | ||
// | ||
// GetName returns the name of the component | ||
// | ||
// GetKey returns the key for the component (may not be unique). Useful for creating directories. | ||
// | ||
// SetKey sets the key for the component. Useful for overriding. | ||
// | ||
// GetAllSatisfies gets all the Satisfies objects for the component. | ||
// | ||
// GetVerifications get all the verifications. | ||
// | ||
// GetReferences get all the references. | ||
// | ||
// GetVersion returns the version | ||
// | ||
// SetVersion sets the version for the component. | ||
// | ||
// GetResponsibleRole gets the responsible party / role for the component. | ||
type Component interface { | ||
GetName() string | ||
GetKey() string | ||
SetKey(string) | ||
GetAllSatisfies() []Satisfies | ||
GetVerifications() *VerificationReferences | ||
GetReferences() *GeneralReferences | ||
GetVersion() semver.Version | ||
SetVersion(semver.Version) | ||
GetResponsibleRole() string | ||
} | ||
|
||
// Satisfies contains information regarding how the component satisfies a given standard and control | ||
// | ||
// GetStandardKey returns the standard | ||
// | ||
// GetControlKey returns the control | ||
// | ||
// GetNarratives gets all the general documentation for this particular standard and control | ||
// | ||
// GetParameters gets all the parameters for this particular standard and control | ||
// | ||
// GetCoveredBy gets the list of all the CoveredBy | ||
// | ||
// GetControlOrigin returns the control origin (only the first one if multiple) | ||
// | ||
// GetControlOrigins returns all the control origins | ||
// | ||
// GetImplementationStatus returns the implementation status (only the first one if multiple) | ||
// | ||
// GetImplementationStatuses returns all implementation statuses | ||
type Satisfies interface { | ||
GetStandardKey() string | ||
GetControlKey() string | ||
GetNarratives() []Section | ||
GetParameters() []Section | ||
GetCoveredBy() CoveredByList | ||
GetControlOrigin() string | ||
GetControlOrigins() []string | ||
GetImplementationStatus() string | ||
GetImplementationStatuses() []string | ||
} | ||
|
||
// Section is a general holder that allows it to be used in something like a map | ||
// | ||
// GetKey returns a unique key | ||
// | ||
// GetText returns the text for the section | ||
type Section interface { | ||
GetKey() string | ||
GetText() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package lib | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/opencontrol/compliance-masonry/lib/components" | ||
"github.com/opencontrol/compliance-masonry/lib/common" | ||
"fmt" | ||
) | ||
|
||
// componentsMap struct is a thread-safe structure mapping for components | ||
type componentsMap struct { | ||
mapping map[string]common.Component | ||
sync.RWMutex | ||
} | ||
|
||
// newComponents creates an instance of Components struct | ||
func newComponents() *componentsMap { | ||
return &componentsMap{mapping: make(map[string]common.Component)} | ||
} | ||
|
||
// add adds a new component to the component map | ||
func (components *componentsMap) add(component common.Component) { | ||
components.Lock() | ||
components.mapping[component.GetKey()] = component | ||
components.Unlock() | ||
} | ||
|
||
// Get retrieves a new component from the component map | ||
func (components *componentsMap) Get(key string) common.Component { | ||
components.RLock() | ||
defer components.RUnlock() | ||
return components.mapping[key] | ||
} | ||
|
||
// CompareAndAdd compares to see if the component exists in the map. If not, it adds the component. | ||
// This function is thread-safe. | ||
func (components *componentsMap) CompareAndAdd(component common.Component) bool { | ||
components.Lock() | ||
defer components.Unlock() | ||
added := false | ||
if _, exists := components.mapping[component.GetKey()]; !exists { | ||
components.mapping[component.GetKey()] = component | ||
added = true | ||
} | ||
return added | ||
} | ||
|
||
// GetAll retrieves all the components without giving directly to the map. | ||
func (components *componentsMap) GetAll() []common.Component { | ||
components.RLock() | ||
defer components.RUnlock() | ||
c := make([]common.Component, len(components.mapping)) | ||
idx := 0 | ||
for _, value := range components.mapping { | ||
c[idx] = value | ||
idx++ | ||
} | ||
return c | ||
} | ||
|
||
// LoadComponent imports components into a Component struct and adds it to the | ||
// Components map. | ||
func (ws *LocalWorkspace) LoadComponent(componentDir string) error { | ||
component, err := components.Load(componentDir) | ||
if err != nil { | ||
return err | ||
} | ||
// If the component is new, make sure we load the justifications as well. | ||
if ws.Components.CompareAndAdd(component) { | ||
ws.Justifications.LoadMappings(component) | ||
} else { | ||
return fmt.Errorf("Component: %s exists!\n", component.GetKey()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.