-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from dokku/cli-skeleton
refactor: use cli-skeleton for commands
- Loading branch information
Showing
30 changed files
with
1,484 additions
and
493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.