-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package variant | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newExportFlattened(r *Runner) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "flattened SRC_DIR DST_DIR", | ||
Short: "Process all the imports and export the \"flattened\" variant command to DST_DIR", | ||
Example: `$ variant export embedded examples/simple flattened | ||
$ cd flattened | ||
$ variant run -h | ||
`, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(c *cobra.Command, args []string) error { | ||
err := r.ap.ExportFlattened(args[0], args[1]) | ||
if err != nil { | ||
c.SilenceUsage = true | ||
} | ||
return err | ||
}, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/hashicorp/hcl/v2/ext/typeexpr" | ||
"github.com/mumoshu/variant2/pkg/conf" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func (app *App) ExportFlattened(srcDir, dstDir string) error { | ||
if err := os.MkdirAll(dstDir, 0755); err != nil { | ||
return err | ||
} | ||
|
||
a, err := New(FromDir(srcDir)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f := hclwrite.NewEmptyFile() | ||
|
||
{ | ||
rootBody := f.Body() | ||
|
||
for _, j := range a.JobByName { | ||
if j.Name == "" { | ||
continue | ||
} | ||
rootBody.AppendNewline() | ||
jobBlock := rootBody.AppendNewBlock("job", []string{j.Name}) | ||
jobBody := jobBlock.Body() | ||
|
||
for i, o := range j.Options { | ||
if i != 0 { | ||
jobBody.AppendNewline() | ||
} | ||
optBlock := jobBody.AppendNewBlock("option", []string{o.Name}) | ||
tpe, diagnostics := typeexpr.TypeConstraint(o.Type) | ||
if diagnostics.HasErrors() { | ||
return diagnostics | ||
} | ||
v := typeexpr.TypeConstraintVal(tpe) | ||
optBlock.Body().SetAttributeValue("type", v) | ||
} | ||
|
||
for i, p := range j.Parameters { | ||
if i != 0 { | ||
jobBody.AppendNewline() | ||
} | ||
paramBlock := jobBody.AppendNewBlock("parameter", []string{p.Name}) | ||
tpe, diagnostics := typeexpr.TypeConstraint(p.Type) | ||
if diagnostics.HasErrors() { | ||
return diagnostics | ||
} | ||
v := typeexpr.TypeConstraintVal(tpe) | ||
paramBlock.Body().SetAttributeValue("type", v) | ||
} | ||
} | ||
} | ||
|
||
tokens := f.BuildTokens(nil) | ||
raw := tokens.Bytes() | ||
formatted := hclwrite.Format(raw) | ||
|
||
binName := filepath.Base(dstDir) | ||
|
||
cfgPath := filepath.Join(dstDir, binName+conf.VariantFileExt) | ||
|
||
if err := ioutil.WriteFile(cfgPath, formatted, 0644); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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