Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
unify cobra command descriptions, args and other
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 8, 2020
1 parent b47b765 commit 7dc25fa
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 63 deletions.
3 changes: 2 additions & 1 deletion internal/commands/configcmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
var catCmd = &cobra.Command{
Use: "cat",
Short: "Print config.json of Dockma.",
Long: `-`,
Long: "Print config.json of Dockma.",
Example: "dockma config cat",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
filepath := config.GetDockmaFilepath(("config.json"))

Expand Down
3 changes: 2 additions & 1 deletion internal/commands/configcmd/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
var homeCmd = &cobra.Command{
Use: "home",
Short: "Print home dir of Dockma config.",
Long: `-`,
Long: "Print home dir of Dockma config.",
Example: "dockma config home",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Dockma config home dir: %s%s%s\n", chalk.Cyan, viper.GetString("home"), chalk.ResetColor)
},
Expand Down
9 changes: 5 additions & 4 deletions internal/commands/configcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"github.com/spf13/cobra"
)

// ConfigCommand is the top level config command
// ConfigCommand implements the top level config command
var ConfigCommand = &cobra.Command{
Use: "config",
Short: "Dockma configuration details.",
Long: "-",
Use: "config",
Short: "Dockma configuration details.",
Long: "Dockma configuration details.",
Aliases: []string{"cfg"},
}
5 changes: 3 additions & 2 deletions internal/commands/configcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

var setCmd = &cobra.Command{
Use: "set",
Short: "Set dockma config vars in an interactive walkthrough.",
Long: `-`,
Short: "Set Dockma config vars in an interactive walkthrough.",
Long: "Set Dockma config vars in an interactive walkthrough.",
Example: "dockma config set",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
options := []string{
fmt.Sprintf("hidesubcommandoutput: %t", viper.GetBool("hidesubcommandoutput")),
Expand Down
9 changes: 6 additions & 3 deletions internal/commands/downcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
"github.com/ttacon/chalk"
)

// DownCommand implements the top level down command
var DownCommand = &cobra.Command{
Use: "down",
Short: "Stops active environment.",
Long: "-",
Use: "down",
Short: "Stops active environment.",
Long: "Stops active environment.",
Example: "dockma down",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
filepath := config.GetLogfile()

Expand Down
13 changes: 3 additions & 10 deletions internal/commands/envscmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ import (
var initCmd = &cobra.Command{
Use: "init [path-to-environment]",
Short: "Initialize new environment.",
Long: `-`,
Long: "Initialize new environment.",
Args: cobra.RangeArgs(0, 1),
Example: "dockma envs init path/to/env",
// TODO add flag to prevent setting active
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("Too many arguments")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
var env string

Expand All @@ -33,7 +26,7 @@ var initCmd = &cobra.Command{
path = args[0]

if err := os.Chdir(path); err != nil {
fmt.Printf("%sError. Could not change directory to: %s%s\n", chalk.Red, path, chalk.ResetColor)
fmt.Printf("%sError: Could not find directory: %s%s\n", chalk.Red, path, chalk.ResetColor)

os.Exit(0)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/commands/envscmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
var listCmd = &cobra.Command{
Use: "list",
Short: "List all configured environments.",
Long: `-`,
Long: "List all configured environments.",
Args: cobra.NoArgs,
Example: "dockma envs list",
Run: func(cmd *cobra.Command, args []string) {
envs := config.GetEnvs()
Expand Down
11 changes: 2 additions & 9 deletions internal/commands/envscmd/remove.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package envscmd

import (
"errors"
"fmt"

"github.com/martinnirtl/dockma/internal/survey"
Expand All @@ -15,15 +14,9 @@ var removeCmd = &cobra.Command{
Use: "remove [environment]",
Aliases: []string{"rm"},
Short: "Remove environment.",
Long: `-`,
Long: "Remove environment.",
Args: cobra.RangeArgs(0, 1),
Example: "dockma envs remove my-env",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("Too many arguments")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
env := ""
if len(args) == 0 {
Expand Down
9 changes: 5 additions & 4 deletions internal/commands/envscmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"github.com/spf13/cobra"
)

// EnvsCommand is the top level Envs command
// EnvsCommand implements the top level envs command
var EnvsCommand = &cobra.Command{
Use: "envs",
Short: "Environments reflect docker-compose based projects.",
Long: "-",
Use: "env",
Aliases: []string{"environment"},
Short: "Environments reflect docker-compose based projects.",
Long: "Environments reflect docker-compose based projects.",
}
11 changes: 2 additions & 9 deletions internal/commands/envscmd/set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package envscmd

import (
"errors"
"fmt"

"github.com/martinnirtl/dockma/internal/utils"
Expand All @@ -13,15 +12,9 @@ import (
var setCmd = &cobra.Command{
Use: "set [environment]",
Short: "Set active environment.",
Long: `-`,
Long: "Set active environment.",
Args: cobra.RangeArgs(0, 1),
Example: "dockma envs set",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("Too many arguments")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
env := ""
if len(args) == 0 {
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/initcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
"github.com/ttacon/chalk"
)

// InitCommand implements the top level init command
var InitCommand = &cobra.Command{
Use: "init",
Short: "Initialize the Dockma CLI.",
Long: "-",
Long: "Initialize the Dockma CLI.",
Args: cobra.NoArgs,
PersistentPreRun: initPreRunHook, // used to override root PreRun func
Run: initCommandHandler,
}
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/inspectcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
"github.com/ttacon/chalk"
)

// InspectCommand implements the top level inspect command
var InspectCommand = &cobra.Command{
Use: "inspect",
Short: "Print detailed output of previously executed command [up|down|pull].",
Long: "-",
Long: "Print detailed output of previously executed command [up|down|pull].",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
filepath := config.GetLogfile()

Expand Down
12 changes: 12 additions & 0 deletions internal/commands/installcmd/root.go
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
package installcmd

import (
"github.com/spf13/cobra"
)

// InstallCommand implements the top level install command
var InstallCommand = &cobra.Command{
Use: "install",
Short: "-",
Long: "-",
Run: func(cmd *cobra.Command, args []string) {},
}
26 changes: 23 additions & 3 deletions internal/commands/logscmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"os"
"sort"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/pkg/dockercompose"
"github.com/martinnirtl/dockma/pkg/externalcommand"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -15,10 +17,13 @@ var followFlag bool
var timestampsFlag bool
var tailFlag int

// LogsCommand implements the top level logs command
var LogsCommand = &cobra.Command{
Use: "logs [service...]",
Short: "Logs output of all or only selected services.",
Long: "-",
Use: "logs [service...]",
Short: "Logs output of all or only selected services.",
Long: "Logs output of all or only selected services.",
Args: cobra.OnlyValidArgs,
ValidArgs: getValidArgs(),
Run: func(cmd *cobra.Command, args []string) {
activeEnv := viper.GetString("active")

Expand Down Expand Up @@ -67,3 +72,18 @@ func addFlagsToArgs(args []string) []string {

return args
}

func getValidArgs() []string {
activeEnv := config.GetActiveEnv()

if activeEnv == "-" {
utils.NoEnvs()
}

envHomeDir := config.GetEnvHomeDir(activeEnv)

services, err := dockercompose.GetServices(envHomeDir)
utils.Error(err)

return services.All
}
3 changes: 2 additions & 1 deletion internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
var createCmd = &cobra.Command{
Use: "create",
Short: "Create named service selection.",
Long: `-`,
Long: "Create named service selection.",
Args: cobra.NoArgs,
Example: "dockma profile create",
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
Expand Down
3 changes: 2 additions & 1 deletion internal/commands/profilecmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a profile of active environment.",
Long: `-`,
Long: "Delete a profile of active environment.",
Args: cobra.NoArgs,
Aliases: []string{"del"},
Example: "dockma profile delete",
Run: func(cmd *cobra.Command, args []string) {
Expand Down
3 changes: 2 additions & 1 deletion internal/commands/profilecmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var servicesFlag bool
var listCmd = &cobra.Command{
Use: "list",
Short: "List profiles of active environment.",
Long: `-`,
Long: "List profiles of active environment.",
Args: cobra.NoArgs,
Example: "dockma profile list",
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
Expand Down
3 changes: 2 additions & 1 deletion internal/commands/profilecmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
var renameCmd = &cobra.Command{
Use: "rename",
Short: "Rename profile.",
Long: `-`,
Long: "Rename profile.",
Args: cobra.NoArgs,
Example: "dockma profile rename",
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/profilecmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package profilecmd

import "github.com/spf13/cobra"

// ProfileCommand is the top level config command
// ProfileCommand implements the top level profile command
var ProfileCommand = &cobra.Command{
Use: "profile",
Short: "Manage profiles (predefined service selections).",
Long: "-",
Long: "Manage profiles (predefined service selections).",
}
4 changes: 3 additions & 1 deletion internal/commands/profilecmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update profile's service selection.",
Long: `-`,
Long: "Update profile's service selection.",
Aliases: []string{"upd"},
Args: cobra.NoArgs,
Example: "dockma profile update",
Run: func(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/pscmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"github.com/spf13/viper"
)

// PSCommand implements the top level ps command
var PSCommand = &cobra.Command{
Use: "ps",
Short: "List running services of active environment.",
Long: "-",
Long: "List running services of active environment.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
activeEnv := viper.GetString("active")

Expand Down
5 changes: 3 additions & 2 deletions internal/commands/pullcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func Pull(path string, log bool) error {
return nil
}

// PullCommand is a top level dockma command
// PullCommand implements the top level pull command
var PullCommand = &cobra.Command{
Use: "pull",
Short: "Run 'git pull' in active environment home dir.",
Long: "-",
Long: "Run 'git pull' in active environment home dir.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {

activeEnv := config.GetActiveEnv()
Expand Down
5 changes: 3 additions & 2 deletions internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"github.com/ttacon/chalk"
)

// UpCommand implements the top level dockma command up
// UpCommand implements the top level up command
var UpCommand = &cobra.Command{
Use: "up",
Short: "Runs active environment with service selection.",
Long: "-",
Long: "Runs active environment with service selection.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
filepath := config.GetLogfile()

Expand Down
6 changes: 4 additions & 2 deletions internal/commands/versioncmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"github.com/spf13/cobra"
)

// VersionCommand is the top level version command
// VersionCommand implements the top level version command
var VersionCommand = &cobra.Command{
Use: "version",
Short: "Print the version number of dockma.",
Long: "-",
Long: "Print the version number of dockma.",
Args: cobra.NoArgs,
Hidden: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("🐳 Dockma v0.0.0")
Expand Down

0 comments on commit 7dc25fa

Please sign in to comment.