This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29c0574
commit d3fb9d7
Showing
2 changed files
with
79 additions
and
0 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
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,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)) | ||
} |