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] Rename structs
  • Loading branch information
fivitti authored and tomaszmrugalski committed Jun 2, 2025
commit 185123d2a83c9e93c28ac9f7e78f85187f8f96f0
8 changes: 4 additions & 4 deletions backend/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ type command interface {
//
// It may be also used to specify arguments of the command that accepts no
// arguments.
type CLICommand struct {
type CommandSettings 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)
var _ flags.Commander = (*CommandSettings)(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 {
func (s *CommandSettings) Execute(_ []string) error {
s.commandSpecified = true
return nil
}

// Indicates if the command was specified.
func (s *CLICommand) isSpecified() bool {
func (s *CommandSettings) isSpecified() bool {
return s.commandSpecified
}

Expand Down
8 changes: 4 additions & 4 deletions backend/cmd/stork-code-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"isc.org/stork/codegen"
)

type stdOptionDefinitionsCommand struct {
cli.CLICommand
type stdOptionDefinitionsSettings struct {
cli.CommandSettings
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(settings *stdOptionDefinitionsCommand) error {
func generateStdOptionDefs(settings *stdOptionDefinitionsSettings) error {
// Print the output to the stdout or to a file.
if settings.Output == "stdout" {
return codegen.GenerateToStdout(settings.Input, settings.Template)
Expand All @@ -36,7 +36,7 @@ func main() {
parser.Usage = "stork-code-gen [command] [options]"

app := cli.NewApp(parser)
stdOptionDefinitionsSettings := &stdOptionDefinitionsCommand{}
stdOptionDefinitionsSettings := &stdOptionDefinitionsSettings{}
app.RegisterCommand(
"std-option-defs",
"Generate standard option definitions from JSON spec.",
Expand Down
136 changes: 68 additions & 68 deletions backend/cmd/stork-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,57 @@ import (
const passwordGenRandomLength = 24

// The CLI flags for the db-create command.
type databaseCreateCommand struct {
cli.CLICommand
type databaseCreateSettings struct {
cli.CommandSettings
DatabaseSettings dbops.DatabaseCLIFlagsWithMaintenance
Force bool `long:"force" short:"f" description:"Recreate the database and the user if they exist" env:"STORK_TOOL_DB_FORCE"`
}

// The CLI flags for the db-init, db-up, db-down, db-reset, db-version commands.
type databaseCommand struct {
cli.CLICommand
type databaseSettings struct {
cli.CommandSettings
DatabaseSettings dbops.DatabaseCLIFlags
}

// The CLI flags for the db-up, db-down, and db-set-version commands.
type databaseVersionCommand struct {
cli.CLICommand
type databaseVersionSettings struct {
cli.CommandSettings
DatabaseSettings dbops.DatabaseCLIFlags
Version string `long:"version" short:"t" description:"Target database schema version (optional)" env:"STORK_TOOL_DB_VERSION"`
}

// The CLI flags for the cert-import command.
type certificateImportCommand struct {
cli.CLICommand
type certificateImportSettings struct {
cli.CommandSettings
DatabaseSettings dbops.DatabaseCLIFlags
Object string `long:"object" short:"f" description:"The object to import; it can be one of 'cakey', 'cacert', 'srvkey', 'srvcert', 'srvtkn'" env:"STORK_TOOL_CERT_OBJECT" choice:"cakey" choice:"cacert" choice:"srvkey" choice:"srvcert" choice:"srvtkn"`
File string `long:"file" short:"i" description:"The file location from which the object should be imported" env:"STORK_TOOL_CERT_FILE"`
}

// The CLI flags for the cert-export command.
type certificateExportCommand struct {
cli.CLICommand
type certificateExportSettings struct {
cli.CommandSettings
DatabaseSettings dbops.DatabaseCLIFlags
Object string `long:"object" short:"f" description:"The object to dump; it can be one of 'cakey', 'cacert', 'srvkey', 'srvcert', 'srvtkn'" env:"STORK_TOOL_CERT_OBJECT" choice:"cakey" choice:"cacert" choice:"srvkey" choice:"srvcert" choice:"srvtkn"`
File string `long:"file" short:"o" description:"The file location where the object should be saved; if not provided, then object is printed to stdout" env:"STORK_TOOL_CERT_FILE"`
}

// The CLI flags for the hook-inspect command.
type hookInspectCommand struct {
cli.CLICommand
type hookInspectSettings struct {
cli.CommandSettings
HookPath string `long:"hook-path" short:"p" description:"The path to the hook file or directory" env:"STORK_TOOL_HOOK_PATH"`
}

// The CLI flags for the deploy-login-page-welcome command.
type loginScreenWelcomeDeployCommand struct {
cli.CLICommand
type loginScreenWelcomeDeploySettings struct {
cli.CommandSettings
File string `long:"file" short:"i" description:"HTML source file with a custom welcome message" env:"STORK_TOOL_LOGIN_SCREEN_WELCOME_FILE"`
RestStaticFilesDir string `long:"rest-static-files-dir" short:"d" description:"The directory with static files for the UI; if not provided the tool will try to use default locations" env:"STORK_TOOL_REST_STATIC_FILES_DIR"`
}

// The CLI flags for the undeploy-login-page-welcome command.
type loginScreenWelcomeUndeployCommand struct {
cli.CLICommand
type loginScreenWelcomeUndeploySettings struct {
cli.CommandSettings
RestStaticFilesDir string `long:"rest-static-files-dir" short:"d" description:"The directory with static files for the UI; if not provided the tool will try to use default locations" env:"STORK_TOOL_REST_STATIC_FILES_DIR"`
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func getDBConn(flags dbops.DatabaseCLIFlags) *dbops.PgDB {
// Execute db-create command. It prepares new database for the Stork
// server. It also creates a user that can access this database using
// a generated or user-specified password and the pgcrypto extension.
func runDBCreate(command *databaseCreateCommand) {
func runDBCreate(command *databaseCreateSettings) {
var err error

// Prepare logging fields.
Expand Down Expand Up @@ -214,19 +214,19 @@ func runDBMigrate(databaseSettings dbops.DatabaseCLIFlags, command, version stri
}

// Execute cert export command.
func runCertExport(certificateCommand *certificateExportCommand) error {
db := getDBConn(certificateCommand.DatabaseSettings)
func runCertExport(certificateSettings *certificateExportSettings) error {
db := getDBConn(certificateSettings.DatabaseSettings)
defer db.Close()

return certs.ExportSecret(db, certificateCommand.Object, certificateCommand.File)
return certs.ExportSecret(db, certificateSettings.Object, certificateSettings.File)
}

// Execute cert import command.
func runCertImport(certificateCommand *certificateImportCommand) error {
db := getDBConn(certificateCommand.DatabaseSettings)
func runCertImport(certificateSettings *certificateImportSettings) error {
db := getDBConn(certificateSettings.DatabaseSettings)
defer db.Close()

return certs.ImportSecret(db, certificateCommand.Object, certificateCommand.File)
return certs.ImportSecret(db, certificateSettings.Object, certificateSettings.File)
}

// Inspect the hook file.
Expand All @@ -252,8 +252,8 @@ func inspectHookFile(path string, library *hooksutil.LibraryManager, err error)
}

// Execute inspect hook command.
func runHookInspect(hookInspectCommand *hookInspectCommand) error {
hookPath := hookInspectCommand.HookPath
func runHookInspect(hookInspectSettings *hookInspectSettings) error {
hookPath := hookInspectSettings.HookPath
fileInfo, err := os.Stat(hookPath)
if err != nil {
return errors.Wrapf(err, "cannot stat the hook path: '%s'", hookPath)
Expand All @@ -278,7 +278,7 @@ func runHookInspect(hookInspectCommand *hookInspectCommand) error {
}

// Deploy specified static file view into assets/static-page-content.
func runStaticViewDeploy(settings *loginScreenWelcomeDeployCommand, outFilename string) error {
func runStaticViewDeploy(settings *loginScreenWelcomeDeploySettings, outFilename string) error {
// Basic checks on the input file.
inFilename := settings.File
if _, err := os.Stat(inFilename); err != nil {
Expand Down Expand Up @@ -329,7 +329,7 @@ func runStaticViewDeploy(settings *loginScreenWelcomeDeployCommand, outFilename
}

// Undeploy specified static file view from assets/static-page-content.
func runStaticViewUndeploy(settings *loginScreenWelcomeUndeployCommand, filename string) error {
func runStaticViewUndeploy(settings *loginScreenWelcomeUndeploySettings, filename string) error {
// Get the directory where our file is to be copied.
directory, err := getOrLocateStaticPageContentDir(settings.RestStaticFilesDir)
if err != nil {
Expand Down Expand Up @@ -413,123 +413,123 @@ func newApp() *cli.App {
// feature.

// Database creation commands.
databaseCreateCommand := &databaseCreateCommand{}
databaseCreateSettings := &databaseCreateSettings{}
app.RegisterCommand(
"db-create", "Create new Stork database", databaseCreateCommand,
"db-create", "Create new Stork database", databaseCreateSettings,
func() {
runDBCreate(databaseCreateCommand)
runDBCreate(databaseCreateSettings)
},
)

databasePasswordGenCommand := &cli.CLICommand{}
databasePasswordGenSettings := &cli.CommandSettings{}
app.RegisterCommand(
"db-password-gen", "Generate random Stork database password",
databasePasswordGenCommand, runDBPasswordGen,
databasePasswordGenSettings, runDBPasswordGen,
)

databaseInitCommand := &databaseCommand{}
databaseInitSettings := &databaseSettings{}
app.RegisterCommand(
"db-init", "Create schema versioning table in the database",
databaseInitCommand, func() {
runDBMigrate(databaseInitCommand.DatabaseSettings, "init", "")
databaseInitSettings, func() {
runDBMigrate(databaseInitSettings.DatabaseSettings, "init", "")
},
)

databaseUpCommand := &databaseVersionCommand{}
databaseUpSettings := &databaseVersionSettings{}
app.RegisterCommand(
"db-up", "Run all available migrations or use -t to specify version",
databaseUpCommand, func() {
databaseUpSettings, func() {
runDBMigrate(
databaseUpCommand.DatabaseSettings,
databaseUpSettings.DatabaseSettings,
"up",
databaseUpCommand.Version,
databaseUpSettings.Version,
)
},
)

databaseDownCommand := &databaseVersionCommand{}
databaseDownSettings := &databaseVersionSettings{}
app.RegisterCommand(
"db-down", "Revert last migration or use -t to specify version to downgrade to",
databaseDownCommand, func() {
databaseDownSettings, func() {
runDBMigrate(
databaseDownCommand.DatabaseSettings,
databaseDownSettings.DatabaseSettings,
"down",
databaseDownCommand.Version,
databaseDownSettings.Version,
)
},
)

databaseResetCommand := &databaseCommand{}
databaseResetSettings := &databaseSettings{}
app.RegisterCommand(
"db-reset", "Reset the database to the initial state",
databaseResetCommand, func() {
runDBMigrate(databaseResetCommand.DatabaseSettings, "reset", "")
databaseResetSettings, func() {
runDBMigrate(databaseResetSettings.DatabaseSettings, "reset", "")
},
)

dbVersionCommand := &databaseCommand{}
dbVersionSettings := &databaseSettings{}
app.RegisterCommand(
"db-version", "Get the current database schema version",
dbVersionCommand, func() {
runDBMigrate(dbVersionCommand.DatabaseSettings, "version", "")
dbVersionSettings, func() {
runDBMigrate(dbVersionSettings.DatabaseSettings, "version", "")
},
)

databaseSetVersionCommand := &databaseVersionCommand{}
databaseSetVersionSettings := &databaseVersionSettings{}
app.RegisterCommand(
"db-set-version", "Set the database schema version",
databaseSetVersionCommand, func() {
databaseSetVersionSettings, func() {
runDBMigrate(
databaseSetVersionCommand.DatabaseSettings,
databaseSetVersionSettings.DatabaseSettings,
"set_version",
databaseSetVersionCommand.Version,
databaseSetVersionSettings.Version,
)
},
)

// Certificate management commands.
certificateExportCommand := &certificateExportCommand{}
certificateExportSettings := &certificateExportSettings{}
app.RegisterCommand(
"cert-export", "Export Stork Server keys, certificates, and tokens",
certificateExportCommand, func() {
err := runCertExport(certificateExportCommand)
certificateExportSettings, func() {
err := runCertExport(certificateExportSettings)
if err != nil {
log.WithError(err).Fatal("Failed to export the certificate")
}
},
)

certificateImportCommand := &certificateImportCommand{}
certificateImportSettings := &certificateImportSettings{}
app.RegisterCommand(
"cert-import", "Import Stork Server keys, certificates, and tokens",
certificateImportCommand, func() {
err := runCertImport(certificateImportCommand)
certificateImportSettings, func() {
err := runCertImport(certificateImportSettings)
if err != nil {
log.WithError(err).Fatal("Failed to import the certificate")
}
},
)

// Hook inspection command.
hookInspectCommand := &hookInspectCommand{}
hookInspectSettings := &hookInspectSettings{}
app.RegisterCommand(
"hook-inspect", "Inspect the hook file or directory",
hookInspectCommand, func() {
err := runHookInspect(hookInspectCommand)
hookInspectSettings, func() {
err := runHookInspect(hookInspectSettings)
if err != nil {
log.WithError(err).Fatal("Failed to inspect the hook")
}
},
)

// Static views deployment commands.
loginScreenWelcomeDeployCommand := &loginScreenWelcomeDeployCommand{}
loginScreenWelcomeDeploySettings := &loginScreenWelcomeDeploySettings{}
app.RegisterCommand(
"deploy-login-page-welcome",
"Deploy custom welcome message on the login screen",
loginScreenWelcomeDeployCommand, func() {
loginScreenWelcomeDeploySettings, func() {
err := runStaticViewDeploy(
loginScreenWelcomeDeployCommand, "login-screen-welcome.html",
loginScreenWelcomeDeploySettings, "login-screen-welcome.html",
)
if err != nil {
log.WithError(err).
Expand All @@ -538,13 +538,13 @@ func newApp() *cli.App {
},
)

loginScreenWelcomeUndeployCommand := &loginScreenWelcomeUndeployCommand{}
loginScreenWelcomeUndeploySettings := &loginScreenWelcomeUndeploySettings{}
app.RegisterCommand(
"undeploy-login-page-welcome",
"Undeploy custom welcome message on the login screen",
loginScreenWelcomeUndeployCommand, func() {
loginScreenWelcomeUndeploySettings, func() {
err := runStaticViewUndeploy(
loginScreenWelcomeUndeployCommand, "login-screen-welcome.html",
loginScreenWelcomeUndeploySettings, "login-screen-welcome.html",
)
if err != nil {
log.WithError(err).
Expand Down