Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cli-skeleton for commands #72

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
100 changes: 100 additions & 0 deletions commands/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package commands

import (
"fmt"
"os"
"strings"

"github.com/josegonzalez/cli-skeleton/command"
"github.com/posener/complete"
flag "github.com/spf13/pflag"
)

type CheckCommand struct {
command.Meta
GlobalFlagCommand
}

func (c *CheckCommand) Name() string {
return "check"
}

func (c *CheckCommand) Synopsis() string {
return "Eats one or more lollipops"
}

func (c *CheckCommand) Help() string {
return command.CommandHelp(c)
}

func (c *CheckCommand) Examples() map[string]string {
appName := os.Getenv("CLI_APP_NAME")
return map[string]string{
"Check if the procfile is valid": fmt.Sprintf("%s %s", appName, c.Name()),
}
}

func (c *CheckCommand) Arguments() []command.Argument {
args := []command.Argument{}
return args
}

func (c *CheckCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *CheckCommand) ParsedArguments(args []string) (map[string]command.Argument, error) {
return command.ParseArguments(args, c.Arguments())
}

func (c *CheckCommand) FlagSet() *flag.FlagSet {
f := c.Meta.FlagSet(c.Name(), command.FlagSetClient)
c.GlobalFlags(f)
return f
}

func (c *CheckCommand) AutocompleteFlags() complete.Flags {
return command.MergeAutocompleteFlags(
c.Meta.AutocompleteFlags(command.FlagSetClient),
c.AutocompleteGlobalFlags(),
complete.Flags{},
)
}

func (c *CheckCommand) Run(args []string) int {
flags := c.FlagSet()
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
c.Ui.Error(err.Error())
c.Ui.Error(command.CommandErrorText(c))
return 1
}

_, err := c.ParsedArguments(flags.Args())
if err != nil {
c.Ui.Error(err.Error())
c.Ui.Error(command.CommandErrorText(c))
return 1
}

entries, err := parseProcfile(c.procfile, c.delimiter, c.strict)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

if len(entries) == 0 {
c.Ui.Error("No processes defined")
return 1
}

names := []string{}
for _, entry := range entries {
names = append(names, entry.Name)
}

processNames := strings.Join(names[:], ", ")
c.Ui.Output(fmt.Sprintf("valid procfile detected %v", processNames))

return 0
}
26 changes: 0 additions & 26 deletions commands/check_command.go

This file was deleted.

117 changes: 117 additions & 0 deletions commands/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package commands

import (
"fmt"
"os"
"procfile-util/procfile"

"github.com/josegonzalez/cli-skeleton/command"
"github.com/posener/complete"
flag "github.com/spf13/pflag"
)

type DeleteCommand struct {
command.Meta
GlobalFlagCommand

processType string
stdout bool
writePath string
}

func (c *DeleteCommand) Name() string {
return "delete"
}

func (c *DeleteCommand) Synopsis() string {
return "Eats one or more lollipops"
}

func (c *DeleteCommand) Help() string {
return command.CommandHelp(c)
}

func (c *DeleteCommand) Examples() map[string]string {
appName := os.Getenv("CLI_APP_NAME")
return map[string]string{
"Command": fmt.Sprintf("%s %s", appName, c.Name()),
}
}

func (c *DeleteCommand) Arguments() []command.Argument {
args := []command.Argument{}
return args
}

func (c *DeleteCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *DeleteCommand) ParsedArguments(args []string) (map[string]command.Argument, error) {
return command.ParseArguments(args, c.Arguments())
}

func (c *DeleteCommand) FlagSet() *flag.FlagSet {
f := c.Meta.FlagSet(c.Name(), command.FlagSetClient)
// required?
f.StringVarP(&c.processType, "process-type", "p", "", "name of process to delete")
f.BoolVarP(&c.stdout, "stdout", "s", false, "write output to stdout")
f.StringVarP(&c.writePath, "write-path", "w", "", "path to Procfile to write to")
c.GlobalFlags(f)
return f
}

func (c *DeleteCommand) AutocompleteFlags() complete.Flags {
return command.MergeAutocompleteFlags(
c.Meta.AutocompleteFlags(command.FlagSetClient),
c.AutocompleteGlobalFlags(),
complete.Flags{
"--process-type": complete.PredictAnything,
"--stdout": complete.PredictNothing,
"--write-path": complete.PredictAnything,
},
)
}

func (c *DeleteCommand) Run(args []string) int {
flags := c.FlagSet()
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
c.Ui.Error(err.Error())
c.Ui.Error(command.CommandErrorText(c))
return 1
}

_, err := c.ParsedArguments(flags.Args())
if err != nil {
c.Ui.Error(err.Error())
c.Ui.Error(command.CommandErrorText(c))
return 1
}

entries, err := parseProcfile(c.procfile, c.delimiter, c.strict)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

if len(entries) == 0 {
c.Ui.Error("No processes defined")
return 1
}

var validEntries []procfile.ProcfileEntry
for _, entry := range entries {
if c.processType == entry.Name {
continue
}
validEntries = append(validEntries, entry)
}

if err := procfile.OutputProcfile(c.procfile, c.writePath, c.delimiter, c.stdout, validEntries); err != nil {
c.Ui.Error(err.Error())
return 1
}

return 0
}
17 changes: 0 additions & 17 deletions commands/delete_command.go

This file was deleted.

105 changes: 105 additions & 0 deletions commands/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package commands

import (
"fmt"
"os"

"github.com/josegonzalez/cli-skeleton/command"
"github.com/posener/complete"
flag "github.com/spf13/pflag"
)

type ExistsCommand struct {
command.Meta
GlobalFlagCommand

processType string
}

func (c *ExistsCommand) Name() string {
return "exists"
}

func (c *ExistsCommand) Synopsis() string {
return "Eats one or more lollipops"
}

func (c *ExistsCommand) Help() string {
return command.CommandHelp(c)
}

func (c *ExistsCommand) Examples() map[string]string {
appName := os.Getenv("CLI_APP_NAME")
return map[string]string{
"Command": fmt.Sprintf("%s %s", appName, c.Name()),
}
}

func (c *ExistsCommand) Arguments() []command.Argument {
args := []command.Argument{}
return args
}

func (c *ExistsCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *ExistsCommand) ParsedArguments(args []string) (map[string]command.Argument, error) {
return command.ParseArguments(args, c.Arguments())
}

func (c *ExistsCommand) FlagSet() *flag.FlagSet {
f := c.Meta.FlagSet(c.Name(), command.FlagSetClient)
// required?
f.StringVarP(&c.processType, "process-type", "p", "", "name of process to delete")
c.GlobalFlags(f)
return f
}

func (c *ExistsCommand) AutocompleteFlags() complete.Flags {
return command.MergeAutocompleteFlags(
c.Meta.AutocompleteFlags(command.FlagSetClient),
c.AutocompleteGlobalFlags(),
complete.Flags{
"--process-type": complete.PredictAnything,
},
)
}

func (c *ExistsCommand) Run(args []string) int {
flags := c.FlagSet()
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
c.Ui.Error(err.Error())
c.Ui.Error(command.CommandErrorText(c))
return 1
}

_, err := c.ParsedArguments(flags.Args())
if err != nil {
c.Ui.Error(err.Error())
c.Ui.Error(command.CommandErrorText(c))
return 1
}

entries, err := parseProcfile(c.procfile, c.delimiter, c.strict)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

if len(entries) == 0 {
c.Ui.Error("No processes defined")
return 1
}

for _, entry := range entries {
if c.processType == entry.Name {
return 0
}
}

c.Ui.Error("No matching process entry found")

return 1
}
19 changes: 0 additions & 19 deletions commands/exists_command.go

This file was deleted.

Loading
Loading