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 all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9b23625
Fix gci issues
olena-zmiiova Sep 4, 2024
0f5d606
Fix nlreturn issues
olena-zmiiova Sep 4, 2024
6235428
Remove unused function
olena-zmiiova Sep 4, 2024
37109cb
Add missed dot in comment
olena-zmiiova Sep 4, 2024
52793f4
Fixed unnecessary leading newlines
olena-zmiiova Sep 4, 2024
76a6b08
Fix magic numbers
olena-zmiiova Sep 4, 2024
550b3e1
Remove named return
olena-zmiiova Sep 4, 2024
a22ec0c
Remove underscores from var names, rename unused params, drop unneces…
olena-zmiiova Sep 4, 2024
4df5e17
Update code styles, disable linters in complex functions planned for …
olena-zmiiova Sep 4, 2024
f894f4d
Merge branch 'refs/heads/embed-code-go-add-linter' into go-fix-linter…
olena-zmiiova Sep 4, 2024
f153cd8
Use sha256 instead of sha1, convert int to uint32, enable linter run …
olena-zmiiova Sep 4, 2024
3603e5d
Fix uint32 format
olena-zmiiova Sep 5, 2024
a6d0d8b
Enable tests run in PR
olena-zmiiova Sep 5, 2024
c0cee0f
Merge branch 'refs/heads/embed-code-go-add-linter' into go-fix-linter…
olena-zmiiova Sep 5, 2024
c98f817
Break lines longer than 100 symbols
olena-zmiiova Sep 5, 2024
61515b4
Rename params
olena-zmiiova Sep 5, 2024
1988d12
Add TODO comments and issues
olena-zmiiova Sep 5, 2024
27dcf74
Add TODO comments for a magic number
olena-zmiiova Sep 5, 2024
1c3be2c
Remove unnecessary branch
olena-zmiiova Sep 5, 2024
564e5b6
Build embed_code.go for ubuntu-latest
github-actions[bot] Sep 5, 2024
03748c7
Build embed_code.go for windows-latest
github-actions[bot] Sep 5, 2024
7883f38
Build embed_code.go for macos-latest
github-actions[bot] Sep 5, 2024
5d9f3a3
Remove unnecessary branch
olena-zmiiova Sep 5, 2024
bf08114
Merge remote-tracking branch 'origin/go-fix-linter-issues' into go-fi…
olena-zmiiova Sep 5, 2024
c31db96
Remove blank line
olena-zmiiova Sep 5, 2024
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
28 changes: 17 additions & 11 deletions embed-code-go/analyzing/analyzing.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
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"
)

const analyticsDir = "./build/analytics"
const embeddingsNotFoundFile = "embeddings-not-found-files.txt"
const embeddingChangedFile = "embeddings-changed-files.txt"

// Represents read and write permissions for the owner of the file, and read-only permissions for group and others.
// Represents read and write permissions for the owner of the file, and read-only permissions
// for group and others.
const permission = 0755

// Analyzes all documentation files.
//
// If any error occurs during embedding, it is written to the analytics file with all the needed information.
// If any error occurs during embedding, it is written to the analytics file with all
// the needed information.
//
// config — a configuration for embedding.
func AnalyzeAll(config configuration.Configuration) {
docFiles := findDocumentationFiles(config)
changedEmbeddings, problemEmbeddings := extractAnalyticsForDocs(config, docFiles)

os.MkdirAll(analyticsDir, permission)
fragmentation.WriteLinesToFile(fmt.Sprintf("%s/%s", analyticsDir, embeddingChangedFile), changedEmbeddings)
fragmentation.WriteLinesToFile(fmt.Sprintf("%s/%s", analyticsDir, embeddingsNotFoundFile), problemEmbeddings)
fragmentation.WriteLinesToFile(
fmt.Sprintf("%s/%s", analyticsDir, embeddingChangedFile), changedEmbeddings)
fragmentation.WriteLinesToFile(
fmt.Sprintf("%s/%s", analyticsDir, embeddingsNotFoundFile), problemEmbeddings)
}

// Finds all documentation files for given config.
Expand All @@ -45,15 +50,15 @@ 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) {
config configuration.Configuration, docFiles []string) ([]string, []string) {
var changedEmbeddingsLines, problemEmbeddingsLines []string

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

return changedEmbeddingsLines, problemEmbeddingsLines
}
Binary file modified embed-code-go/bin/embed-code-macos
Binary file not shown.
Binary file modified embed-code-go/bin/embed-code-ubuntu
Binary file not shown.
Binary file modified embed-code-go/bin/embed-code-win.exe
Binary file not shown.
45 changes: 29 additions & 16 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 All @@ -38,7 +38,8 @@ import (
//
// DocsRoot — a path to a root directory with docs files.
//
// CodeIncludes — a string with comma-separated patterns for filtering the code files to be considered.
// CodeIncludes — a string with comma-separated patterns for filtering the code files
// to be considered.
// Directories are never matched by these patterns.
// For example, "**/*.java,**/*.gradle".
// The default value is "**/*.*".
Expand All @@ -50,8 +51,8 @@ import (
// For example, "docs/**/*.md,guides/*.html".
// The default value is "**/*.md,**/*.html".
//
// FragmentsDir — a directory where fragmented code is stored. A temporary directory that should not be
// tracked in VCS. The default value is: "./build/fragments".
// FragmentsDir — a directory where fragmented code is stored. A temporary directory that should
// not be tracked in VCS. The default value is: "./build/fragments".
//
// Separator — a string that's inserted between multiple partitions of a single fragment.
// The default value is "...".
Expand Down Expand Up @@ -124,10 +125,14 @@ func AnalyzeCodeSamples(config configuration.Configuration) {
func ReadArgs() Args {
codeRoot := flag.String("code_root", "", "a path to a root directory with code files")
docsRoot := flag.String("docs_root", "", "a path to a root directory with docs files")
codeIncludes := flag.String("code_includes", "", "a comma-separated string of glob patterns for code files to include")
docIncludes := flag.String("doc_includes", "", "a comma-separated string of glob patterns for docs files to include")
fragmentsDir := flag.String("fragments_dir", "", "a path to a directory where fragmented code is stored")
separator := flag.String("separator", "", "a string that's inserted between multiple partitions of a single fragment")
codeIncludes := flag.String("code_includes", "",
"a comma-separated string of glob patterns for code files to include")
docIncludes := flag.String("doc_includes", "",
"a comma-separated string of glob patterns for docs files to include")
fragmentsDir := flag.String("fragments_dir", "",
"a path to a directory where fragmented code is stored")
separator := flag.String("separator", "",
"a string that's inserted between multiple partitions of a single fragment")
configFilePath := flag.String("config_file_path", "", "a path to a yaml configuration file")
mode := flag.String("mode", "",
"a mode of embed-code execution, which can be 'check' or 'embed'")
Expand All @@ -146,10 +151,14 @@ func ReadArgs() Args {
}
}

// Checks the validity of provided userArgs and returns an error message if any of the validation rules are broken.
// If everything is ok, returns an empty string.
// Checks the validity of provided userArgs and returns an error message if any of the validation
// rules are broken. If everything is ok, returns an empty string.
//
// userArgs — a struct with user-provided args.
//
// TODO:2024-09-05:olena-zmiiova: Temporary disabling cyclop as this function is planned to
// be refactored. See https://github.com/SpineEventEngine/embed-code/issues/46
// nolint:cyclop
func Validate(userArgs Args) string {
isModeSet := userArgs.Mode != ""
isRootsSet := userArgs.CodeRoot != "" && userArgs.DocsRoot != ""
Expand All @@ -170,7 +179,8 @@ func Validate(userArgs Args) string {
return "If one of code_root and docs_root is set, the another one must be set as well."
}
if !(isRootsSet || isConfigSet) {
return "Embed code should be used with either config_file_path or both code_root and docs_root being set."
return "Embed code should be used with either config_file_path or both code_root and " +
"docs_root being set."
}

return validationMessage
Expand All @@ -196,6 +206,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 +232,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 +256,7 @@ func BuildEmbedCodeConfiguration(userArgs Args) configuration.Configuration {
if userArgs.Separator != "" {
embedCodeConfig.Separator = userArgs.Separator
}

return embedCodeConfig
}

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

return parsedArgs
}

Expand Down
10 changes: 7 additions & 3 deletions embed-code-go/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ var DefaultDocIncludes = []string{"**/*.md", "**/*.html"}

// The configuration for all the settings for the plugin to work.
//
// It is used to get data for scanning for and doc files, to receive fragments' dir and separator for partitions.
// 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
41 changes: 26 additions & 15 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 @@ -32,18 +33,20 @@ const (
// The entry point for embed-code.
//
// There are three modes, which are chosen by 'mode' arg. If it is set to 'check',
// then the checking for up-to-date is performed. If it is set to 'embed', the embedding is performed.
// If it is set to 'analyze', the analyzing is performed.
// then the checking for up-to-date is performed. If it is set to 'embed', the embedding is
// performed. If it is set to 'analyze', the analyzing is performed.
//
// 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.
//
// Checking for up-to-date 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 compared to the code which is already embedded into the docs;
// - for each tag, the code fragments are compared to the code which is already embedded
// into the docs;
// - if there is a difference, the error is reported.
//
// The 'mode' arg is required.
Expand All @@ -55,29 +58,35 @@ const (
//
// If both options are missed, the embedding fails.
// If both options are set, the embedding fails as well.
// If config file is not exists or does not contain 'code_root' and 'docs_root' fields, the embedding fails.
// If config file is not exists or does not contain 'code_root' and 'docs_root' fields, the
// embedding fails.
//
// All possible args:
// - code_root — a path to a root directory with code files;
// - docs_root — a path to a root directory with docs files;
// - config_file_path — a path to a yaml configuration file;
// - mode — string which represents the mode of embed-code execution. if it is set to 'check',
// then the checking for up-to-date is performed. If it is set to 'embed', the embedding is performed.
// then the checking for up-to-date is performed. If it is set to 'embed', the embedding
// is performed.
// If it is set to 'analyze', the analyzing is performed;
// - code_includes — a comma-separated string of glob patterns for code files to include. For example:
// - code_includes — a comma-separated string of glob patterns for code files to include.
// For example:
// "**/*.java,**/*.gradle". Default value is "**/*.*";
// - doc_includes — a comma-separated string of glob patterns for docs files to include. For example:
// - doc_includes — a comma-separated string of glob patterns for docs files to include.
// For example:
// "docs/**/*.md,guides/*.html". Default value is "**/*.md,**/*.html";
// - 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 "...".
// - 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 +95,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
}
Loading
Loading