This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
146 lines (126 loc) · 3.73 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
package main
import (
"errors"
"log"
"os"
"github.com/docker/docker/client"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/craftcms/nitro/command/add"
"github.com/craftcms/nitro/command/apply"
"github.com/craftcms/nitro/command/clean"
"github.com/craftcms/nitro/command/completion"
"github.com/craftcms/nitro/command/composer"
"github.com/craftcms/nitro/command/context"
"github.com/craftcms/nitro/command/create"
"github.com/craftcms/nitro/command/database"
"github.com/craftcms/nitro/command/destroy"
"github.com/craftcms/nitro/command/hosts"
"github.com/craftcms/nitro/command/initialize"
"github.com/craftcms/nitro/command/npm"
"github.com/craftcms/nitro/command/queue"
"github.com/craftcms/nitro/command/restart"
"github.com/craftcms/nitro/command/ssh"
"github.com/craftcms/nitro/command/start"
"github.com/craftcms/nitro/command/stop"
"github.com/craftcms/nitro/command/trust"
"github.com/craftcms/nitro/command/update"
"github.com/craftcms/nitro/command/validate"
"github.com/craftcms/nitro/command/version"
"github.com/craftcms/nitro/config"
nitro "github.com/craftcms/nitro/pkg/client"
"github.com/craftcms/nitro/setup"
"github.com/craftcms/nitro/terminal"
)
var rootCommand = &cobra.Command{
Use: "nitro",
Short: "Local Craft CMS dev made easy",
Long: `Nitro is a command-line tool focused on making local Craft CMS development quick and easy.
Version: ` + version.Version,
RunE: rootMain,
SilenceUsage: true,
Version: version.Version,
}
func rootMain(command *cobra.Command, _ []string) error {
return command.Help()
}
func init() {
// set any global flags
flags := rootCommand.PersistentFlags()
// set a default environment if there is a variable
// set the default environment name
env := "nitro-dev"
if os.Getenv("NITRO_DEFAULT_ENVIRONMENT") != "" {
env = os.Getenv("NITRO_DEFAULT_ENVIRONMENT")
}
flags.StringP("environment", "e", env, "The environment")
// get the users home directory
home, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
// check for or create the config
if _, err := config.Load(home, env); err != nil {
if errors.Is(err, config.ErrNoConfigFile) {
// get the home directory
home, err := homedir.Dir()
if err != nil {
log.Fatal("unable to get the home directory, %w", err)
}
// setup the config file
if err := setup.FirstTime(home, env); err != nil {
log.Fatal(err)
}
}
}
// create the docker client
client, err := client.NewEnvClient()
if err != nil {
log.Fatal(err)
}
// get the port for the nitrod API
apiPort := "5000"
if os.Getenv("NITRO_API_PORT") != "" {
apiPort = os.Getenv("NITRO_API_PORT")
}
// create the nitrod gRPC API
nitrod, err := nitro.NewClient("127.0.0.1", apiPort)
if err != nil {
log.Fatal(err)
}
// create the "terminal" for capturing output
term := terminal.New()
// register all of the commands
commands := []*cobra.Command{
initialize.NewCommand(client, term),
hosts.New(home, term),
create.New(client, term),
add.New(home, client, term),
start.New(client, term),
stop.New(client, term),
queue.New(home, client, term),
ssh.New(home, client, term),
restart.New(client, term),
update.New(client, term),
destroy.New(client, term),
composer.NewCommand(client, term),
npm.New(client, term),
completion.New(),
apply.New(home, client, nitrod, term),
clean.NewCommand(home, client, term),
context.New(home, client, term),
trust.New(client, term),
version.New(client, nitrod, term),
validate.New(home, client, term),
database.New(home, client, term),
hosts.New(home, term),
}
// add the commands
rootCommand.AddCommand(commands...)
}
func main() {
// execute the root command
if err := rootCommand.Execute(); err != nil {
os.Exit(1)
}
}