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

Commit

Permalink
unify output
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 25, 2020
1 parent 11920b5 commit 82116c8
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 56 deletions.
9 changes: 5 additions & 4 deletions internal/commands/configcmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ func runCatCommand(cmd *cobra.Command, args []string) {
content, err := ioutil.ReadFile(filepath)

if err != nil {
utils.ErrorAndExit(errors.New("Could not read config file"))
} else {
fmt.Printf("Here comes the %s file:\n", chalk.Cyan.Color("Dockma config"))
fmt.Println(err)

fmt.Println(string(content))
utils.ErrorAndExit(errors.New("Could not read config file"))
}

fmt.Printf("Here comes the %s file:\n", chalk.Cyan.Color("Dockma config"))
fmt.Println(string(content))
}
4 changes: 2 additions & 2 deletions internal/commands/configcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var configVars []string = []string{"hidesubcommandoutput", "username"}
func getSetCommand() *cobra.Command {
return &cobra.Command{
Use: "set",
Short: "Set Dockma config vars in an interactive walkthrough",
Long: "Set Dockma config vars in an interactive walkthrough",
Short: "Set variables of Dockma configuration",
Long: "Set variables of Dockma configuration",
Example: "dockma config set",
Args: cobra.OnlyValidArgs,
ValidArgs: configVars,
Expand Down
7 changes: 3 additions & 4 deletions internal/commands/downcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
func GetDownCommand() *cobra.Command {
return &cobra.Command{
Use: "down",
Short: "Stops active environment",
Long: "Stops active environment",
Short: "Downs active environment",
Long: "Downs active environment",
Example: "dockma down",
Args: cobra.NoArgs,
PersistentPreRun: hooks.RequiresActiveEnv,
Expand All @@ -29,7 +29,6 @@ func GetDownCommand() *cobra.Command {

func runDownCommand(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()

envHomeDir := activeEnv.GetHomeDir()

err := os.Chdir(envHomeDir)
Expand All @@ -51,5 +50,5 @@ func runDownCommand(cmd *cobra.Command, args []string) {
utils.Success("Executed command: docker-compose down")

viper.Set(fmt.Sprintf("envs.%s.running", activeEnv.GetName()), false)
config.Save("", fmt.Errorf("Failed to set running to 'false' [%s]", activeEnv))
config.Save("", fmt.Errorf("Failed to set environment %s to %s", chalk.Underline.TextStyle(activeEnv.GetName()), chalk.Underline.TextStyle("not running")))
}
19 changes: 10 additions & 9 deletions internal/commands/envcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,29 @@ func runInitCommand(cmd *cobra.Command, args []string) {
path = args[0]

err := os.Chdir(path)

if err != nil {
utils.ErrorAndExit(fmt.Errorf("Could not find directory: %s", path))
fmt.Println(err)
utils.ErrorAndExit(fmt.Errorf("Could not change to path %s", chalk.Underline.TextStyle(path)))
}
} else {
utils.Warn("No path provided. Default is: .")
}

envName = survey.InputName("Enter a name for the new environment (has to be unique)", "")

workingDir, err := os.Getwd()
if err != nil {
utils.ErrorAndExit(errors.New("Could not read current working directory"))
}

pull := "off"
if _, err := os.Stat(".git"); !os.IsNotExist(err) {
pull = survey.Select(fmt.Sprintf("Run %s before %s", chalk.Cyan.Color("git pull"), chalk.Cyan.Color("dockma up")), []string{"auto", "optional", "manual", "off"})
} else {
pull = "no-git"
}

workingDir, err := os.Getwd()
if err != nil {
fmt.Println(err)
utils.ErrorAndExit(errors.New("Could get current working dir"))
}

proceed := survey.Confirm(fmt.Sprintf("Add new environment %s (location: %s)", chalk.Cyan.Color(envName), workingDir), true)
if !proceed {
utils.Abort()
Expand All @@ -63,7 +64,7 @@ func runInitCommand(cmd *cobra.Command, args []string) {
viper.Set(fmt.Sprintf("envs.%s.pull", envName), pull)
viper.Set(fmt.Sprintf("envs.%s.running", envName), false)

config.Save(fmt.Sprintf("Initialized new environment: %s", chalk.Cyan.Color(envName)), fmt.Errorf("Failed to save newly created environment"))
config.Save(fmt.Sprintf("Initialized new environment: %s", chalk.Cyan.Color(envName)), fmt.Errorf("Failed to save newly created environment: %s", envName))

activeEnv := config.GetActiveEnv()
oldEnv := activeEnv.GetName()
Expand All @@ -78,7 +79,7 @@ func runInitCommand(cmd *cobra.Command, args []string) {
if set {
viper.Set("active", envName)

config.Save(fmt.Sprintf("Set active environment: %s", chalk.Cyan.Color(envName)), fmt.Errorf("Failed to set active environment"))
config.Save(fmt.Sprintf("Set active environment: %s", chalk.Cyan.Color(envName)), fmt.Errorf("Failed to set active environment: %s", envName))
}

}
8 changes: 6 additions & 2 deletions internal/commands/envcmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/martinnirtl/dockma/internal/commands/hooks"
"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/ttacon/chalk"
)
Expand All @@ -15,6 +16,7 @@ var pathFlag bool
func getListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List all configured environments",
Long: "List all configured environments",
Example: "dockma envs list",
Expand All @@ -36,13 +38,15 @@ func runListCommand(cmd *cobra.Command, args []string) {
activeEnvName := activeEnv.GetName()

if activeEnvName == "-" {
hooks.PrintNoActiveEnvSet()
utils.Warn("Active environment not set.")

fmt.Printf("Use %s or %s to set active environment.\n\nEnvironments:\n", chalk.Cyan.Color("dockma env init"), chalk.Cyan.Color("dockma env set"))
}

for _, envName := range envs {
if envName == activeEnvName {
fmt.Printf("%s ", chalk.Cyan.Color("[active]"))
} else {
} else if activeEnvName != "-" {
fmt.Print(" ")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/commands/envcmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ func runRemoveCommand(cmd *cobra.Command, args []string) {

if envName == activeEnv.GetName() {
viper.Set("active", "-")

config.Save(chalk.Yellow.Color("Unset active environment."), errors.New("Failed to unset active environment"))
}

envs := viper.GetStringMap("envs")
delete(envs, envName)
viper.Set("envs", envs)

config.Save(fmt.Sprintf("Removed environment: %s", chalk.Cyan.Color(envName)), errors.New("Failed to remove environment"))
config.Save(fmt.Sprintf("Removed environment: %s", chalk.Cyan.Color(envName)), fmt.Errorf("Failed to remove environment: %s", envName))
}
8 changes: 4 additions & 4 deletions internal/commands/envcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func runSetCommand(cmd *cobra.Command, args []string) {
utils.Warn(fmt.Sprintf("Leaving running environment: %s", activeEnv.GetName()))
}

viper.Set("active", envName)

config.Save(fmt.Sprintf("New active environment: %s (old: %s)", chalk.Cyan.Color(envName), activeEnv.GetName()), fmt.Errorf("Failed to set active environment"))

env, err := config.GetEnv(envName)
utils.ErrorAndExit(err)

if env.IsRunning() {
utils.Warn("New active environment is presently running.")
}

viper.Set("active", envName)

config.Save(fmt.Sprintf("New active environment: %s (old: %s)", chalk.Cyan.Color(envName), activeEnv.GetName()), fmt.Errorf("Failed to set active environment: %s", envName))
}
11 changes: 7 additions & 4 deletions internal/commands/initcmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package initcmd

import (
"errors"
"fmt"
"os"
"os/user"
Expand Down Expand Up @@ -30,15 +31,15 @@ func GetInitCommand() *cobra.Command {

func initPreRunHook(cmd *cobra.Command, args []string) {
if init := config.GetInitTime(); !init.IsZero() {
proceed := survey.Confirm(fmt.Sprintf("%sDockma CLI has already been initialized!%s Do you want to proceed", chalk.Yellow, chalk.ResetColor), false)
proceed := survey.Confirm(fmt.Sprintf("%s Do you want to proceed", chalk.Yellow.Color("Dockma CLI has already been initialized!")), false)
if !proceed {
utils.Abort()
}
} else {
accept := survey.Confirm(fmt.Sprintf("Dockma CLI config will be stored at: %s", config.GetHomeDir()), true)

if !accept {
fmt.Printf("Ok, you can change the config default location by setting %sDOCKMA_HOME%s environment variable.\n", chalk.Cyan, chalk.ResetColor)
fmt.Printf("Dockma's config location can be set by %s environment variable.\n", chalk.Cyan.Color("DOCKMA_HOME"))

os.Exit(0)
}
Expand All @@ -59,13 +60,15 @@ func runInitCommand(cmd *cobra.Command, args []string) {
home := config.GetHomeDir()

if err := os.MkdirAll(home, os.FileMode(0755)); err != nil {
utils.ErrorAndExit(fmt.Errorf("Could not create config dir: %s", err))
fmt.Println(err)
utils.ErrorAndExit(errors.New("Could not create config dir"))
}

filepath := path.Join(home, "config.json")

if err := viper.WriteConfigAs(filepath); err != nil {
utils.ErrorAndExit(fmt.Errorf("Could not save config.json at: %s", home))
fmt.Println(err)
utils.ErrorAndExit(fmt.Errorf("Could not save config.json to %s", chalk.Underline.TextStyle(home)))
}

fmt.Printf("%s has been initialized successfully!\n\nStart with adding a new environment by %s or run %s for some little docs.\n", chalk.Cyan.Color("Dockma"), chalk.Cyan.Color("dockma env init"), chalk.Cyan.Color("dockma help"))
Expand Down
10 changes: 6 additions & 4 deletions internal/commands/inspectcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
func GetInspectCommand() *cobra.Command {
return &cobra.Command{
Use: "inspect",
Short: "Print detailed output of previously executed external command [up|down|pull]",
Long: "Print detailed output of previously executed external command [up|down|pull]",
Short: "Print detailed output of previously executed external command",
Long: "Print detailed output of previously executed external command",
Example: "dockma inspect",
Args: cobra.NoArgs,
Run: runInspectCommand,
Expand All @@ -34,10 +34,12 @@ func runInspectCommand(cmd *cobra.Command, args []string) {
if errors.Is(err, os.ErrNotExist) {
fmt.Println("Nothing to output yet.")
} else {
utils.ErrorAndExit(fmt.Errorf("Could not read logfile %s", viper.GetString("subcommandlogfile")))
logfileName := viper.GetString("subcommandlogfile")

utils.ErrorAndExit(fmt.Errorf("Could not read logfile %s", chalk.Underline.TextStyle(logfileName)))
}
} else {
fmt.Println(chalk.Cyan.Color("Here come the logs:"))
fmt.Println(chalk.Cyan.Color("Here comes the original output:"))
fmt.Print(string(content))
}
}
7 changes: 6 additions & 1 deletion internal/commands/logscmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logscmd

import (
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -42,7 +43,11 @@ func runLogsCommand(cmd *cobra.Command, args []string) {
envHomeDir := activeEnv.GetHomeDir()

err := os.Chdir(envHomeDir)
utils.ErrorAndExit(err)
if err != nil {
fmt.Println(err)

utils.ErrorAndExit(errors.New("Could not change dir"))
}

args = addFlagsToArgs(args)
command := externalcommand.JoinCommand("docker-compose logs", args...)
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/profilecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func createCmdArgsValidator(cmd *cobra.Command, args []string) error {
}

if len(args) > 1 {
return errors.New("Command only takes one argument")
return fmt.Errorf("Expected 1 argument. Got %d", len(args))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/profilecmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func runDeleteCommand(cmd *cobra.Command, args []string) {
profileNames := activeEnv.GetProfileNames()

if len(profileNames) == 0 {
fmt.Printf("No profiles in environment: %s\n", chalk.Cyan.Color(activeEnv.GetName()))
fmt.Printf("No profiles in environment %s.\n", chalk.Cyan.Color(activeEnv.GetName()))

os.Exit(0)
}
Expand Down
1 change: 0 additions & 1 deletion internal/commands/profilecmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func runListCommand(cmd *cobra.Command, args []string) {
fmt.Println(chalk.Bold.TextStyle(profileName))

profile, err := activeEnv.GetProfile(profileName)

utils.ErrorAndExit(err)

for _, service := range profile.Services {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/profilecmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func runRenameCommand(cmd *cobra.Command, args []string) {
profileNames := activeEnv.GetProfileNames()

if len(profileNames) == 0 {
fmt.Printf("No profiles in environment: %s\n", chalk.Cyan.Color(activeEnv.GetName()))
fmt.Printf("No profiles in environment %s.\n", chalk.Cyan.Color(activeEnv.GetName()))

os.Exit(0)
}
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 @@ -7,8 +7,8 @@ func GetProfileCommand() *cobra.Command {
profileCommand := &cobra.Command{
Use: "profile",
Aliases: []string{"profiles"},
Short: "Manage profiles (predefined service selections)",
Long: "Manage profiles (predefined service selections)",
Short: "Manage profiles (named service selections)",
Long: "Manage profiles (named service selections)",
}

profileCommand.AddCommand(getCreateCommand())
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/profilecmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func runUpdateCommand(cmd *cobra.Command, args []string) {
profileNames := activeEnv.GetProfileNames()

if len(profileNames) == 0 {
fmt.Printf("No profiles in environment: %s\n", chalk.Cyan.Color(activeEnv.GetName()))
fmt.Printf("No profiles in environment %s.\n", chalk.Cyan.Color(activeEnv.GetName()))

os.Exit(0)
}
Expand Down
1 change: 0 additions & 1 deletion internal/commands/pscmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func GetPSCommand() *cobra.Command {

func runPSCommand(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()

envHomeDir := activeEnv.GetHomeDir()

err := os.Chdir(envHomeDir)
Expand Down
1 change: 0 additions & 1 deletion internal/commands/restartcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func GetRestartCommand() *cobra.Command {

func runRestartCommand(cmd *cobra.Command, args []string) {
activeEnv := config.GetActiveEnv()

envHomeDir := activeEnv.GetHomeDir()

err := os.Chdir(envHomeDir)
Expand Down
13 changes: 9 additions & 4 deletions internal/commands/scriptcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/pkg/externalcommand"
"github.com/spf13/cobra"
"github.com/ttacon/chalk"
)

// GetScriptCommand returns the top level script command
func GetScriptCommand() *cobra.Command {
return &cobra.Command{
Use: "script [scriptname]",
Short: "Run script located in scripts dir of active environment",
Long: "Run script located in scripts dir of active environment",
Short: "Run script (.sh) located in scripts dir of active environment",
Long: "Run script (.sh) located in scripts dir of active environment",
Example: "dockma script",
Args: cobra.ArbitraryArgs,
PreRun: hooks.RequiresActiveEnv,
Expand All @@ -46,11 +47,15 @@ func runScriptCommand(cmd *cobra.Command, args []string) {
}
} else {
files, err := ioutil.ReadDir(filepath.Join(envHomeDir, "scripts"))
utils.ErrorAndExit(err)
if err != nil {
fmt.Println(err)

utils.ErrorAndExit(fmt.Errorf("Could not read dir %s", chalk.Underline.TextStyle(envHomeDir+"/scripts")))
}

scripts := make([]string, 0, len(files))
for _, file := range files {
if !file.IsDir() && len(file.Name()) > 0 {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".sh") {
scripts = append(scripts, file.Name())
}
}
Expand Down
Loading

0 comments on commit 82116c8

Please sign in to comment.