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

Commit

Permalink
add scripts command
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 24, 2020
1 parent 29c0574 commit d3fb9d7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/martinnirtl/dockma/internal/commands/profilecmd"
"github.com/martinnirtl/dockma/internal/commands/pscmd"
"github.com/martinnirtl/dockma/internal/commands/restartcmd"
"github.com/martinnirtl/dockma/internal/commands/scriptcmd"
"github.com/martinnirtl/dockma/internal/commands/testcmd"
"github.com/martinnirtl/dockma/internal/commands/upcmd"
"github.com/martinnirtl/dockma/internal/commands/versioncmd"
Expand Down Expand Up @@ -83,6 +84,7 @@ func initRootCmd() {
RootCommand.AddCommand(profilecmd.GetProfileCommand())
RootCommand.AddCommand(pscmd.GetPSCommand())
RootCommand.AddCommand(restartcmd.GetRestartCommand())
RootCommand.AddCommand(scriptcmd.GetScriptCommand())
RootCommand.AddCommand(upcmd.GetUpCommand())
RootCommand.AddCommand(versioncmd.GetVersionCommand())

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

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/martinnirtl/dockma/pkg/externalcommand"
"github.com/spf13/cobra"
)

// 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",
Example: "dockma script",
Args: cobra.ArbitraryArgs,
Run: runScriptCommand,
}
}

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

if activeEnv.GetName() == "-" {
utils.PrintNoActiveEnvSet()
}

envHomeDir := activeEnv.GetHomeDir()

err := os.Chdir(envHomeDir)
utils.ErrorAndExit(err)

var scriptname string
var scriptArgs []string
if len(args) > 0 {
scriptname = args[0]
scriptArgs = args[1:]

if !strings.HasSuffix(scriptname, ".sh") {
scriptname = scriptname + ".sh"
}
} else {
files, err := ioutil.ReadDir(filepath.Join(envHomeDir, "scripts"))
utils.ErrorAndExit(err)

scripts := make([]string, 0, len(files))
for _, file := range files {
if !file.IsDir() && len(file.Name()) > 0 {
scripts = append(scripts, file.Name())
}
}

scriptname = survey.Select("Select script to run", scripts)

addArgs := survey.Confirm("Input additional args", false)
if addArgs {
scriptArgsString := survey.Input("Input script arguments", "")
scriptArgs = strings.Split(scriptArgsString, " ")
}
}

baseCommand := fmt.Sprintf("./scripts/%s", scriptname)
command := externalcommand.JoinCommand(baseCommand, scriptArgs...)

_, err = externalcommand.Execute(command, nil, "")
utils.ErrorAndExit(err)

utils.Success(fmt.Sprintf("Executed script: %s", scriptname))
}

0 comments on commit d3fb9d7

Please sign in to comment.