Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 45 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ package cli
import (
_type "embed-code/embed-code-go/type"
"flag"
"fmt"
"log/slog"
"os"
"strings"

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

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -152,6 +155,10 @@ func ReadArgs() Config {
// Returns filled Config.
func FillArgsFromConfigFile(args Config) (Config, error) {
configFields := readConfigFields(args.ConfigPath)
slog.Info(fmt.Sprintf(
"Loaded config file `%s`. Found %d embedding setup(s).",
logging.FileReference(args.ConfigPath), len(configFields.Embeddings),
))
args.BaseDocsPath = configFields.BaseDocsPath
args.BaseCodePaths = configFields.BaseCodePaths

Expand Down Expand Up @@ -181,6 +188,11 @@ func BuildEmbedCodeConfiguration(userArgs Config) []configuration.Configuration

if len(userArgs.Embeddings) > 0 {
for _, embedding := range userArgs.Embeddings {
slog.Info(fmt.Sprintf(
"Processing the `%s` embedding setup. Documentation folder: `%s`. %s.",
embedding.Name, logging.FileReference(embedding.DocsPath),
sourceFoldersLabel(embedding.CodePaths),
))
embedCodeConfigs = append(embedCodeConfigs, configFromEmbedding(embedding))
}
return embedCodeConfigs
Expand All @@ -189,13 +201,17 @@ func BuildEmbedCodeConfiguration(userArgs Config) []configuration.Configuration
embedCodeConfig := configWithOptionalParams(userArgs)
embedCodeConfig.CodeRoots = userArgs.BaseCodePaths
embedCodeConfig.DocumentationRoot = userArgs.BaseDocsPath
slog.Info(fmt.Sprintf(
"Preparing command line settings. Documentation folder: `%s`. %s.",
logging.FileReference(userArgs.BaseDocsPath), sourceFoldersLabel(userArgs.BaseCodePaths),
))

embedCodeConfigs = append(embedCodeConfigs, embedCodeConfig)

return embedCodeConfigs
}

// Creates a new Configuration from one complete embedding config.
// configFromEmbedding creates a new Configuration from one complete embedding config.
func configFromEmbedding(embedding EmbeddingConfig) configuration.Configuration {
embedCodeConfig := configuration.NewConfiguration()
embedCodeConfig.Name = embedding.Name
Expand All @@ -215,21 +231,46 @@ func configFromEmbedding(embedding EmbeddingConfig) configuration.Configuration
return embedCodeConfig
}

// Creates a new Configuration with the filled optional properties from the user args.
// configWithOptionalParams creates a new Configuration with optional properties from user args.
func configWithOptionalParams(userArgs Config) configuration.Configuration {
embedCodeConfig := configuration.NewConfiguration()

if len(userArgs.DocIncludes) > 0 {
embedCodeConfig.DocIncludes = userArgs.DocIncludes
}
if len(userArgs.DocExcludes) > 0 {
embedCodeConfig.DocExcludes = userArgs.DocExcludes
}
if isNotEmpty(userArgs.Separator) {
embedCodeConfig.Separator = userArgs.Separator
}

return embedCodeConfig
}

// Returns a list of strings from given comma-separated string listArgument.
// sourceFoldersLabel formats source folders for human-readable log messages.
func sourceFoldersLabel(paths _type.NamedPathList) string {
if len(paths) == 0 {
return "No source code folders configured"
}

var labels []string
for _, path := range paths {
label := fmt.Sprintf("`%s`", logging.FileReference(path.Path))
if strings.TrimSpace(path.Name) != "" {
label = fmt.Sprintf("`%s` as `%s`", logging.FileReference(path.Path), path.Name)
}
labels = append(labels, label)
}

if len(labels) == 1 {
return "Source code folder: " + labels[0]
}

return "Source code folders: " + strings.Join(labels, ", ")
}

// parseListArgument returns a list of strings from given comma-separated string listArgument.
func parseListArgument(listArgument string) []string {
splitArgs := strings.Split(listArgument, ",")
parsedArgs := make([]string, 0)
Expand All @@ -242,7 +283,7 @@ func parseListArgument(listArgument string) []string {
return parsedArgs
}

// Reads the file from provided configFilePath and returns a ConfigFields struct.
// readConfigFields reads the provided config file and returns parsed fields.
//
// configFilePath — a path to a yaml configuration file.
//
Expand Down
10 changes: 10 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ var _ = Describe("CLI validation", func() {
Expect(embedConfigs[2].Separator).To(Equal("---"))
})

It("should copy command line doc excludes to the runtime config", func() {
config := baseCliConfig()
config.DocExcludes = []string{"old-docs/**/*.md", "drafts/**/*"}

embedConfigs := cli.BuildEmbedCodeConfiguration(config)

Expect(embedConfigs).To(HaveLen(1))
Expect(embedConfigs[0].DocExcludes).To(Equal([]string(config.DocExcludes)))
})

})

})
Expand Down
49 changes: 46 additions & 3 deletions embedding/parsing/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package parsing

import (
"fmt"
"log/slog"
"strings"

"embed-code/embed-code-go/configuration"
"embed-code/embed-code-go/embedding/commentfilter"
Expand Down Expand Up @@ -158,16 +160,22 @@ func (e Instruction) Content() ([]string, error) {
if err != nil {
return nil, err
}
codeFileReference, referenceErr := fragmentation.ResolveCodeFileReference(
e.CodeFile,
e.Configuration,
)
if e.StartPattern != nil || e.EndPattern != nil || e.LinePattern != nil {
codeFileReference, err := fragmentation.ResolveCodeFileReference(e.CodeFile, e.Configuration)
if err != nil {
return nil, err
if referenceErr != nil {
return nil, referenceErr
}
fileContent, err = e.matchingLines(fileContent, codeFileReference)
if err != nil {
return nil, err
}
}
if referenceErr == nil {
slog.Info(e.contentLogMessage(codeFileReference))
}

return commentfilter.Filter(
fileContent,
Expand All @@ -178,6 +186,41 @@ func (e Instruction) Content() ([]string, error) {
), nil
}

// contentLogMessage describes the source content selected by this instruction.
func (e Instruction) contentLogMessage(codeFileReference string) string {
switch {
case e.Fragment != "":
return fmt.Sprintf("Extracted fragment `%s` from `%s`.",
e.Fragment, codeFileReference)
case e.LinePattern != nil:
return fmt.Sprintf("Extracted line-pattern embedding from `%s` using %s.",
codeFileReference, patternLabel("line", e.LinePattern))
case e.StartPattern != nil || e.EndPattern != nil:
return fmt.Sprintf("Extracted start/end-pattern embedding from `%s` using %s.",
codeFileReference, rangePatternLabel(e.StartPattern, e.EndPattern))
default:
return fmt.Sprintf("Extracted source file `%s`.", codeFileReference)
}
}

// rangePatternLabel formats the start and end patterns set on an instruction.
func rangePatternLabel(start *Pattern, end *Pattern) string {
var labels []string
if start != nil {
labels = append(labels, patternLabel("start", start))
}
if end != nil {
labels = append(labels, patternLabel("end", end))
}

return strings.Join(labels, " and ")
}

// patternLabel formats a pattern for human-readable logs.
func patternLabel(kind string, pattern *Pattern) string {
return fmt.Sprintf("%s pattern `%s`", kind, pattern.sourceGlob)
}

// Returns string representation of Instruction.
func (e Instruction) String() string {
return fmt.Sprintf(
Expand Down
Loading
Loading