-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(render): add new command to render final file
- Loading branch information
Showing
29 changed files
with
671 additions
and
79 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
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,24 +1,17 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// | ||
// | ||
// Define the CLI data for the file sub-command | ||
// | ||
// | ||
|
||
func newAddFileCmd() *cobra.Command { | ||
addFileCmd := &cobra.Command{ | ||
Use: "file [sub-command]...", | ||
Use: "file", | ||
Short: "Sub-command to host the decK file manipulation operations", | ||
Long: `Sub-command to host the decK file manipulation operations`, | ||
} | ||
|
||
addFileCmd.AddCommand(newFileRenderCmd()) | ||
|
||
return addFileCmd | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/blang/semver/v4" | ||
"github.com/kong/deck/file" | ||
"github.com/kong/deck/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
fileRenderCmdKongStateFile []string | ||
fileRenderCmdKongFileOutput string | ||
fileRenderCmdStateFormat string | ||
) | ||
|
||
func newFileRenderCmd() *cobra.Command { | ||
renderCmd := &cobra.Command{ | ||
Use: "render", | ||
Short: "Render the configuration as Kong declarative config", | ||
Long: ``, | ||
Args: validateNoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return render(cmd.Context(), fileRenderCmdKongStateFile, fileRenderCmdStateFormat) | ||
}, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if len(fileRenderCmdStateFormat) == 0 { | ||
return fmt.Errorf("a state file with Kong's configuration " + | ||
"must be specified using `-s`/`--state` flag") | ||
} | ||
return preRunSilenceEventsFlag() | ||
}, | ||
} | ||
|
||
renderCmd.Flags().StringSliceVarP(&fileRenderCmdKongStateFile, | ||
"state", "s", []string{"-"}, "file(s) containing Kong's configuration.\n"+ | ||
"This flag can be specified multiple times for multiple files.\n"+ | ||
"Use `-` to read from stdin.") | ||
renderCmd.Flags().StringVarP(&fileRenderCmdKongFileOutput, "output-file", "o", | ||
"-", "file to which to write Kong's configuration."+ | ||
"Use `-` to write to stdout.") | ||
renderCmd.Flags().StringVar(&fileRenderCmdStateFormat, "format", | ||
"yaml", "output file format: json or yaml.") | ||
|
||
return renderCmd | ||
} | ||
|
||
func render(ctx context.Context, filenames []string, format string) error { | ||
targetContent, err := file.GetContentFromFiles(filenames, true) | ||
if err != nil { | ||
return err | ||
} | ||
s, _ := state.NewKongState() | ||
rawState, err := file.Get(ctx, targetContent, file.RenderConfig{ | ||
CurrentState: s, | ||
KongVersion: semver.Version{Major: 3, Minor: 0}, | ||
}, dumpConfig, nil) | ||
if err != nil { | ||
return err | ||
} | ||
targetState, err := state.Get(rawState) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return file.KongStateToFile(targetState, file.WriteConfig{ | ||
Filename: fileRenderCmdKongFileOutput, | ||
FileFormat: file.Format(strings.ToUpper(format)), | ||
KongVersion: "3.0.0", | ||
}) | ||
} |
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
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 @@ | ||
output.yaml |
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.