Commit 629c3d1e authored by Addshore's avatar Addshore 🏄
Browse files

Add no-interaction option to prompt flows

parent 18545f5c
...@@ -36,17 +36,21 @@ var mwddCmd = &cobra.Command{ ...@@ -36,17 +36,21 @@ var mwddCmd = &cobra.Command{
mwdd := mwdd.DefaultForUser() mwdd := mwdd.DefaultForUser()
mwdd.EnsureReady() mwdd.EnsureReady()
if mwdd.Env().Missing("PORT") { if mwdd.Env().Missing("PORT") {
prompt := promptui.Prompt{ if !NoInteraction {
Label: "What port would you like to use for your development environment?", prompt := promptui.Prompt{
Default: ports.FreeUpFrom("8080"), Label: "What port would you like to use for your development environment?",
Validate: ports.IsValidAndFree, Default: ports.FreeUpFrom("8080"),
} Validate: ports.IsValidAndFree,
value, err := prompt.Run() }
if err == nil { value, err := prompt.Run()
mwdd.Env().Set("PORT", value) if err == nil {
mwdd.Env().Set("PORT", value)
} else {
fmt.Println("Can't continue without a port")
os.Exit(1)
}
} else { } else {
fmt.Println("Can't continue without a port") mwdd.Env().Set("PORT", ports.FreeUpFrom("8080"))
os.Exit(1)
} }
} }
}, },
...@@ -96,6 +100,7 @@ var mwddResumeCmd = &cobra.Command{ ...@@ -96,6 +100,7 @@ var mwddResumeCmd = &cobra.Command{
func init() { func init() {
mwddCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)") mwddCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)")
mwddCmd.PersistentFlags().BoolVarP(&NoInteraction, "no-interaction", "n", false, "Do not ask any interactive question")
mwddCmd.AddCommand(mwddWhereCmd) mwddCmd.AddCommand(mwddWhereCmd)
mwddCmd.AddCommand(mwddDestroyCmd) mwddCmd.AddCommand(mwddDestroyCmd)
......
...@@ -49,20 +49,23 @@ var mwddMediawikiCmd = &cobra.Command{ ...@@ -49,20 +49,23 @@ var mwddMediawikiCmd = &cobra.Command{
usrDir := usr.HomeDir usrDir := usr.HomeDir
if mwdd.Env().Missing("MEDIAWIKI_VOLUMES_CODE") { if mwdd.Env().Missing("MEDIAWIKI_VOLUMES_CODE") {
// Prompt the user for a directory or confirmation if !NoInteraction {
dirPrompt := promptui.Prompt{ // Prompt the user for a directory or confirmation
Label: "What directory would you like to store MediaWiki source code in?", dirPrompt := promptui.Prompt{
Default: mediawiki.GuessMediaWikiDirectoryBasedOnContext(), Label: "What directory would you like to store MediaWiki source code in?",
} Default: mediawiki.GuessMediaWikiDirectoryBasedOnContext(),
value, err := dirPrompt.Run() }
value, err := dirPrompt.Run()
if err == nil { if err == nil {
mwdd.Env().Set("MEDIAWIKI_VOLUMES_CODE", paths.FullifyUserProvidedPath(value)) mwdd.Env().Set("MEDIAWIKI_VOLUMES_CODE", paths.FullifyUserProvidedPath(value))
} else {
fmt.Println("Can't continue without a MediaWiki code directory")
os.Exit(1)
}
} else { } else {
fmt.Println("Can't continue without a MediaWiki code directory") mwdd.Env().Set("MEDIAWIKI_VOLUMES_CODE", mediawiki.GuessMediaWikiDirectoryBasedOnContext())
os.Exit(1)
} }
} }
// Default the mediawiki container to a .composer directory in the running users home dir // Default the mediawiki container to a .composer directory in the running users home dir
...@@ -85,58 +88,72 @@ var mwddMediawikiCmd = &cobra.Command{ ...@@ -85,58 +88,72 @@ var mwddMediawikiCmd = &cobra.Command{
// TODO ask if they want to get any more skins and extensions? // TODO ask if they want to get any more skins and extensions?
// TODO async cloning of repos for speed! // TODO async cloning of repos for speed!
if !mediawiki.MediaWikiIsPresent() { if !mediawiki.MediaWikiIsPresent() {
cloneMwPrompt := promptui.Prompt{ if !NoInteraction {
Label: "MediaWiki code not detected in " + mwdd.Env().Get("MEDIAWIKI_VOLUMES_CODE") + ". Do you want to clone it now?", cloneMwPrompt := promptui.Prompt{
IsConfirm: true, Label: "MediaWiki code not detected in " + mwdd.Env().Get("MEDIAWIKI_VOLUMES_CODE") + ". Do you want to clone it now?",
IsConfirm: true,
}
_, err := cloneMwPrompt.Run()
setupOpts.GetMediaWiki = err == nil
} else {
setupOpts.GetMediaWiki = true
} }
_, err := cloneMwPrompt.Run()
setupOpts.GetMediaWiki = err == nil
} }
if !mediawiki.VectorIsPresent() { if !mediawiki.VectorIsPresent() {
cloneMwPrompt := promptui.Prompt{ if !NoInteraction {
Label: "Vector skin is not detected in " + mwdd.Env().Get("MEDIAWIKI_VOLUMES_CODE") + ". Do you want to clone it from Gerrit?", cloneMwPrompt := promptui.Prompt{
IsConfirm: true, Label: "Vector skin is not detected in " + mwdd.Env().Get("MEDIAWIKI_VOLUMES_CODE") + ". Do you want to clone it from Gerrit?",
IsConfirm: true,
}
_, err := cloneMwPrompt.Run()
setupOpts.GetVector = err == nil
} else {
setupOpts.GetVector = true
} }
_, err := cloneMwPrompt.Run()
setupOpts.GetVector = err == nil
} }
if setupOpts.GetMediaWiki || setupOpts.GetVector { if setupOpts.GetMediaWiki || setupOpts.GetVector {
cloneFromGithubPrompt := promptui.Prompt{ if !NoInteraction {
Label: "Do you want to clone from Github for extra speed? (your git remotes will be switched to Gerrit after download)", cloneFromGithubPrompt := promptui.Prompt{
IsConfirm: true, Label: "Do you want to clone from Github for extra speed? (your git remotes will be switched to Gerrit after download)",
} IsConfirm: true,
_, err := cloneFromGithubPrompt.Run() }
setupOpts.UseGithub = err == nil _, err := cloneFromGithubPrompt.Run()
setupOpts.UseGithub = err == nil
cloneShallowPrompt := promptui.Prompt{ cloneShallowPrompt := promptui.Prompt{
Label: "Do you want to use shallow clones for extra speed? (You can fetch all history later using `git fetch --unshallow`)", Label: "Do you want to use shallow clones for extra speed? (You can fetch all history later using `git fetch --unshallow`)",
IsConfirm: true, IsConfirm: true,
} }
_, err = cloneShallowPrompt.Run() _, err = cloneShallowPrompt.Run()
setupOpts.UseShallow = err == nil setupOpts.UseShallow = err == nil
finalRemoteTypePrompt := promptui.Prompt{ finalRemoteTypePrompt := promptui.Prompt{
Label: "How do you want to interact with Gerrit for the cloned repositores? (http or ssh)", Label: "How do you want to interact with Gerrit for the cloned repositores? (http or ssh)",
Default: "ssh", Default: "ssh",
}
remoteType, err := finalRemoteTypePrompt.Run()
if err != nil || (remoteType != "ssh" && remoteType != "http") {
fmt.Println("Invalid Gerrit interaction type chosen.")
os.Exit(1)
}
setupOpts.GerritInteractionType = remoteType
if remoteType == "ssh" {
gerritUsernamePrompt := promptui.Prompt{
Label: "What is your Gerrit username?",
} }
gerritUsername, err := gerritUsernamePrompt.Run() remoteType, err := finalRemoteTypePrompt.Run()
if err != nil || len(gerritUsername) < 1 { if err != nil || (remoteType != "ssh" && remoteType != "http") {
fmt.Println("Gerrit username required for ssh interaction type.") fmt.Println("Invalid Gerrit interaction type chosen.")
os.Exit(1) os.Exit(1)
} }
setupOpts.GerritUsername = gerritUsername setupOpts.GerritInteractionType = remoteType
if remoteType == "ssh" {
gerritUsernamePrompt := promptui.Prompt{
Label: "What is your Gerrit username?",
}
gerritUsername, err := gerritUsernamePrompt.Run()
if err != nil || len(gerritUsername) < 1 {
fmt.Println("Gerrit username required for ssh interaction type.")
os.Exit(1)
}
setupOpts.GerritUsername = gerritUsername
}
} else {
setupOpts.UseGithub = true
setupOpts.UseShallow = true
// Default is ssh, but http is the only non interactive choice we can make here..
setupOpts.GerritInteractionType = "http"
} }
setupOpts.UseShallow = err == nil
} }
if setupOpts.GetMediaWiki || setupOpts.GetVector { if setupOpts.GetMediaWiki || setupOpts.GetVector {
......
...@@ -29,8 +29,8 @@ import ( ...@@ -29,8 +29,8 @@ import (
// Verbosity indicating verbose mode. // Verbosity indicating verbose mode.
var Verbosity int var Verbosity int
// NonInteractive skips prompts with a yes // NoInteraction skips prompts with a yes or default values
var NonInteractive bool var NoInteraction bool
// These vars are currently used by the docker exec command // These vars are currently used by the docker exec command
......
...@@ -38,19 +38,23 @@ var updateCmd = &cobra.Command{ ...@@ -38,19 +38,23 @@ var updateCmd = &cobra.Command{
fmt.Println("New update found: " + toUpdateToOrMessage) fmt.Println("New update found: " + toUpdateToOrMessage)
updatePrompt := promptui.Prompt{ if !NoInteraction {
Label: " Do you want to update?", updatePrompt := promptui.Prompt{
IsConfirm: true, Label: " Do you want to update?",
} IsConfirm: true,
_, err := updatePrompt.Run() }
if err == nil { _, err := updatePrompt.Run()
// Note: technically there is a small race condition here, and we might update to a newer version if it was release between stages if err != nil {
updateSuccess, updateMessage := updater.Update(Version, GitSummary, Verbosity >= 2) return
fmt.Println(updateMessage)
if !updateSuccess {
os.Exit(1)
} }
} }
// Technically there is a small race condition here, and we might update to a newer version if it was release between stages
updateSuccess, updateMessage := updater.Update(Version, GitSummary, Verbosity >= 2)
fmt.Println(updateMessage)
if !updateSuccess {
os.Exit(1)
}
}, },
} }
...@@ -58,4 +62,5 @@ func init() { ...@@ -58,4 +62,5 @@ func init() {
rootCmd.AddCommand(updateCmd) rootCmd.AddCommand(updateCmd)
updateCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)") updateCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)")
updateCmd.PersistentFlags().BoolVarP(&NoInteraction, "no-interaction", "n", false, "Do not ask any interactive question")
} }
...@@ -13,6 +13,9 @@ trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT ...@@ -13,6 +13,9 @@ trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# Run this integration test using a non standard port # Run this integration test using a non standard port
./bin/mw docker env set PORT 9191 ./bin/mw docker env set PORT 9191
# And already fill in the location of mediawiki
./bin/mw docker env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
# So we should get no prompts, even though we don't pass --no-interaction
# Create # Create
./bin/mw docker mediawiki create ./bin/mw docker mediawiki create
......
...@@ -9,10 +9,13 @@ trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG ...@@ -9,10 +9,13 @@ trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# Setup the default hosts in hosts file # Setup the default hosts in hosts file
./bin/mw docker hosts add # --no-interaction, as the first time we run a docker command it'll prompt us for the port, default selected is 8080
./bin/mw docker hosts add --no-interaction
# Create # Create, from the mediawiki dir, to allow --no-interaction to detect the existing mediawiki directory, setting the volume path
./bin/mw docker mediawiki create cd ./mediawiki
./../bin/mw docker mediawiki create --no-interaction
cd ./..
# Create: Validate the basic stuff # Create: Validate the basic stuff
./bin/mw docker docker-compose ps ./bin/mw docker docker-compose ps
......
...@@ -8,6 +8,10 @@ trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG ...@@ -8,6 +8,10 @@ trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting # echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# Set some corret values so we don't get asked
./bin/mw docker env set PORT 8080
./bin/mw docker env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
# Setup the default hosts in hosts file # Setup the default hosts in hosts file
./bin/mw docker hosts add ./bin/mw docker hosts add
......
...@@ -10,13 +10,6 @@ docker-compose version ...@@ -10,13 +10,6 @@ docker-compose version
# Output CLI version # Output CLI version
./bin/mw version ./bin/mw version
# Setup things that otherwise need user input
./bin/mw docker env set PORT 8080
./bin/mw docker env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
# And output their values
./bin/mw docker env list
cat $(./bin/mw docker env where)
# Create a fresh LocalSettings.php file # Create a fresh LocalSettings.php file
rm -f mediawiki/LocalSettings.php rm -f mediawiki/LocalSettings.php
echo "<?php" >> mediawiki/LocalSettings.php echo "<?php" >> mediawiki/LocalSettings.php
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment