Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a7e2329
[#1587] Exclude a common part of CLI parser
fivitti Jan 2, 2025
ac04f65
[#1587] Working general CLI parser
fivitti Jan 3, 2025
ffd2cf4
[#1587] Fix the wrong argument
fivitti Jan 7, 2025
8808118
[#1587] Unify agent and server parsers
fivitti Jan 7, 2025
e301a43
[#1587] Verify the envvars
fivitti Jan 7, 2025
30c4d21
[#1587] Fix linter issues
fivitti Jan 7, 2025
c8ca1c5
[#1587] Verify system environment variables
fivitti Jan 7, 2025
81b857a
[#1587] Add unit tests
fivitti Jan 7, 2025
48add73
[#1587] Extend unit test
fivitti Jan 7, 2025
06ba0b6
[#1587] Fix unit tests
fivitti Jan 7, 2025
cfb6ec4
[#1587] Simplify utility
fivitti Jan 7, 2025
7a29eb8
[#1587] Fix linter issue
fivitti Jan 8, 2025
522be1b
[#1587] Remove redundant flags
fivitti Jan 8, 2025
c6657d4
[#1587] Add unit tests
fivitti Jan 8, 2025
bf98f00
[#1587] Add a Changelog entry
fivitti Jan 8, 2025
19b7c94
[#1587] Unify the CLI handling in the Stork tool
fivitti Jan 8, 2025
66db02f
[#1587] Move package
fivitti Jan 8, 2025
425910d
[#1587] Exclude app to a separate file
fivitti Jan 8, 2025
7b201bd
[#1587] Unexport structs
fivitti Jan 8, 2025
6569020
[#1587] Unify code-gen CLI
fivitti Jan 8, 2025
27c6b7c
[#1587] Remove unnecessary dependencies
fivitti Jan 8, 2025
185123d
[#1587] Rename structs
fivitti Jan 9, 2025
f1d16f2
[#1587] Add unit tests
fivitti Jan 9, 2025
60f2fe4
[#1587] Rephrase a sentence
fivitti Jan 9, 2025
1acfd8d
[#1587] Support hooks only for agent and server
fivitti Jan 9, 2025
9285a3c
[#1587] Add unit test
fivitti Jan 9, 2025
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
Prev Previous commit
Next Next commit
[#1587] Unify code-gen CLI
  • Loading branch information
fivitti authored and tomaszmrugalski committed Jun 2, 2025
commit 6569020f689f3a849fc3d70434f856470f1333bf
96 changes: 26 additions & 70 deletions backend/cmd/stork-code-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,51 @@ import (
"os"

"github.com/jessevdk/go-flags"
"github.com/urfave/cli/v2"
"isc.org/stork"
"github.com/sirupsen/logrus"
"isc.org/stork/cli"
"isc.org/stork/codegen"
)

// The struct that must be embedded in all structures defining the command
// settings. It allows to recognize which command was specified in the CLI.
// It is related to how the go-flags library handles the subcommands.
//
// It may be also used to specify arguments of the command that accepts no
// arguments.
type cliCommand struct {
// It is true if the register command was specified. Otherwise, it is false.
commandSpecified bool
}

// Checks if the struct implement the library interface.
var _ flags.Commander = (*cliCommand)(nil)

// Implements the tools/golang/gopath/pkg/mod/github.com/jessevdk/[email protected]/command.go Commander interface.
// It is an only way to recognize which command was specified.
func (s *cliCommand) Execute(_ []string) error {
s.commandSpecified = true
return nil
}

// The CLI flags not related to any specific command.
type generalCommand struct {
cliCommand
// If true, the version of Stork is printed. It takes precedence
// over all other commands and arguments.
Version bool `short:"v" long:"version" description:"Show software version"`
}

type stdOptionDefinitionsCommand struct {
cliCommand
cli.CLICommand
Input string `short:"i" long:"input" description:"Path to the input file holding option definitions' specification." required:"true"`
Output string `short:"o" long:"output" description:"Path to the output file or 'stdout' to print the generated code in the terminal." required:"true"`
Template string `short:"t" long:"template" description:"Path to the template file used to generate the output file. The generated code is embedded in the template file."`
}

// Generates the code defining standard option definitions to stdout or
// to a file.
func generateStdOptionDefs(c *cli.Context) error {
func generateStdOptionDefs(settings *stdOptionDefinitionsCommand) error {
// Print the output to the stdout or to a file.
if c.String("output") == "stdout" {
return codegen.GenerateToStdout(c.String("input"), c.String("template"))
if settings.Output == "stdout" {
return codegen.GenerateToStdout(settings.Input, settings.Template)
}
return codegen.GenerateToFile(c.String("input"), c.String("template"), c.String("output"))
return codegen.GenerateToFile(settings.Input, settings.Template, settings.Output)
}

// Man function exposing command line parameters.
func main() {
app := &cli.App{
Name: "Stork Code Gen",
Usage: "Code generator used in Stork development",
Version: stork.Version,
HelpName: "stork-code-gen",
Flags: []cli.Flag{},
Commands: []*cli.Command{
{
Name: "std-option-defs",
Usage: "Generate standard option definitions from JSON spec.",
UsageText: "stork-code-gen std-option-defs",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "input",
Usage: "Path to the input file holding option definitions' specification.",
Required: true,
Aliases: []string{"i"},
},
&cli.StringFlag{
Name: "output",
Usage: "Path to the output file or 'stdout' to print the generated code in the terminal.",
Required: true,
Aliases: []string{"o"},
},
&cli.StringFlag{
Name: "template",
Usage: "Path to the template file used to generate the output file. The generated code is embedded in the template file.",
Aliases: []string{"t"},
},
},
Action: generateStdOptionDefs,
},
nothing := struct{}{}
parser := flags.NewParser(&nothing, flags.Default)
parser.Name = "Stork Code Gen"
parser.ShortDescription = "Code generator used in Stork development"
parser.Usage = "stork-code-gen [command] [options]"

app := cli.NewApp(parser)
stdOptionDefinitionsSettings := &stdOptionDefinitionsCommand{}
app.RegisterCommand(
"std-option-defs",
"Generate standard option definitions from JSON spec.",
stdOptionDefinitionsSettings,
func() {
err := generateStdOptionDefs(stdOptionDefinitionsSettings)
if err != nil {
logrus.WithError(err).Fatal("Failed to generate standard option definitions.")
}
},
}
)

err := app.Run(os.Args)
err := app.Run(os.Args[1:])
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions backend/cmd/stork-code-gen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TestMainHelp(t *testing.T) {
func TestStdOptionDefsHelp(t *testing.T) {
defer testutil.CreateOsArgsRestorePoint()()
os.Args = make([]string, 3)
os.Args[1] = "help"
os.Args[2] = "std-option-defs"
os.Args[1] = "std-option-defs"
os.Args[2] = "--help"

stdoutBytes, _, err := testutil.CaptureOutput(main)
require.NoError(t, err)
Expand Down