Skip to content

Commit

Permalink
Reorganised the project so it follows the rules:
Browse files Browse the repository at this point in the history
- All methods use pointer receivers
- All constructor functions are defined directly under the struct defnition that they create
- All methods are defined under the constructor function of the struct or under the struct defnition
  • Loading branch information
Nusiq committed Nov 5, 2021
1 parent 2a2ed2a commit 97326ca
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 271 deletions.
30 changes: 15 additions & 15 deletions regolith/file_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ type EditedFiles struct {
Bp map[string]filesList `json:"bp"`
}

// LoadEditedFiles data from edited_files.json or returns an empty object
// if file doesn't exist.
func LoadEditedFiles() EditedFiles {
data, err := os.ReadFile(EditedFilesPath)
if err != nil {
return NewEditedFiles()
}
result := NewEditedFiles()
err = json.Unmarshal(data, &result)
if err != nil {
return NewEditedFiles()
}
return result
}

// Dump dumps EditedFiles to EditedFilesPath in JSON format.
func (f *EditedFiles) Dump() error {
result, err := json.MarshalIndent(f, "", "\t")
Expand Down Expand Up @@ -71,21 +86,6 @@ func (f *EditedFiles) UpdateFromPaths(rpPath string, bpPath string) error {
return nil
}

// LoadEditedFiles data from edited_files.json or returns an empty object
// if file doesn't exist.
func LoadEditedFiles() EditedFiles {
data, err := os.ReadFile(EditedFilesPath)
if err != nil {
return NewEditedFiles()
}
result := NewEditedFiles()
err = json.Unmarshal(data, &result)
if err != nil {
return NewEditedFiles()
}
return result
}

// NewEditedFiles creates new EditedFiles object with lists of the files from
// rpPath and bpPath.
func NewEditedFiles() EditedFiles {
Expand Down
28 changes: 0 additions & 28 deletions regolith/filters.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package regolith

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"time"
)
Expand Down Expand Up @@ -82,31 +79,6 @@ func RunStandardFilter(filter Filter) error {
return RunRemoteFilter(FilterNameToUrl(StandardLibraryUrl, filter.Filter), filter)
}

// LoadFiltersFromPath returns a Profile with list of filters loaded from
// filters.json from input file path. The path should point at a directory
// with filters.json file in it, not at the file itself.
func LoadFiltersFromPath(path string) (*Profile, error) {
path = path + "/filter.json"
file, err := ioutil.ReadFile(path)

if err != nil {
return nil, wrapError(fmt.Sprintf("Couldn't find %s! Consider running 'regolith install'", path), err)
}

var result *Profile
err = json.Unmarshal(file, &result)
if err != nil {
return nil, wrapError(fmt.Sprintf("Couldn't load %s: ", path), err)
}
// Replace nil filter settings with empty map
for fk := range result.Filters {
if result.Filters[fk].Settings == nil {
result.Filters[fk].Settings = make(map[string]interface{})
}
}
return result, nil
}

// RunRemoteFilter runs loads and runs the content of filter.json from in
// regolith cache. The url is the URL of the filter from which the filter
// was downloaded (used to specify its path in the cache). The parentFilter
Expand Down
3 changes: 2 additions & 1 deletion regolith/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func RunProfile(profileName string) error {
}

// Run the filters!
for _, filter := range profile.Filters {
for filter := range profile.Filters {
filter := profile.Filters[filter]
path, _ := filepath.Abs(".")
err := filter.RunFilter(path)
if err != nil {
Expand Down
Loading

0 comments on commit 97326ca

Please sign in to comment.