-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: carry config object to template to enable dynamic word wrap
⚠️ BREAKING CHANGES: - `.Metadata.title` replaced with `.Info.RecipeName`. - `.Metadata.ImageName` replaced with `.Info.ImageFileName`. - Removal of trailing whitespace from `cook.stepsSection`. - Addition of double new lines to `cook.sourceSection`. Other changes include: - Addition of `word-wrap` option to dynamically specify the word wrap length. - Addition of `.Info.ImageFilePath`, `.Info.NewRecipeFilePath`, and `.Info.RecipeFilePath` to template. - Addition of `.Config.*` to template. - Addition of config file aliases. E.g. `--word-wrap 80` from the command line can be used as `wordWrap: 80` in the config file.
- Loading branch information
1 parent
da4899e
commit b952e80
Showing
11 changed files
with
351 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/aquilax/cooklang-go" | ||
"github.com/nicholaswilde/cook-docs/pkg/cook" | ||
"github.com/nicholaswilde/cook-docs/pkg/document" | ||
"github.com/nicholaswilde/cook-docs/pkg/types" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func retrieveInfoAndPrintDocumentation(recipeSearchRoot string, recipePath string, templateFiles []string, waitGroup *sync.WaitGroup, config *types.Config) { | ||
defer waitGroup.Done() | ||
|
||
recipeInfo := cook.ParseRecipeInformation(recipePath) | ||
_, _ = cook.ParseFile(recipePath, config) | ||
recipeData, err := cooklang.ParseFile(recipeInfo.RecipePath) | ||
|
||
if err != nil { | ||
log.Warnf("Error parsing file for recipe %s, skipping: %s", recipeInfo.RecipePath, err) | ||
return | ||
} | ||
|
||
recipeData = cook.MergeRecipeData(recipeInfo, recipeData) | ||
|
||
document.PrintDocumentation(recipeSearchRoot, recipeData, recipeInfo, templateFiles, config) | ||
} | ||
|
||
func GetFullSearchRoot(searchRoot string) (string, error) { | ||
var fullSearchRoot string | ||
if path.IsAbs(searchRoot) { | ||
fullSearchRoot = searchRoot | ||
} else { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
fullSearchRoot = path.Join(cwd, searchRoot) | ||
} | ||
return fullSearchRoot, nil | ||
} | ||
|
||
func cookDocs(_ *cobra.Command, _ []string) { | ||
var config types.Config | ||
viper.Unmarshal(&config) | ||
log.Println(config) | ||
|
||
initializeCli(&config) | ||
|
||
recipeSearchRoot := config.RecipeSearchRoot | ||
|
||
fullSearchRoot, err := GetFullSearchRoot(recipeSearchRoot) | ||
if err != nil { | ||
log.Warnf("Error getting working directory: %s", err) | ||
return | ||
} | ||
|
||
recipePaths, err := cook.FindRecipePaths(fullSearchRoot) | ||
if err != nil { | ||
log.Errorf("Error finding recipe paths: %s", err) | ||
os.Exit(1) | ||
} | ||
log.Infof("Found recipes [%s]", strings.Join(recipePaths, ", ")) | ||
|
||
templateFiles := config.TemplateFiles | ||
log.Debugf("Rendering from optional template files [%s]", strings.Join(templateFiles, ", ")) | ||
|
||
waitGroup := sync.WaitGroup{} | ||
|
||
for _, r := range recipePaths { | ||
waitGroup.Add(1) | ||
|
||
// On dry runs all output goes to stdout, and so as to not jumble things, generate serially | ||
if config.DryRun { | ||
retrieveInfoAndPrintDocumentation(fullSearchRoot, r, templateFiles, &waitGroup, &config) | ||
} else { | ||
go retrieveInfoAndPrintDocumentation(fullSearchRoot, r, templateFiles, &waitGroup, &config) | ||
} | ||
} | ||
waitGroup.Wait() | ||
} | ||
|
||
func main() { | ||
command, err := newCookDocsCommand(cookDocs) | ||
if err != nil { | ||
log.Errorf("Failed to create the CLI commander: %s", err) | ||
os.Exit(1) | ||
} | ||
if err := command.Execute(); err != nil { | ||
log.Errorf("Failed to start the CLI: %s", err) | ||
os.Exit(1) | ||
} | ||
} | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/nicholaswilde/cook-docs/pkg/cook" | ||
"github.com/nicholaswilde/cook-docs/pkg/document" | ||
"github.com/nicholaswilde/cook-docs/pkg/types" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func retrieveInfoAndPrintDocumentation(recipePath string, waitGroup *sync.WaitGroup, config *types.Config) { | ||
defer waitGroup.Done() | ||
|
||
recipe, err := cook.ParseFile(recipePath, config) | ||
if err != nil { | ||
log.Warnf("Error parsing file for recipe %s, skipping: %s", recipePath, err) | ||
return | ||
} | ||
|
||
document.PrintDocumentation(recipe) | ||
} | ||
|
||
func GetFullSearchRoot(searchRoot string) (string, error) { | ||
var fullSearchRoot string | ||
if path.IsAbs(searchRoot) { | ||
fullSearchRoot = searchRoot | ||
} else { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
fullSearchRoot = path.Join(cwd, searchRoot) | ||
} | ||
return fullSearchRoot, nil | ||
} | ||
|
||
func cookDocs(_ *cobra.Command, _ []string) { | ||
var config types.Config | ||
viper.Unmarshal(&config) | ||
|
||
initializeCli(&config) | ||
|
||
fullSearchRoot, err := GetFullSearchRoot(config.RecipeSearchRoot) | ||
if err != nil { | ||
log.Warnf("Error getting working directory: %s", err) | ||
return | ||
} | ||
|
||
recipePaths, err := cook.FindRecipeFilePaths(fullSearchRoot) | ||
if err != nil { | ||
log.Errorf("Error finding recipe paths: %s", err) | ||
os.Exit(1) | ||
} | ||
log.Infof("Found recipes [%s]", strings.Join(recipePaths, ", ")) | ||
|
||
log.Debugf("Rendering from optional template files [%s]", strings.Join(config.TemplateFiles, ", ")) | ||
|
||
waitGroup := sync.WaitGroup{} | ||
|
||
for _, r := range recipePaths { | ||
waitGroup.Add(1) | ||
|
||
// On dry runs all output goes to stdout, and so as to not jumble things, generate serially | ||
if config.DryRun { | ||
retrieveInfoAndPrintDocumentation(r, &waitGroup, &config) | ||
} else { | ||
go retrieveInfoAndPrintDocumentation(r, &waitGroup, &config) | ||
} | ||
} | ||
waitGroup.Wait() | ||
} | ||
|
||
func main() { | ||
command, err := newCookDocsCommand(cookDocs) | ||
if err != nil { | ||
log.Errorf("Failed to create the CLI commander: %s", err) | ||
os.Exit(1) | ||
} | ||
if err := command.Execute(); err != nil { | ||
log.Errorf("Failed to start the CLI: %s", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.