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

Commit

Permalink
features and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 6, 2020
1 parent ee81a72 commit b3ba402
Show file tree
Hide file tree
Showing 26 changed files with 535 additions and 153 deletions.
4 changes: 2 additions & 2 deletions internal/commands/configcmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io/ioutil"

"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/internal/config"
"github.com/spf13/cobra"
"github.com/ttacon/chalk"
)
Expand All @@ -15,7 +15,7 @@ var catCmd = &cobra.Command{
Long: `-`,
Example: "dockma config cat",
Run: func(cmd *cobra.Command, args []string) {
filepath := utils.GetFullLogfilePath("config.json")
filepath := config.GetDockmaFilepath(("config.json"))

content, err := ioutil.ReadFile(filepath)

Expand Down
9 changes: 5 additions & 4 deletions internal/commands/downcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import (
"fmt"
"os"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/pkg/externalcommand"
"github.com/martinnirtl/dockma/pkg/externalcommand/spinnertimebridger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
)

var DownCommand = &cobra.Command{
Use: "down",
Short: "Stops active environment.",
Long: "-",
Run: func(cmd *cobra.Command, args []string) {
logfileName := viper.GetString("logfile")
filepath := utils.GetFullLogfilePath(logfileName)
filepath := config.GetLogfile()

activeEnv := viper.GetString("active")

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

envHomeDir := viper.GetString(fmt.Sprintf("environments.%s.home", activeEnv))
envHomeDir := viper.GetString(fmt.Sprintf("envs.%s.home", activeEnv))

err := os.Chdir(envHomeDir)

Expand All @@ -35,7 +36,7 @@ var DownCommand = &cobra.Command{

var timebridger externalcommand.Timebridger
if hideCmdOutput := viper.GetBool("hidesubcommandoutput"); !hideCmdOutput {
timebridger = spinnertimebridger.New("Running command '%s'", "", 14, "cyan")
timebridger = spinnertimebridger.New("Running 'docker-compose down'", fmt.Sprintf("%sSuccessfully executed 'docker-compose down'%s", chalk.Green, chalk.ResetColor), 14, "cyan")
}

_, err = externalcommand.Execute("docker-compose down", timebridger, filepath)
Expand Down
1 change: 0 additions & 1 deletion internal/commands/environmentscmd/rename.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package environmentscmd
package envscmd

import (
"errors"
"fmt"
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/pkg/dockercompose"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
Expand Down Expand Up @@ -40,14 +39,18 @@ var initCmd = &cobra.Command{
}
}

err := survey.AskOne(&survey.Input{
Message: "Enter a name for the new environment (has to be unique)",
}, &env)
env, err := survey.Input("Enter a name for the new environment (has to be unique)", "")

if err != nil {
utils.Abort()
}

if env == "" {
utils.Error(errors.New("Got empty string for environment name"))
} else if env == "-" {
utils.Error(errors.New("Invalid environment name '-'"))
}

workingDir, err := os.Getwd()

if err != nil {
Expand All @@ -56,20 +59,13 @@ var initCmd = &cobra.Command{
os.Exit(0)
}

// TODO read docker-compose.yaml
services, err := dockercompose.GetServices(workingDir)
autoPull, err := survey.Confirm(fmt.Sprintf("Run %sgit pull%s before %sdockma up%s", chalk.Cyan, chalk.Reset, chalk.Cyan, chalk.ResetColor), false)

if err != nil {
fmt.Print(err)

os.Exit(1)
utils.Abort()
}

proceed := false
err = survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Add new environment %s%s%s (location: %s)", chalk.Cyan, env, chalk.ResetColor, workingDir),
Default: true,
}, &proceed)
proceed, err := survey.Confirm(fmt.Sprintf("Add new environment %s%s%s (location: %s)", chalk.Cyan, env, chalk.ResetColor, workingDir), true)

if !proceed {
utils.Abort()
Expand All @@ -79,8 +75,8 @@ var initCmd = &cobra.Command{
os.Exit(0)
}

viper.Set(fmt.Sprintf("environments.%s.home", env), workingDir)
viper.Set(fmt.Sprintf("environments.%s.services", env), services.All)
viper.Set(fmt.Sprintf("envs.%s.home", env), workingDir)
viper.Set(fmt.Sprintf("envs.%s.autopull", env), autoPull)

oldEnv := viper.GetString("active")

Expand All @@ -97,5 +93,5 @@ var initCmd = &cobra.Command{
}

func init() {
EnvironmentsCommand.AddCommand(initCmd)
EnvsCommand.AddCommand(initCmd)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package environmentscmd
package envscmd

import (
"fmt"

"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
Expand All @@ -16,7 +16,7 @@ var listCmd = &cobra.Command{
Long: `-`,
Example: "dockma envs list",
Run: func(cmd *cobra.Command, args []string) {
envs := utils.GetEnvironments()
envs := config.GetEnvs()

activeEnv := viper.GetString("active")

Expand All @@ -35,5 +35,5 @@ var listCmd = &cobra.Command{
}

func init() {
EnvironmentsCommand.AddCommand(listCmd)
EnvsCommand.AddCommand(listCmd)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package environmentscmd
package envscmd

import (
"errors"
"fmt"
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -33,13 +32,10 @@ var removeCmd = &cobra.Command{
env = utils.GetEnvironment(args[0])
}

sure := false
if err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Are you sure to remove '%s'", env),
}, &sure); err != nil || !sure {
fmt.Printf("%sAborted.%s\n", chalk.Cyan, chalk.ResetColor)
sure, err := survey.Confirm(fmt.Sprintf("Are you sure to remove '%s'", env), false)

os.Exit(0)
if err != nil || !sure {
utils.Abort()
}

activeEnv := viper.GetString("active")
Expand All @@ -54,11 +50,11 @@ var removeCmd = &cobra.Command{
fmt.Printf("%sRemoved environment: %s%s\n", chalk.Cyan, env, chalk.ResetColor)
}

envs := viper.GetStringMap("environments")
envs := viper.GetStringMap("envs")

delete(envs, env)

viper.Set("environments", envs)
viper.Set("envs", envs)

if err := viper.WriteConfig(); err != nil {
fmt.Printf("%sError removing environment: %s%s\n", chalk.Red, env, chalk.ResetColor)
Expand All @@ -67,5 +63,5 @@ var removeCmd = &cobra.Command{
}

func init() {
EnvironmentsCommand.AddCommand(removeCmd)
EnvsCommand.AddCommand(removeCmd)
}
1 change: 1 addition & 0 deletions internal/commands/envscmd/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package envscmd
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package environmentscmd
package envscmd

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

// EnvironmentsCommand is the top level environments command
var EnvironmentsCommand = &cobra.Command{
// EnvsCommand is the top level Envs command
var EnvsCommand = &cobra.Command{
Use: "envs",
Short: "Environments reflect docker-compose based projects.",
Long: "-",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package environmentscmd
package envscmd

import (
"errors"
Expand Down Expand Up @@ -49,5 +49,5 @@ var setCmd = &cobra.Command{
}

func init() {
EnvironmentsCommand.AddCommand(setCmd)
EnvsCommand.AddCommand(setCmd)
}
33 changes: 7 additions & 26 deletions internal/commands/initcmd/root.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package initcmd

import (
"errors"
"fmt"
"os"
"os/user"
"path"
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -26,21 +25,13 @@ var InitCommand = &cobra.Command{

func initPreRunHook(cmd *cobra.Command, args []string) {
if init := viper.GetTime("init"); !init.IsZero() {
proceed := false
err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("%sDockma CLI has already been initialized!%s Do you want to proceed", chalk.Yellow, chalk.ResetColor),
Default: false,
}, &proceed, survey.WithValidator(survey.Required))
proceed, err := survey.Confirm(fmt.Sprintf("%sDockma CLI has already been initialized!%s Do you want to proceed", chalk.Yellow, chalk.ResetColor), false)

if err != nil || !proceed {
utils.Abort()
}
} else {
accept := false
err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Dockma CLI config will be stored at: %s", viper.GetString("home")),
Default: true,
}, &accept, survey.WithValidator(survey.Required))
accept, err := survey.Confirm(fmt.Sprintf("Dockma CLI config will be stored at: %s", viper.GetString("home")), true)

if err != nil {
utils.Abort()
Expand All @@ -58,21 +49,11 @@ func initCommandHandler(cmd *cobra.Command, args []string) {
username = sysUser.Username
}

survey.AskOne(&survey.Input{
Message: "What is your name",
Default: strings.Title(username),
}, &username, survey.WithValidator(func(val interface{}) error {
switch text := val.(type) {
case string:
if len(text) > 0 {
return nil
}
default:
return errors.New("invalid input for username")
}
username, err := survey.Input("What is your name", strings.Title(username))

return nil
}))
if err != nil {
utils.Abort()
}

viper.Set("username", username)
viper.Set("init", time.Now())
Expand Down
5 changes: 2 additions & 3 deletions internal/commands/inspectcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io/ioutil"
"os"

"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
Expand All @@ -17,8 +17,7 @@ var InspectCommand = &cobra.Command{
Short: "Print detailed output of previously executed command [up|down].",
Long: "-",
Run: func(cmd *cobra.Command, args []string) {
filename := viper.GetString("logfile")
filepath := utils.GetFullLogfilePath(filename)
filepath := config.GetLogfile()

content, err := ioutil.ReadFile(filepath)

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/logscmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var LogsCommand = &cobra.Command{
utils.NoEnvs()
}

envHomeDir := viper.GetString(fmt.Sprintf("environments.%s.home", activeEnv))
envHomeDir := viper.GetString(fmt.Sprintf("envs.%s.home", activeEnv))

err := os.Chdir(envHomeDir)

Expand Down
Loading

0 comments on commit b3ba402

Please sign in to comment.