Skip to content

Commit

Permalink
pkg/cdi: unexport spec.Write(), NewSpec().
Browse files Browse the repository at this point in the history
This should reduce confusion about how generates Specs
should be written (cache.WriteSpec() vs. spec.Write())
and also reduce the possibility/temptation for users to
bypass the registry/cache when interacting with Specs.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Oct 27, 2022
1 parent 7c8505c commit 98524c3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error {
path += defaultSpecExt
}

spec, err = NewSpec(raw, path, prio)
spec, err = newSpec(raw, path, prio)
if err != nil {
return err
}

return spec.Write(true)
return spec.write(true)
}

// RemoveSpec removes a Spec with the given name from the highest
Expand Down
14 changes: 7 additions & 7 deletions pkg/cdi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ func ReadSpec(path string, priority int) (*Spec, error) {
return nil, errors.Errorf("failed to parse CDI Spec %q, no Spec data", path)
}

spec, err := NewSpec(raw, path, priority)
spec, err := newSpec(raw, path, priority)
if err != nil {
return nil, err
}

return spec, nil
}

// NewSpec creates a new Spec from the given CDI Spec data. The
// newSpec creates a new Spec from the given CDI Spec data. The
// Spec is marked as loaded from the given path with the given
// priority. If Spec data validation fails NewSpec returns a nil
// priority. If Spec data validation fails newSpec returns a nil
// Spec and an error.
func NewSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) {
func newSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) {
err := validateSpec(raw)
if err != nil {
return nil, err
Expand All @@ -124,8 +124,8 @@ func NewSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) {
}

// Write the CDI Spec to the file associated with it during instantiation
// by NewSpec() or ReadSpec().
func (s *Spec) Write(overwrite bool) error {
// by newSpec() or ReadSpec().
func (s *Spec) write(overwrite bool) error {
var (
data []byte
dir string
Expand Down Expand Up @@ -259,7 +259,7 @@ func ParseSpec(data []byte) (*cdi.Spec, error) {

// SetSpecValidator sets a CDI Spec validator function. This function
// is used for extra CDI Spec content validation whenever a Spec file
// loaded (using ReadSpec() or NewSpec()) or written (Spec.Write()).
// loaded (using ReadSpec() or written (using WriteSpec()).
func SetSpecValidator(fn func(*cdi.Spec) error) {
validatorLock.Lock()
defer validatorLock.Unlock()
Expand Down
12 changes: 6 additions & 6 deletions pkg/cdi/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ devices:
}
require.NoError(t, err)

spec, err = NewSpec(raw, tc.name, 0)
spec, err = newSpec(raw, tc.name, 0)
if tc.invalid || tc.schemaFail {
require.Error(t, err)
require.Nil(t, spec)
Expand Down Expand Up @@ -367,22 +367,22 @@ devices:
err = yaml.Unmarshal([]byte(tc.data), raw)
require.NoError(t, err)

spec, err = NewSpec(raw, filepath.Join(dir, tc.name), 0)
spec, err = newSpec(raw, filepath.Join(dir, tc.name), 0)
if tc.invalid {
require.Error(t, err, "NewSpec with invalid data")
require.Nil(t, spec, "NewSpec with invalid data")
require.Error(t, err, "newSpec with invalid data")
require.Nil(t, spec, "newSpec with invalid data")
return
}

require.NoError(t, err)
require.NotNil(t, spec)

err = spec.Write(true)
err = spec.write(true)
require.NoError(t, err)
_, err = os.Stat(spec.GetPath())
require.NoError(t, err, "spec.Write destination file")

err = spec.Write(false)
err = spec.write(false)
require.Error(t, err)

chk, err = ReadSpec(spec.GetPath(), spec.GetPriority())
Expand Down

0 comments on commit 98524c3

Please sign in to comment.