mwdd.go 2.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*Package cmd is used for command line.

Copyright © 2020 Addshore

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd

import (
21
22
23
	"fmt"
	"os"

Addshore's avatar
Addshore committed
24
	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/exec"
25
	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/mwdd"
26
	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/util/ports"
27
	"github.com/manifoldco/promptui"
28
29
30
31
	"github.com/spf13/cobra"
)

var mwddCmd = &cobra.Command{
32
	Use:   "docker",
33
34
	Short: "The MediaWiki-Docker-Dev like development environment",
	RunE:  nil,
35
36
37
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		mwdd := mwdd.DefaultForUser()
		mwdd.EnsureReady()
38
		if mwdd.Env().Missing("PORT") {
39
			prompt := promptui.Prompt{
40
41
42
				Label:    "What port would you like to use for your development environment?",
				Default:  ports.FreeUpFrom("8080"),
				Validate: ports.IsValidAndFree,
43
44
45
			}
			value, err := prompt.Run()
			if err == nil {
46
				mwdd.Env().Set("PORT", value)
47
48
49
50
51
52
			} else {
				fmt.Println("Can't continue without a port")
				os.Exit(1)
			}
		}
	},
53
54
55
56
57
58
59
60
61
62
63
64
}

var mwddWhereCmd = &cobra.Command{
	Use:   "where",
	Short: "States the working directory for the environment",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(mwdd.DefaultForUser().Directory())
	},
}

var mwddDestroyCmd = &cobra.Command{
	Use:   "destroy",
65
	Short: "Destroy the all containers",
66
	Run: func(cmd *cobra.Command, args []string) {
Addshore's avatar
Addshore committed
67
		options := exec.HandlerOptions{
68
			Verbosity: Verbosity,
Addshore's avatar
Addshore committed
69
		}
70
		mwdd.DefaultForUser().DownWithVolumesAndOrphans(options)
71
72
73
74
75
76
77
	},
}

var mwddSuspendCmd = &cobra.Command{
	Use:   "suspend",
	Short: "Suspend the Default containers",
	Run: func(cmd *cobra.Command, args []string) {
78
		options := exec.HandlerOptions{
79
			Verbosity: Verbosity,
80
		}
81
		mwdd.DefaultForUser().Stop([]string{}, options)
82
83
84
85
86
87
88
	},
}

var mwddResumeCmd = &cobra.Command{
	Use:   "resume",
	Short: "Resume the Default containers",
	Run: func(cmd *cobra.Command, args []string) {
89
		options := exec.HandlerOptions{
90
			Verbosity: Verbosity,
91
		}
92
93
		fmt.Println("Any services that you have not already created will show as 'failed'")
		mwdd.DefaultForUser().Start([]string{}, options)
94
95
96
97
98
99
100
101
102
103
104
105
106
	},
}

func init() {
	mwddCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)")

	mwddCmd.AddCommand(mwddWhereCmd)
	mwddCmd.AddCommand(mwddDestroyCmd)
	mwddCmd.AddCommand(mwddSuspendCmd)
	mwddCmd.AddCommand(mwddResumeCmd)

	rootCmd.AddCommand(mwddCmd)
}