Skip to content

Commit acad708

Browse files
authored
Merge pull request #448 from kool-dev/preset-octane
Preset for Laravel Octane
2 parents 319901e + 26c3b26 commit acad708

31 files changed

+444
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/kool
2+
/kool-cli
23
/dist/
34
/.env
45
Output/

commands/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ $ kool completion fish > ~/.config/fish/completions/kool.fish
8989
`,
9090
DisableFlagsInUseLine: true,
9191
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
92-
Args: cobra.ExactValidArgs(1),
92+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
9393
Hidden: true,
9494
RunE: DefaultCommandRunFunction(completion),
9595
}

commands/create.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"kool-dev/kool/core/environment"
66
"kool-dev/kool/core/presets"
77
"os"
8+
"path"
9+
"path/filepath"
810

911
"github.com/spf13/cobra"
1012
)
@@ -59,10 +61,18 @@ func (c *KoolCreate) Execute(args []string) (err error) {
5961

6062
c.Shell().Println("Initializing", preset, "preset...")
6163

64+
if !path.IsAbs(createDirectory) {
65+
if createDirectory, err = filepath.Abs(createDirectory); err != nil {
66+
return
67+
}
68+
}
69+
6270
if err = os.Chdir(createDirectory); err != nil {
6371
return
6472
}
6573

74+
c.env.Set("PWD", createDirectory)
75+
6676
if err = c.parser.Install(preset, c.Shell()); err != nil {
6777
return
6878
}

commands/root.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"kool-dev/kool/core/environment"
77
"kool-dev/kool/core/parser"
88
"kool-dev/kool/core/shell"
9+
"os"
910
"path"
11+
"path/filepath"
1012
"strings"
1113

1214
"github.com/spf13/cobra"
@@ -47,6 +49,8 @@ var version string = DEV_VERSION
4749

4850
var rootCmd = NewRootCmd(environment.NewEnvStorage())
4951

52+
var originalWorkingDir = ""
53+
5054
func init() {
5155
AddCommands(rootCmd)
5256
}
@@ -67,7 +71,7 @@ Complete documentation is available at https://kool.dev/docs`,
6771
Version: version,
6872
DisableAutoGenTag: true,
6973
DisableFlagsInUseLine: true,
70-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
74+
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
7175
if verbose := cmd.Flags().Lookup("verbose"); verbose != nil && verbose.Value.String() == "true" {
7276
env.Set("KOOL_VERBOSE", verbose.Value.String())
7377
}
@@ -76,6 +80,44 @@ Complete documentation is available at https://kool.dev/docs`,
7680
shell.NewShell().Warning("Warning: you are executing a development version of kool.")
7781
hasWarnedDevelopmentVersion = true
7882
}
83+
84+
workDirFlag := cmd.Flags().Lookup("working_dir")
85+
if workDirFlag != nil && workDirFlag.Value.String() != "" {
86+
workDir := workDirFlag.Value.String()
87+
88+
if originalWorkingDir != "" {
89+
// having an original working dir set means we have
90+
// already changed the working dir before and we are in
91+
// a recursive kool call. We need to restore the original
92+
// working dir before changing it again.
93+
if err = os.Chdir(originalWorkingDir); err != nil {
94+
return
95+
}
96+
}
97+
98+
if !path.IsAbs(workDir) {
99+
if workDir, err = filepath.Abs(workDir); err != nil {
100+
return
101+
}
102+
}
103+
104+
if err = os.Chdir(workDir); err != nil {
105+
return
106+
}
107+
108+
if originalWorkingDir == "" {
109+
// we only set the original working dir if it is not set
110+
// yet. This is to avoid overriding the original working
111+
// dir in recursive calls.
112+
if originalWorkingDir, err = os.Getwd(); err != nil {
113+
return
114+
}
115+
}
116+
117+
environment.NewEnvStorage().Set("PWD", workDir)
118+
}
119+
120+
return
79121
},
80122
RunE: func(cmd *cobra.Command, args []string) (err error) {
81123
if len(args) == 0 {
@@ -106,7 +148,8 @@ Complete documentation is available at https://kool.dev/docs`,
106148
},
107149
}
108150

109-
cmd.PersistentFlags().Bool("verbose", false, "increases output verbosity")
151+
cmd.PersistentFlags().Bool("verbose", false, "Increases output verbosity")
152+
cmd.PersistentFlags().StringP("working_dir", "w", "", "Changes the working directory for the command")
110153
return
111154
}
112155

docs/2-Presets/Laravel+Octane.md

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# Start a Laravel Octane Project with Docker in 3 Easy Steps
2+
3+
1. Run `kool create laravel+octane my-project`
4+
2. Update **.env.example**
5+
3. Run `kool run setup`
6+
7+
> Yes, using **kool** + Docker to create and work on new Laravel Octane projects is that easy!
8+
9+
## Requirements
10+
11+
If you haven't done so already, you first need to [install Docker and the kool CLI](/docs/getting-started/installation).
12+
13+
Also, make sure you're running the latest version of **kool**. Run the following command to compare your local version of **kool** with the latest release, and, if a newer version is available, automatically download and install it.
14+
15+
```bash
16+
$ kool self-update
17+
```
18+
19+
> Please note that it helps to have a basic understanding of how Docker and Docker Compose work to use Kool with Docker.
20+
21+
## 1. Run `kool create laravel+octane my-project`
22+
23+
Use the [`kool create PRESET FOLDER` command](/docs/commands/kool-create) to create your new Laravel Octane project:
24+
25+
```bash
26+
$ kool create laravel+octane my-project
27+
```
28+
29+
Under the hood, this command will run do the same as the standar Laravel preset to get a fresh install of latest Laravel using a customized **kool** Docker image with Swoole: <a href="https://github.com/kool-dev/docker-php-swoole" target="_blank">kooldev/php:8.1-nginx-swoole</a>.
30+
31+
After installing Laravel, `kool create` automatically requires `laravel/octane` and runs `artisan octane:install`. After that you will have the options for including a database or cache service, all of which helps you easily set up the initial tech stack for your project using an interactive wizard.
32+
33+
---
34+
35+
Now, move into your new Laravel Octane with Swoole project:
36+
37+
```bash
38+
$ cd my-project
39+
```
40+
41+
The [`kool preset` command](/docs/commands/kool-preset) auto-generated the following configuration files and added them to your project, which you can modify and extend.
42+
43+
```bash
44+
+docker-compose.yml
45+
+kool.yml
46+
```
47+
48+
> Now's a good time to review the **docker-compose.yml** file and verify the services match the choices you made earlier using the wizard.
49+
50+
## 2. Update .env.example
51+
52+
You need to update some default values in Laravel's **.env.example** file to match the services in your **docker-compose.yml** file.
53+
54+
### Database Services
55+
56+
MySQL 5.7 and 8.0 or MariaDB 10.5
57+
58+
```diff
59+
-DB_HOST=127.0.0.1
60+
+DB_HOST=database
61+
```
62+
63+
PostgreSQL 13.0
64+
65+
```diff
66+
-DB_CONNECTION=mysql
67+
+DB_CONNECTION=pgsql
68+
69+
-DB_HOST=127.0.0.1
70+
+DB_HOST=database
71+
72+
-DB_PORT=3306
73+
+DB_PORT=5432
74+
```
75+
76+
> In order to avoid permission issues with mysql and mariaDB, add a user other than root and a password to your **.env.example** file
77+
78+
```diff
79+
-DB_USERNAME=root
80+
+DB_USERNAME=<some_user>
81+
82+
-DB_PASSWORD=
83+
+DB_PASSWORD=<somepass>
84+
```
85+
86+
### Cache Services
87+
88+
Redis
89+
90+
```diff
91+
-REDIS_HOST=127.0.0.1
92+
+REDIS_HOST=cache
93+
```
94+
95+
Memcached
96+
97+
```diff
98+
-MEMCACHED_HOST=127.0.0.1
99+
+MEMCACHED_HOST=cache
100+
```
101+
102+
## 3. Run `kool run setup`
103+
104+
> Say hello to **kool.yml**, say goodbye to custom shell scripts!
105+
106+
As mentioned above, the [`kool preset` command](/docs/commands/kool-preset) added a **kool.yml** file to your project. Think of **kool.yml** as a super easy-to-use task _helper_. Instead of writing custom shell scripts, add your own scripts to **kool.yml** (under the `scripts` key), and run them with `kool run SCRIPT` (e.g. `kool run artisan`). You can add your own single line commands (see `composer` below), or add a list of commands that will be executed in sequence (see `setup` below).
107+
108+
To help get you started, **kool.yml** comes prebuilt with an initial set of scripts (based on the choices you made earlier using the **preset** wizard), including a script called `setup`, which helps you spin up a project for the first time.
109+
110+
```yaml
111+
scripts:
112+
artisan: kool exec app php artisan
113+
composer: kool exec app composer
114+
mysql: kool exec -e MYSQL_PWD=$DB_PASSWORD database mysql -uroot
115+
node: kool docker kooldev/node:16 node
116+
npm: kool docker kooldev/node:16 npm # or yarn
117+
npx: kool exec app npx
118+
119+
setup:
120+
- kool run before-start
121+
- kool start
122+
- kool run composer install
123+
- kool run artisan key:generate
124+
125+
reset:
126+
- kool run composer install
127+
- kool run artisan migrate:fresh --seed
128+
- kool run yarn install
129+
130+
before-start:
131+
- kool docker kooldev/bash -c "cp .env.example .env"
132+
- kool run yarn install
133+
```
134+
135+
Go ahead and run `kool run setup` to start your Docker environment and finish setting up your project:
136+
137+
```bash
138+
# CAUTION: this script will reset your `.env` file with `.env.example`
139+
$ kool run setup
140+
```
141+
142+
> As you can see in **kool.yml**, the `setup` script will do the following in sequence: copy your updated **.env.example** file to **.env**; start your Docker environment; use Composer to install vendor dependencies; generate your `APP_KEY` (in `.env`); and then build your Node packages and assets.
143+
144+
Once `kool run setup` finishes, you should be able to access your new site at [http://localhost](http://localhost) and see the Laravel welcome page. Hooray!
145+
146+
Verify your Docker container is running using the [`kool status` command](/docs/commands/kool-status).
147+
148+
Run `kool logs app` to see the logs from the running `app` container.
149+
150+
> Use `kool logs` to see the logs from all running containers. Add the `-f` option after `kool logs` to follow the logs (i.e. `kool logs -f app`).
151+
152+
---
153+
154+
### Run Commands in Docker Containers
155+
156+
Use [`kool exec`](/docs/commands/kool-exec) to execute a command inside a running service container:
157+
158+
```bash
159+
# kool exec [OPTIONS] SERVICE COMMAND [--] [ARG...]
160+
161+
$ kool exec app ls
162+
```
163+
164+
Try `kool run artisan --help` to execute the `kool exec app php artisan --help` command in your running `app` container and print out information about Laravel's CLI commands.
165+
166+
### Open Sessions in Docker Containers
167+
168+
Similar to SSH, if you want to open a Bash session in your `app` container, run `kool exec app bash`, where `app` is the name of the service container in **docker-compose.yml**. If you prefer, you can use `sh` instead of `bash` (`kool exec app sh`).
169+
170+
```bash
171+
$ kool exec app bash
172+
bash-5.1#
173+
174+
$ kool exec app sh
175+
/app #
176+
```
177+
178+
### Connect to Docker Database Container
179+
180+
You can easily start a new SQL client session inside your running `database` container by executing `kool run mysql` (MySQL) or `kool run psql` (PostgreSQL) in your terminal. This runs the single-line `mysql` or `psql` script included in your **kool.yml**.
181+
182+
### Access Private Repos and Packages in Docker Containers
183+
184+
If you need your `app` container to use your local SSH keys to pull private repositories and/or install private packages (which have been added as dependencies in your `composer.json` or `package.json` file), you can simply add `$HOME/.ssh:/home/kool/.ssh:delegated` under the `volumes` key of the `app` service in your **docker-compose.yml** file. This maps a `.ssh` folder in the container to the `.ssh` folder on your host machine.
185+
186+
```diff
187+
volumes:
188+
- .:/app:delegated
189+
+ - $HOME/.ssh:/home/kool/.ssh:delegated
190+
```
191+
192+
## Staying kool
193+
194+
When it's time to stop working on the project:
195+
196+
```bash
197+
$ kool stop
198+
```
199+
200+
And when you're ready to start work again:
201+
202+
```bash
203+
$ kool start
204+
```
205+
206+
## Additional Presets
207+
208+
We have more presets to help you start projects with **kool** in a standardized way across different frameworks.
209+
210+
- **[AdonisJs](/docs/2-Presets/AdonisJs.md)**
211+
- **[CodeIgniter](/docs/2-Presets/CodeIgniter.md)**
212+
- **[Express.js](/docs/2-Presets/ExpressJS.md)**
213+
- **[Hugo](/docs/2-Presets/Hugo.md)**
214+
- **[NestJS](/docs/2-Presets/NestJS.md)**
215+
- **[Next.js](/docs/2-Presets/NextJS.md)**
216+
- **[Node.js](/docs/2-Presets/NodeJS.md)**
217+
- **[Nuxt.js](/docs/2-Presets/NuxtJS.md)**
218+
- **[PHP](/docs/2-Presets/PHP.md)**
219+
- **[Symfony](/docs/2-Presets/Symfony.md)**
220+
- **[WordPress](/docs/2-Presets/WordPress.md)**
221+
222+
Missing a preset? **[Make a request](https://github.com/kool-dev/kool/issues/new)**, or contribute by opening a Pull Request. Go to [https://github.com/kool-dev/kool/tree/main/presets](https://github.com/kool-dev/kool/tree/main/presets) and browse the code to learn more about how presets work.

docs/2-Presets/Laravel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Use the [`kool create PRESET FOLDER` command](/docs/commands/kool-create) to cre
2626
$ kool create laravel my-project
2727
```
2828

29-
Under the hood, this command will run `composer create-project --no-install --no-scripts --prefer-dist laravel/laravel my-project` using a customized **kool** Docker image: <a href="https://github.com/kool-dev/docker-php" target="_blank">kooldev/php:7.4</a>.
29+
Under the hood, this command will run `composer create-project --no-install --no-scripts --prefer-dist laravel/laravel my-project` using a customized **kool** Docker image: <a href="https://github.com/kool-dev/docker-php" target="_blank">kooldev/php:8.1</a>.
3030

3131
After installing Laravel, `kool create` automatically runs the `kool preset laravel` command, which helps you easily set up the initial tech stack for your project using an interactive wizard.
3232

docs/4-Commands/0-kool.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ kool
1717
### Options
1818

1919
```
20-
-h, --help help for kool
21-
--verbose increases output verbosity
20+
-h, --help help for kool
21+
--verbose Increases output verbosity
22+
-w, --working_dir string Changes the working directory for the command
2223
```
2324

2425
### SEE ALSO

docs/4-Commands/kool-completion.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ kool completion [bash|zsh|fish|powershell]
6060
### Options inherited from parent commands
6161

6262
```
63-
--verbose increases output verbosity
63+
--verbose Increases output verbosity
64+
-w, --working_dir string Changes the working directory for the command
6465
```
6566

6667
### SEE ALSO

docs/4-Commands/kool-create.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ kool create PRESET FOLDER
1919
### Options inherited from parent commands
2020

2121
```
22-
--verbose increases output verbosity
22+
--verbose Increases output verbosity
23+
-w, --working_dir string Changes the working directory for the command
2324
```
2425

2526
### SEE ALSO

docs/4-Commands/kool-docker.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ kool docker [OPTIONS] IMAGE [COMMAND] [--] [ARG...]
2727
### Options inherited from parent commands
2828

2929
```
30-
--verbose increases output verbosity
30+
--verbose Increases output verbosity
31+
-w, --working_dir string Changes the working directory for the command
3132
```
3233

3334
### SEE ALSO

0 commit comments

Comments
 (0)