Skip to content

Commit

Permalink
Fix file names and permissions
Browse files Browse the repository at this point in the history
- Add file permissions constants
- Add function to handle filenames
- Add function to handle writing files
- Files shouldn't be executable
- Files shouldn't use spaces in file names for multi-OS support
  • Loading branch information
redhatrises committed Jul 30, 2018
1 parent 60895be commit 5aa7472
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 16 deletions.
13 changes: 13 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (C) 2018 OpenControl Contributors. See LICENSE.md for license.
*/

package constants

const (
// FileReadWrite handle Read/Write File Permissions
FileReadWrite = 0600

// FileReadWriteExec handle Read/Write/Write Execute Permissions
FileReadWriteExec = 0700
)
37 changes: 37 additions & 0 deletions internal/utils/masonryutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright (C) 2018 OpenControl Contributors. See LICENSE.md for license.
*/

package masonryutil

import (
"io/ioutil"
"os"
"strings"
)

// replaceParentheses removes parenthesis in strings
func replaceParentheses(text string) string {
return strings.Replace(strings.Replace(text, "(", "", -1), ")", "", -1)
}

// replaceSpace changes whitepspace to underscore in strings
func replaceSpaceUnderscore(text string) string {
return strings.Replace(text, " ", "_", -1)
}

// FileNameHandler creates a standardized filename
func FileNameHandler(fileName string) string {
normalizedFileName := replaceParentheses(fileName)
normalizedFileName = replaceSpaceUnderscore(normalizedFileName)

return normalizedFileName
}

// FileWriter handles all file writing for Compliance Masonry
func FileWriter(filePath string, fileText []byte, filePerms os.FileMode) {
err := ioutil.WriteFile(filePath, fileText, filePerms)
if err != nil {
panic(err)
}
}
5 changes: 0 additions & 5 deletions pkg/cli/docs/gitbook/gitbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package gitbook
import (
"fmt"
"path/filepath"
"strings"

"github.com/opencontrol/compliance-masonry/pkg/lib"
"github.com/opencontrol/compliance-masonry/pkg/lib/common"
Expand Down Expand Up @@ -51,10 +50,6 @@ func exportLink(text string, location string) string {
return fmt.Sprintf("* [%s](%s)\n", text, location)
}

func replaceParentheses(text string) string {
return strings.Replace(strings.Replace(text, "(", "", -1), ")", "", -1)
}

// BuildGitbook entry point for creating gitbook
func (config Config) BuildGitbook() []error {
var errs []error
Expand Down
8 changes: 5 additions & 3 deletions pkg/cli/docs/gitbook/gitbookCertification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package gitbook

import (
"fmt"
"io/ioutil"

"path/filepath"

"github.com/opencontrol/compliance-masonry/internal/constants"
"github.com/opencontrol/compliance-masonry/internal/utils"
"github.com/opencontrol/compliance-masonry/pkg/lib/common"
)

Expand Down Expand Up @@ -92,7 +94,7 @@ func (openControl *OpenControlGitBook) getControlOrigin(text string, controlOrig
}

func (openControl *OpenControlGitBook) exportControl(control *ControlGitbook) (string, string) {
key := replaceParentheses(fmt.Sprintf("%s-%s", control.standardKey, control.controlKey))
key := masonryutil.FileNameHandler(fmt.Sprintf("%s-%s", control.standardKey, control.controlKey))
text := fmt.Sprintf("# %s\n## %s\n", key, control.GetName())
if len(control.GetDescription()) > 0 {
text += "#### Description\n"
Expand Down Expand Up @@ -136,7 +138,7 @@ func (openControl *OpenControlGitBook) exportStandards() {
for _, controlKey := range controlKeys {
control := standard.GetControl(controlKey)
controlPath, controlText := openControl.exportControl(&ControlGitbook{control, standardsExportPath, standardKey, controlKey})
ioutil.WriteFile(controlPath, []byte(controlText), 0700)
masonryutil.FileWriter(controlPath, []byte(controlText), constants.FileReadWrite)
}
}
}
6 changes: 4 additions & 2 deletions pkg/cli/docs/gitbook/gitbookComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package gitbook

import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"

"github.com/opencontrol/compliance-masonry/internal/constants"
"github.com/opencontrol/compliance-masonry/internal/utils"
)

func (component *ComponentGitbook) exportComponent() (string, string) {
Expand Down Expand Up @@ -43,6 +45,6 @@ func (openControl *OpenControlGitBook) exportComponents() {
for _, component := range openControl.GetAllComponents() {
componentsGitBook := ComponentGitbook{component, componentsExportPath}
componentPath, componentText := componentsGitBook.exportComponent()
ioutil.WriteFile(componentPath, []byte(componentText), 0700)
masonryutil.FileWriter(componentPath, []byte(componentText), constants.FileReadWrite)
}
}
8 changes: 5 additions & 3 deletions pkg/cli/docs/gitbook/gitbookSummaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package gitbook
import (
"fmt"
"github.com/opencontrol/compliance-masonry/pkg/lib/common"
"io/ioutil"
"path/filepath"
"strings"

"github.com/opencontrol/compliance-masonry/internal/constants"
"github.com/opencontrol/compliance-masonry/internal/utils"
)

// createSubHeading will create a subheading with the passed in string.
Expand Down Expand Up @@ -80,7 +82,7 @@ func (*OpenControlGitBook) buildStandardsSummary(standardKey, controlKey string,
oldFamilyFileName fileName, familySummaryMap map[string]string) (string, fileName, map[string]string) {
summary := ""
// format the filename
controlLink := replaceParentheses(createFileName(standardKey, controlKey).withExt(".md"))
controlLink := masonryutil.FileNameHandler(createFileName(standardKey, controlKey).withExt(".md"))

// get the control.
control := standard.GetControl(controlKey)
Expand Down Expand Up @@ -112,7 +114,7 @@ func (*OpenControlGitBook) buildStandardsSummary(standardKey, controlKey string,

func (openControl *OpenControlGitBook) exportFamilyReadMap(familySummaryMap *map[string]string) {
for family, familySummary := range *(familySummaryMap) {
ioutil.WriteFile(filepath.Join(openControl.exportPath, "standards", family+".md"), []byte(familySummary), 0700)
masonryutil.FileWriter(filepath.Join(openControl.exportPath, "standards", family+".md"), []byte(familySummary), constants.FileReadWrite)
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/docs/gitbook/gitbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"strings"
"testing"

"github.com/opencontrol/compliance-masonry/internal/utils"
)

type exportLinkTest struct {
Expand Down Expand Up @@ -56,7 +58,7 @@ var replaceParenthesesTests = []replaceParenthesesTest{

func TestReplaceParentheses(t *testing.T) {
for _, example := range replaceParenthesesTests {
actual := replaceParentheses(example.text)
actual := masonryutil.FileNameHandler(example.text)
if actual != example.expected {
t.Errorf("Expected: `%s`, Actual: `%s`", example.expected, actual)
}
Expand Down
5 changes: 3 additions & 2 deletions tools/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

metaleapfs "github.com/metaleap/go-util/fs"
"github.com/opencontrol/compliance-masonry/internal/constants"
)

//go:generate mockery -name Util
Expand Down Expand Up @@ -69,14 +70,14 @@ func (fs OSUtil) AppendOrCreate(filePath string, text string) error {
if _, err = os.Stat(filePath); err == nil {
err = AppendToFile(filePath, text)
} else {
err = ioutil.WriteFile(filePath, []byte(text), 0700)
err = ioutil.WriteFile(filePath, []byte(text), constants.FileReadWrite)
}
return err
}

// AppendToFile adds text to a file
func AppendToFile(filePath string, text string) error {
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0700)
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, constants.FileReadWrite)
if err != nil {
return err
}
Expand Down

0 comments on commit 5aa7472

Please sign in to comment.