This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Extract helper conduit: oscal_source
- Loading branch information
Showing
5 changed files
with
64 additions
and
52 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
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,49 @@ | ||
package oscal_source | ||
|
||
import ( | ||
"fmt" | ||
"github.com/docker/oscalkit/types/oscal" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// OSCALSource is intermediary that handles IO and low-level common operations consistently for oscalkit | ||
type OSCALSource struct { | ||
UserPath string | ||
file *os.File | ||
oscal *oscal.OSCAL | ||
} | ||
|
||
// Open creates new OSCALSource and load it up | ||
func Open(path string) (*OSCALSource, error) { | ||
result := OSCALSource{UserPath: path} | ||
return &result, result.open() | ||
} | ||
|
||
func (s *OSCALSource) open() error { | ||
var err error | ||
path := s.UserPath | ||
if !filepath.IsAbs(path) { | ||
if path, err = filepath.Abs(path); err != nil { | ||
return fmt.Errorf("Cannot get absolute path: %v", err) | ||
} | ||
} | ||
if _, err = os.Stat(path); err != nil { | ||
return fmt.Errorf("Cannot stat %s, %v", path, err) | ||
} | ||
if s.file, err = os.Open(path); err != nil { | ||
return fmt.Errorf("Cannot open file %s: %v", path, err) | ||
} | ||
if s.oscal, err = oscal.New(s.file); err != nil { | ||
return fmt.Errorf("Cannot parse file: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *OSCALSource) OSCAL() *oscal.OSCAL { | ||
return s.oscal | ||
} | ||
|
||
// Close the OSCALSource | ||
func (s *OSCALSource) Close() { | ||
} |