gencmd

package
v0.7.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

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
  1. 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
    
  2. 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
    
  3. Add the new package to cmd/cli/main.go, in this case it would be:
    	_ "github.com/datumforge/datum/cmd/cli/cmd/contact"
    
  4. Add flags for the Create and Update commands for input
  5. Add validation to the createValidation() and updateValidation() functions for the required input
  6. 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:

  1. Run the read-only generate command
    task cli:generate:ro
    
  2. 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
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(cmdName string, cmdDirName string, readOnly bool, force bool) error

Generate generates the cli command files for the given command name

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL