GenCMD
This package is used to generate the base CRUD operations for a cli command for the DatumClient
Usage
The Taskfile includes two commands:
* cli:generate: generates a new cli cmd
* cli:generate:ro: generates a new cli cmd with only the read cmds
To use the cli directly instead of the Taskfile
:
go run cmd/cli/gencmd/generate/main.go generate --help
generate is the command to generate the stub files for a given cli cmd
Usage:
generate [flags]
Flags:
-d, --dir string root directory location to generate the files (default "cmd")
-h, --help help for generate
-i, --interactive interactive prompt, set to false to disable (default true)
-n, --name string name of the command to generate
-r, --read-only only generate the read only commands, no create, update or delete commands
Generate All CRUD Operations
- Run the generate command
task cli:generate
task: [cli:generate] go run gencmd/generate/main.go generate
Name of the command (should be the singular version of the object): Contact
----> creating cli cmd for: Contact
----> executing template: create.tmpl
----> executing template: delete.tmpl
----> executing template: doc.tmpl
----> executing template: get.tmpl
----> executing template: root.tmpl
----> executing template: update.tmpl
- This will create a set of cobra command files:
ls -l cmd/cli/cmd/contact/
total 48
-rw-r--r-- 1 sarahfunkhouser staff 1261 Jun 27 13:30 create.go
-rw-r--r-- 1 sarahfunkhouser staff 1086 Jun 27 13:30 delete.go
-rw-r--r-- 1 sarahfunkhouser staff 102 Jun 27 13:30 doc.go
-rw-r--r-- 1 sarahfunkhouser staff 1079 Jun 27 13:30 get.go
-rw-r--r-- 1 sarahfunkhouser staff 2570 Jun 27 13:30 root.go
-rw-r--r-- 1 sarahfunkhouser staff 1511 Jun 27 13:30 update.go
- Add the new package to
cmd/cli/main.go
, in this case it would be:
_ "github.com/datumforge/datum/cmd/cli/cmd/contact"
- Add flags for the
Create
and Update
commands for input
- Add validation to the
createValidation()
and updateValidation()
functions for the required input
- Add fields for the table output in
root.go
in tableOutput()
function, by default it only includes ID
.
// tableOutput prints the plans in a table format
func tableOutput(plans []datumclient.Contact) {
writer := tables.NewTableWriter(cmd.OutOrStdout(), "ID", "Name", "Email", "PhoneNumber")
for _, p := range plans {
writer.AddRow(p.ID, p.Name, *p.Email, *p.PhoneNumber)
}
writer.Render()
}
Generate Read-Only Operations
A common use-case for some of the resolvers is a read-only
set of functions, particularly in our History
schemas. To generate the cmds with only the get
functionality use the --read-only
flag:
- Run the read-only generate command
task cli:generate:ro
- The resulting commands will just be a
get
command
task: [cli:generate:ro] go run gencmd/generate/main.go generate --read-only
Name of the command (should be the singular version of the object): ContactHistory
----> creating cli cmd for: ContactHistory
----> executing template: doc.tmpl
----> executing template: get.tmpl
----> executing template: root.tmpl