Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues found by golangci-linter #45

Merged
merged 25 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci_linter_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
pull_request:
branches:
- embed-code-go
# Temporary change to enable linters on PR #45. Will be removed before merge.
- embed-code-go-add-linter

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/go_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
pull_request:
branches:
- embed-code-go
# Temporary change to run tests on PR #45. Will be removed before merge.
- embed-code-go-add-linter

jobs:
build:
Expand Down
17 changes: 9 additions & 8 deletions embed-code-go/analyzing/analyzing.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package analyzing

import (
"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding"
"embed-code/embed-code-go/fragmentation"
"fmt"
"os"
"strings"

"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding"
"embed-code/embed-code-go/fragmentation"

"github.com/bmatcuk/doublestar/v4"
)

Expand Down Expand Up @@ -45,15 +46,14 @@ func findDocumentationFiles(config configuration.Configuration) []string {
}
documentationFiles = append(documentationFiles, matches...)
}

return documentationFiles
}

// Returns a list of embeddings that are not up-to-date with their code files.
// Also returns a list of embeddings which cause an error.
func extractAnalyticsForDocs(
config configuration.Configuration,
docFiles []string,
) (changedEmbeddingsLines []string, problemEmbeddingsLines []string) {
func extractAnalyticsForDocs(config configuration.Configuration, docFiles []string) ([]string, []string) {
var changedEmbeddingsLines, problemEmbeddingsLines []string

for _, docFile := range docFiles {
processor := embedding.NewEmbeddingProcessor(docFile, config)
Expand All @@ -72,5 +72,6 @@ func extractAnalyticsForDocs(
}
}
}
return

return changedEmbeddingsLines, problemEmbeddingsLines
}
17 changes: 11 additions & 6 deletions embed-code-go/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
package cli

import (
"embed-code/embed-code-go/analyzing"
"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding"
"embed-code/embed-code-go/fragmentation"
"flag"
"fmt"
"os"
"strings"

"os"
"embed-code/embed-code-go/analyzing"
"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding"
"embed-code/embed-code-go/fragmentation"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -150,6 +150,8 @@ func ReadArgs() Args {
// If everything is ok, returns an empty string.
//
// userArgs — a struct with user-provided args.
// Temporary disabling cyclop as this function is planned to be refactored.
// nolint:cyclop
olena-zmiiova marked this conversation as resolved.
Show resolved Hide resolved
func Validate(userArgs Args) string {
isModeSet := userArgs.Mode != ""
isRootsSet := userArgs.CodeRoot != "" && userArgs.DocsRoot != ""
Expand Down Expand Up @@ -196,6 +198,7 @@ func ValidateConfigFile(configFilePath string) string {
if configFields.CodeRoot == "" || configFields.DocsRoot == "" {
return "Config must include both code_root and docs_root fields."
}

return validationMessage
}

Expand All @@ -221,14 +224,14 @@ func FillArgsFromConfigFile(args Args) Args {
if configFields.Separator != "" {
args.Separator = configFields.Separator
}

return args
}

// Generates and returns a configuration based on provided userArgs.
//
// userArgs — a struct with user-provided args.
func BuildEmbedCodeConfiguration(userArgs Args) configuration.Configuration {

embedCodeConfig := configuration.NewConfiguration()
embedCodeConfig.CodeRoot = userArgs.CodeRoot
embedCodeConfig.DocumentationRoot = userArgs.DocsRoot
Expand All @@ -245,6 +248,7 @@ func BuildEmbedCodeConfiguration(userArgs Args) configuration.Configuration {
if userArgs.Separator != "" {
embedCodeConfig.Separator = userArgs.Separator
}

return embedCodeConfig
}

Expand All @@ -261,6 +265,7 @@ func parseListArgument(listArgument string) []string {
parsedArgs = append(parsedArgs, trimmed)
}
}

return parsedArgs
}

Expand Down
7 changes: 5 additions & 2 deletions embed-code-go/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ var DefaultDocIncludes = []string{"**/*.md", "**/*.html"}
//
// It is used to get data for scanning for and doc files, to receive fragments' dir and separator for partitions.
// The example of creating the Configuration with default values:
// var config = configuration.NewConfiguration()
//
// var config = configuration.NewConfiguration()
//
// If there's need to modify the default configuration,
// it can be done with just setting values to corresponding fields:
// config.FragmentsDir = "foo/bar"
//
// config.FragmentsDir = "foo/bar"
type Configuration struct {
// A root directory of the source code to be embedded.
CodeRoot string
Expand Down
16 changes: 10 additions & 6 deletions embed-code-go/embed_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package main

import (
"embed-code/embed-code-go/cli"
"fmt"

"embed-code/embed-code-go/cli"
)

const (
Expand All @@ -38,7 +39,8 @@ const (
// Embedding is the process that consists of the following steps:
// - the code fragments are extracted from the code files;
// - the docs files are scanned for <embed-code> tags;
// - for each tag, the code fragments are embedded into the docs. The embedding is parametrized with the tag attributes.
// - for each tag, the code fragments are embedded into the docs. The embedding is parametrized with
// the tag attributes.
olena-zmiiova marked this conversation as resolved.
Show resolved Hide resolved
//
// Checking for up-to-date is the process that consists of the following steps:
// - the code fragments are extracted from the code files;
Expand Down Expand Up @@ -71,13 +73,13 @@ const (
// - fragments_dir — a path to a directory with code fragments. Default value is "./build/fragments";
// - separator — a string which is used as a separator between code fragments. Default value is "...".
func main() {

userArgs := cli.ReadArgs()

validationMessage := cli.Validate(userArgs)
if validationMessage != "" {
fmt.Println("Validation error:")
fmt.Println(validationMessage)

return
}

Expand All @@ -86,19 +88,21 @@ func main() {
if validationMessage != "" {
fmt.Println("Configuration file validation error:")
fmt.Println(validationMessage)

return
}
userArgs = cli.FillArgsFromConfigFile(userArgs)
}

config := cli.BuildEmbedCodeConfiguration(userArgs)

if userArgs.Mode == ModeCheck {
switch userArgs.Mode {
case ModeCheck:
cli.CheckCodeSamples(config)
} else if userArgs.Mode == ModeEmbed {
case ModeEmbed:
cli.EmbedCodeSamples(config)
cli.CheckCodeSamples(config)
} else if userArgs.Mode == ModeAnalyze {
case ModeAnalyze:
cli.AnalyzeCodeSamples(config)
}
}
4 changes: 3 additions & 1 deletion embed-code-go/embedding/embedding_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package embedding

import (
"embed-code/embed-code-go/embedding/parsing"
"fmt"

"embed-code/embed-code-go/embedding/parsing"
)

// Describes an error which occurs if something goes wrong during embedding.
Expand Down Expand Up @@ -52,5 +53,6 @@ func (err EmbeddingError) Error() string {
}
errorString += unacceptedEmbbeddingsStr
}

return errorString
}
19 changes: 12 additions & 7 deletions embed-code-go/embedding/embedding_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
package embedding

import (
"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding/parsing"
"embed-code/embed-code-go/embedding_instruction"
"errors"
"fmt"
"os"
"strings"

"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding/parsing"
"embed-code/embed-code-go/embedding_instruction"

"github.com/bmatcuk/doublestar/v4"
)

// Represents read, write, and execute permissions for owner, while allowing the group and others to read and execute it.
// Represents read, write, and execute permissions for owner, while allowing the group and others
// to read and execute it.
const filePermission = 0755

// The EmbeddingProcessor entity processes a single documentation file and embeds code snippets
Expand Down Expand Up @@ -89,6 +91,7 @@ func (ep EmbeddingProcessor) Embed() error {
return EmbeddingError{Context: context}
}
}

return nil
}

Expand All @@ -100,9 +103,9 @@ func (ep EmbeddingProcessor) FindChangedEmbeddings() ([]embedding_instruction.Em
changedEmbeddings := context.FindChangedEmbeddings()
if err != nil {
return changedEmbeddings, EmbeddingError{Context: context}
} else {
return changedEmbeddings, nil
}

return changedEmbeddings, nil
}

// Reports whether the embedding of the target markdown is up-to-date with the code file.
Expand All @@ -111,6 +114,7 @@ func (ep EmbeddingProcessor) IsUpToDate() bool {
if err != nil {
panic(err)
}

return !context.IsContentChanged()
}

Expand Down Expand Up @@ -146,6 +150,7 @@ func (ep EmbeddingProcessor) constructEmbedding() (parsing.ParsingContext, error
isErrorFaced = true
}
accepted = true

break
}
}
Expand All @@ -156,7 +161,7 @@ func (ep EmbeddingProcessor) constructEmbedding() (parsing.ParsingContext, error
}
}

var err error = nil
var err error
if isErrorFaced {
err = constructEmbeddingError
}
Expand Down
7 changes: 5 additions & 2 deletions embed-code-go/embedding/parsing/blank_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package parsing

import (
"embed-code/embed-code-go/configuration"
"strings"

"embed-code/embed-code-go/configuration"
)

// Represents a blank line of a markdown.
Expand All @@ -35,6 +36,7 @@ func (b BlankLine) Recognize(context ParsingContext) bool {
if !context.ReachedEOF() && strings.TrimSpace(context.CurrentLine()) == "" {
return !context.CodeFenceStarted && context.Embedding != nil
}

return false
}

Expand All @@ -43,9 +45,10 @@ func (b BlankLine) Recognize(context ParsingContext) bool {
// Appends the current line of the context to the result, and moves to the next line.
//
// This implementation never returns an error.
func (b BlankLine) Accept(context *ParsingContext, config configuration.Configuration) error {
func (b BlankLine) Accept(context *ParsingContext, _ configuration.Configuration) error {
line := context.CurrentLine()
context.Result = append(context.Result, line)
context.ToNextLine()

return nil
}
9 changes: 7 additions & 2 deletions embed-code-go/embedding/parsing/code_fence_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package parsing

import (
"embed-code/embed-code-go/configuration"
"strings"

"embed-code/embed-code-go/configuration"
)

//
Expand All @@ -41,8 +42,10 @@ type CodeFenceEnd struct{}
func (c CodeFenceEnd) Recognize(context ParsingContext) bool {
if !context.ReachedEOF() {
indentation := strings.Repeat(" ", context.CodeFenceIndentation)

return context.CodeFenceStarted && strings.HasPrefix(context.CurrentLine(), indentation+"```")
}

return false
}

Expand All @@ -54,7 +57,7 @@ func (c CodeFenceEnd) Recognize(context ParsingContext) bool {
// config — a configuration of the embedding.
//
// Returns an error if the rendering was not successful.
func (c CodeFenceEnd) Accept(context *ParsingContext, config configuration.Configuration) error {
func (c CodeFenceEnd) Accept(context *ParsingContext, _ configuration.Configuration) error {
line := context.CurrentLine()
err := renderSample(context)
context.SetEmbedding(nil)
Expand All @@ -66,6 +69,7 @@ func (c CodeFenceEnd) Accept(context *ParsingContext, config configuration.Confi
context.CodeFenceStarted = false
context.CodeFenceIndentation = 0
context.ToNextLine()

return err
}

Expand All @@ -87,5 +91,6 @@ func renderSample(context *ParsingContext) error {
indentation := strings.Repeat(" ", context.CodeFenceIndentation)
context.Result = append(context.Result, indentation+line)
}

return nil
}
Loading
Loading