Skip to content

Commit

Permalink
Change transform config to pathorcontent (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode authored Jun 15, 2021
1 parent 2e71ff4 commit 46cac5d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,12 @@ func validateAnchorDir(anchorDir string, files []string) (_ string, err error) {

func registerTransform(_ context.Context, app *extkingpin.App) {
cmd := app.Command("transform", "Transform markdown files in various ways. For example pre process markdown files to allow it for use for popular static HTML websites based on markdown source code and front matter options.")
cfg := cmd.Flag("config", "Path to the YAML file with spec defined in github.com/bwplotka/mdox/pkg/transform.Config").
Short('c').Default(".mdox.yaml").ExistingFile()
cfg := extflag.RegisterPathOrContent(cmd, "config", "Path to the YAML file with spec defined in github.com/bwplotka/mdox/pkg/transform.Config", extflag.WithEnvSubstitution())
cmd.Run(func(ctx context.Context, logger log.Logger) error {
return transform.Dir(ctx, logger, *cfg)
validateConfig, err := cfg.Content()
if err != nil {
return err
}
return transform.Dir(ctx, logger, validateConfig)
})
}
13 changes: 0 additions & 13 deletions pkg/transform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package transform

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -116,18 +115,6 @@ type FrontMatterConfig struct {
Template string
}

func parseConfigFile(configFile string) (Config, error) {
configFile, err := filepath.Abs(configFile)
if err != nil {
return Config{}, errors.Wrap(err, "abs")
}
c, err := ioutil.ReadFile(configFile)
if err != nil {
return Config{}, errors.Wrap(err, "read config file")
}
return ParseConfig(c)
}

func ParseConfig(c []byte) (Config, error) {
cfg := Config{}
dec := yaml.NewDecoder(bytes.NewReader(c))
Expand Down
4 changes: 2 additions & 2 deletions pkg/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func prepOutputDir(d string, gitIgnored bool) error {
}

// Dir transforms directory using given configuration file.
func Dir(ctx context.Context, logger log.Logger, configFile string) error {
c, err := parseConfigFile(configFile)
func Dir(ctx context.Context, logger log.Logger, config []byte) error {
c, err := ParseConfig(config)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/transform/transform_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ func TestTransform(t *testing.T) {

testutil.Ok(t, os.RemoveAll(tmpDir))
t.Run("mdox1.yaml", func(t *testing.T) {
testutil.Ok(t, transform.Dir(context.Background(), logger, filepath.Join(testData, "mdox1.yaml")))
mdox1, err := ioutil.ReadFile(filepath.Join(testData, "mdox1.yaml"))
testutil.Ok(t, err)
testutil.Ok(t, transform.Dir(context.Background(), logger, mdox1))
assertDirContent(t, filepath.Join(testData, "expected", "test1"), filepath.Join(tmpDir, "test1"))
})
t.Run("mdox2.yaml", func(t *testing.T) {
testutil.Ok(t, transform.Dir(context.Background(), logger, filepath.Join(testData, "mdox2.yaml")))
mdox2, err := ioutil.ReadFile(filepath.Join(testData, "mdox2.yaml"))
testutil.Ok(t, err)
testutil.Ok(t, transform.Dir(context.Background(), logger, mdox2))
assertDirContent(t, filepath.Join(testData, "expected", "test2"), filepath.Join(tmpDir, "test2"))

})
Expand Down

0 comments on commit 46cac5d

Please sign in to comment.