Skip to content

Commit

Permalink
improve code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Oct 5, 2021
1 parent 993352a commit 627047a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion rules/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

// Action to perform on a file
// Action represents an action to perform on a file.
type Action struct {
Action string `yaml:"action"`
Payload string `yaml:"payload"`
Expand Down
19 changes: 10 additions & 9 deletions rules/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"sync"

"github.com/reugn/fsweeper/ospkg"
"github.com/reugn/fsweeper/internal/ospkg"
"gopkg.in/yaml.v2"
)

Expand All @@ -32,14 +32,14 @@ const (
)

var (
// Filters is the supported filters list
// Filters represents the supported filters list.
Filters = [...]string{filterName, filterExtension, filterSize, filterLastEdited, filterContains}

// Actions is the supported actions list
// Actions represents the supported actions list.
Actions = [...]string{actionEcho, actionTouch, actionMove, actionRename, actionDelete}
)

// Rule configuration to apply on a file
// Rule represents a configuration to apply on a file.
type Rule struct {
Path string `yaml:"path"`
Recursive bool `yaml:"recursive"`
Expand Down Expand Up @@ -120,19 +120,19 @@ func (r *Rule) runActions(filePath string, vars *Vars) {
}
}

// Config is a multiple rules container
// Config is a multiple rules container.
type Config struct {
Vars Vars `yaml:"vars"`
Rules []Rule `yaml:"rules"`
wg sync.WaitGroup
}

// ReadConfig reads configuration from the default configuration file
// ReadConfig reads configuration from the default configuration file.
func ReadConfig() *Config {
return ReadConfigFromFile(GetDefaultConfigFile())
}

// ReadConfigFromFile reads configuration from a custom configuration file
// ReadConfigFromFile reads configuration from a custom configuration file.
func ReadConfigFromFile(file string) *Config {
c := &Config{}

Expand All @@ -155,7 +155,7 @@ func ReadConfigFromFile(file string) *Config {
return c
}

// ReadConfigFromByteArray reads configuration from a given byte array
// ReadConfigFromByteArray reads configuration from the given byte array.
func ReadConfigFromByteArray(configYaml []byte) *Config {
c := &Config{}

Expand All @@ -169,7 +169,7 @@ func ReadConfigFromByteArray(configYaml []byte) *Config {
return c
}

// Execute rules
// Execute executes rules.
func (c *Config) Execute() {
for _, rule := range c.Rules {
c.wg.Add(1)
Expand Down Expand Up @@ -212,5 +212,6 @@ func validatePath(dirPath string) string {
if strings.HasSuffix(dirPath, ospkg.PathSeparator) {
return dirPath
}

return dirPath + ospkg.PathSeparator
}
4 changes: 2 additions & 2 deletions rules/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"
)

// Filter to verify on a file
// Filter represents filter rules to apply on files.
type Filter struct {
Filter string `yaml:"filter"`
Payload string `yaml:"payload"`
Expand All @@ -28,7 +28,7 @@ func (f *Filter) extensionFilter(filePath string) bool {
func (f *Filter) containsFilter(filePath string) bool {
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Printf("Failed to read file %s. %s\n", filePath, err.Error)
log.Printf("Failed to read file %s. %s\n", filePath, err.Error())
return false
}

Expand Down
3 changes: 2 additions & 1 deletion rules/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const (
lenFunc = "len"
)

// Pipeline chains together a series of template commands to compactly express a series of transformations
// Pipeline chains together series of template commands to compactly express
// series of transformations.
func Pipeline(in string, chain []string) string {
out := in

Expand Down
4 changes: 2 additions & 2 deletions rules/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package rules

import "os"

// DefaultConfigFile is a configuration file default path
// DefaultConfigFile is the configuration file default path.
const DefaultConfigFile string = "conf.yaml"

// GetDefaultConfigFile returns a configuration file default path
// GetDefaultConfigFile returns a configuration file path.
func GetDefaultConfigFile() string {
confFile, ok := os.LookupEnv("FSWEEPER_CONFIG_FILE")
if !ok {
Expand Down
22 changes: 11 additions & 11 deletions rules/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/reugn/fsweeper/ospkg"
"github.com/reugn/fsweeper/internal/ospkg"
"gopkg.in/yaml.v2"
)

Expand All @@ -31,29 +31,29 @@ const (

var re *regexp.Regexp = regexp.MustCompile(`\{\{(.+?)\}\}`)

// Vars are the configuration variables to substitute
// Vars represents configuration variables for substitution.
type Vars struct {
TimeFormat string `yaml:"timeFormat"`
DateFormat string `yaml:"dateFormat"`
DateTimeFormat string `yaml:"dateTimeFormat"`
now time.Time
}

// String is a Stringer interface implementation
// String is the `fmt.Stringer` interface implementation.
func (v *Vars) String() string {
arr, _ := yaml.Marshal(*v)
return string(arr)
}

// Process payload string
// Process processes the payload string.
func (v *Vars) Process(str string, ctx string) string {
compile := func(s string) string {
return v.processVariableBlock(s, ctx)
}
return re.ReplaceAllStringFunc(str, compile)
}

// compile single variable block
// compile a single variable block.
func (v *Vars) processVariableBlock(variable string, ctx string) string {
variable = strings.Trim(variable, "{{")
variable = strings.Trim(variable, "}}")
Expand Down Expand Up @@ -108,7 +108,7 @@ func (v *Vars) init() {
v.now = time.Now()
}

// returns file size
// returns the file size
func getFileSize(filePath string) string {
fi, err := os.Stat(filePath)
if err != nil {
Expand All @@ -117,29 +117,29 @@ func getFileSize(filePath string) string {
return strconv.FormatInt(fi.Size(), 10)
}

// returns file name
// returns the file name
func getFileName(filePath string) string {
p := strings.Split(filePath, ospkg.PathSeparator)
return p[len(p)-1]
}

// returns file extension
// returns the file extension
func getFileExtension(filePath string) string {
p := strings.Split(filePath, ".")
return p[len(p)-1]
}

// returns formatted time
// returns the formatted time
func (v *Vars) getTime() string {
return v.now.Format(v.TimeFormat)
}

// returns formatted date
// returns the formatted date
func (v *Vars) getDate() string {
return v.now.Format(v.DateFormat)
}

// returns formatted datetime
// returns the formatted datetime
func (v *Vars) getDateTime() string {
return v.now.Format(v.DateTimeFormat)
}

0 comments on commit 627047a

Please sign in to comment.