-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·215 lines (181 loc) · 5.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/abhisekmazumdar/idk/runner"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "idk",
Usage: "replace idk with dev tool followed by the arguments & flags",
Description: "idk - Helper CLI that knows for you and runs command as lando OR ddev.",
Commands: []*cli.Command{
{
Name: "composer",
Aliases: []string{"c"},
Usage: "runs as ddev/lando composer ...",
Action: cmdComposer,
},
{
Name: "drush",
Aliases: []string{"d"},
Usage: "runs as ddev/lando drush ...",
Action: cmdDrush,
},
{
Name: "create-project",
Usage: "runs as ddev/lando composer create-project ...",
Action: func(cCtx *cli.Context) error {
cmdComposerBased("create-project ", cCtx)
return nil
},
},
{
Name: "install",
Aliases: []string{"i"},
Usage: "runs as ddev/lando composer install ...",
Action: func(cCtx *cli.Context) error {
cmdComposerBased("install", cCtx)
return nil
},
},
{
Name: "require",
Aliases: []string{"r"},
Usage: "runs as ddev/lando composer require ...",
Action: func(cCtx *cli.Context) error {
cmdComposerBased("require", cCtx)
return nil
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "runs as ddev/lando composer update ...",
Action: func(cCtx *cli.Context) error {
cmdComposerBased("update", cCtx)
return nil
},
},
{
Name: "site-install",
Aliases: []string{"si"},
Usage: "runs as ddev/lando drush site-install profile_name",
Action: func(cCtx *cli.Context) error {
cmdDrushBased("site-install", cCtx)
return nil
},
},
{
Name: "pm:enable",
Aliases: []string{"en"},
Usage: "runs as ddev/lando drush pm-enable module_name",
Action: func(cCtx *cli.Context) error {
cmdDrushBased("pm:enable", cCtx)
return nil
},
},
{
Name: "pm:uninstall",
Aliases: []string{"pmu"},
Usage: "runs as ddev/lando drush pm-uninstall module_name",
Action: func(cCtx *cli.Context) error {
cmdDrushBased("pm:uninstall", cCtx)
return nil
},
},
{
Name: "cache:rebuild",
Aliases: []string{"cr"},
Usage: "runs as ddev/lando drush cache:rebuild",
Action: func(cCtx *cli.Context) error {
cmdDrushBased("cache:rebuild", cCtx)
return nil
},
},
},
Authors: []*cli.Author{
{Name: "Abhisek Mazumdar (abhisekmazumdar)"},
},
Action: mainAction,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
// Handle when user is not very lazy.
func mainAction(cCtx *cli.Context) error {
devTool, strArr := helper(cCtx)
runner.Run(devTool, strArr...)
return nil
}
// Handle any command which has composer as first argument.
func cmdComposer(cCtx *cli.Context) error {
devTool, strArr := helper(cCtx)
strArr = append([]string{"composer"}, strArr...)
runner.Run(devTool, strArr...)
return nil
}
// Handle any command which has drush as first argument.
func cmdDrush(cCtx *cli.Context) error {
devTool, strArr := helper(cCtx)
strArr = append([]string{"drush"}, strArr...)
runner.Run(devTool, strArr...)
return nil
}
// Handle any command which has a composer's second argument and idk sub-command.
func cmdComposerBased(subCmd string, cCtx *cli.Context) error {
devTool, strArr := helper(cCtx)
strArr = append([]string{"composer", subCmd}, strArr...)
runner.Run(devTool, strArr...)
return nil
}
// Handle any command which has a drush's second argument and idk sub-command.
func cmdDrushBased(subCmd string, cCtx *cli.Context) error {
devTool, strArr := helper(cCtx)
strArr = append([]string{"drush", subCmd}, strArr...)
runner.Run(devTool, strArr...)
return nil
}
// Helper for all commands.
func helper(cCtx *cli.Context) (string, []string) {
// Make array of stings for the arguments.
args := cCtx.Args()
var strArr []string
for i := 0; i < args.Len(); i++ {
strArr = append(strArr, args.Get(i))
}
fmt.Println("Checking which dev tool configuration files are present...")
fmt.Println()
devTool := ""
devTool = checkForDevTool()
return devTool, strArr
}
// Check for dev tool's(ddev or lando) config files are present.
func checkForDevTool() string {
reader := bufio.NewReader(os.Stdin)
if _, err := os.Stat("./.ddev"); !os.IsNotExist(err) {
if _, err := os.Stat("./.lando.yml"); !os.IsNotExist(err) {
fmt.Print("Both .lando.yml and .ddev directories exist.\nWhich one would you like to use? Type 'lando' or 'ddev': ")
option, _ := reader.ReadString('\n')
option = strings.ToLower(strings.TrimSpace(option))
if option != "ddev" && option != "lando" {
fmt.Println("You 🫵 spoiled all the fun.\nInvalid input, Try Again!!")
os.Exit(1)
}
return option
} else {
return "ddev"
}
} else if _, err := os.Stat("./.lando.yml"); !os.IsNotExist(err) {
return "lando"
} else {
fmt.Println("No ddev or lando configuration files found 🙁")
os.Exit(1)
}
return ""
}