mwdd.go 3.11 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
24
	"fmt"
	"os"

	"github.com/manifoldco/promptui"
25
	"github.com/spf13/cobra"
26
27
28
	"gitlab.wikimedia.org/releng/cli/internal/exec"
	"gitlab.wikimedia.org/releng/cli/internal/mwdd"
	"gitlab.wikimedia.org/releng/cli/internal/util/ports"
29
30
31
)

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
40
41
42
43
44
45
46
47
48
49
50
51
			if !NoInteraction {
				prompt := promptui.Prompt{
					Label:    "What port would you like to use for your development environment?",
					Default:  ports.FreeUpFrom("8080"),
					Validate: ports.IsValidAndFree,
				}
				value, err := prompt.Run()
				if err == nil {
					mwdd.Env().Set("PORT", value)
				} else {
					fmt.Println("Can't continue without a port")
					os.Exit(1)
				}
52
			} else {
53
				mwdd.Env().Set("PORT", ports.FreeUpFrom("8080"))
54
55
56
			}
		}
	},
57
58
59
60
61
62
63
64
65
66
67
68
}

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",
69
	Short: "Destroy the all containers",
70
	Run: func(cmd *cobra.Command, args []string) {
Addshore's avatar
Addshore committed
71
		options := exec.HandlerOptions{
72
			Verbosity: Verbosity,
Addshore's avatar
Addshore committed
73
		}
74
		mwdd.DefaultForUser().DownWithVolumesAndOrphans(options)
75
76
77
78
79
80
81
	},
}

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

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

func init() {
	mwddCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)")
103
	mwddCmd.PersistentFlags().BoolVarP(&NoInteraction, "no-interaction", "n", false, "Do not ask any interactive question")
104
105
106
107
108
109
110
111

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

	rootCmd.AddCommand(mwddCmd)
}