-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce experimental watch command (skeletton)
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
- Loading branch information
Showing
14 changed files
with
284 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// alphaCommand groups all experimental subcommands | ||
func alphaCommand(p *ProjectOptions, backend api.Service) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Short: "Experimental commands", | ||
Use: "alpha [COMMAND]", | ||
Hidden: true, | ||
} | ||
cmd.AddCommand(watchCommand(p, backend)) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type watchOptions struct { | ||
*ProjectOptions | ||
quiet bool | ||
} | ||
|
||
func watchCommand(p *ProjectOptions, backend api.Service) *cobra.Command { | ||
opts := watchOptions{ | ||
ProjectOptions: p, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "watch [SERVICE...]", | ||
Short: "EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated", | ||
PreRunE: Adapt(func(ctx context.Context, args []string) error { | ||
return nil | ||
}), | ||
RunE: Adapt(func(ctx context.Context, args []string) error { | ||
return runWatch(ctx, backend, opts, args) | ||
}), | ||
ValidArgsFunction: completeServiceNames(p), | ||
} | ||
|
||
cmd.Flags().BoolVar(&opts.quiet, "quiet", false, "hide build output") | ||
return cmd | ||
} | ||
|
||
func runWatch(ctx context.Context, backend api.Service, opts watchOptions, services []string) error { | ||
fmt.Fprintln(os.Stderr, "watch command is EXPERIMENTAL") | ||
project, err := opts.ToProject(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return backend.Watch(ctx, project, services, api.WatchOptions{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# docker compose alpha | ||
|
||
<!---MARKER_GEN_START--> | ||
Experimental commands | ||
|
||
### Subcommands | ||
|
||
| Name | Description | | ||
|:----------------------------------|:-----------------------------------------------------------------------------------------------------| | ||
| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated | | ||
|
||
|
||
|
||
<!---MARKER_GEN_END--> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# docker compose alpha watch | ||
|
||
<!---MARKER_GEN_START--> | ||
EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated | ||
|
||
### Options | ||
|
||
| Name | Type | Default | Description | | ||
|:----------|:-----|:--------|:------------------| | ||
| `--quiet` | | | hide build output | | ||
|
||
|
||
<!---MARKER_GEN_END--> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
command: docker compose alpha | ||
short: Experimental commands | ||
long: Experimental commands | ||
pname: docker compose | ||
plink: docker_compose.yaml | ||
cname: | ||
- docker compose alpha watch | ||
clink: | ||
- docker_compose_alpha_watch.yaml | ||
deprecated: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
command: docker compose alpha watch | ||
short: | | ||
EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated | ||
long: | | ||
EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated | ||
usage: docker compose alpha watch [SERVICE...] | ||
pname: docker compose alpha | ||
plink: docker_compose_alpha.yaml | ||
options: | ||
- option: quiet | ||
value_type: bool | ||
default_value: "false" | ||
description: hide build output | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
deprecated: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/compose-spec/compose-go/types" | ||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/pkg/errors" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type DevelopmentConfig struct { | ||
} | ||
|
||
func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error { | ||
fmt.Fprintln(s.stderr(), "not implemented yet") | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
err := project.WithServices(services, func(service types.ServiceConfig) error { | ||
var config DevelopmentConfig | ||
if y, ok := service.Extensions["x-develop"]; ok { | ||
err := mapstructure.Decode(y, &config) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if service.Build == nil { | ||
return errors.New("can't watch a service without a build section") | ||
} | ||
context := service.Build.Context | ||
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("watching " + context) | ||
err = watcher.Add(context) | ||
if err != nil { | ||
return err | ||
} | ||
eg.Go(func() error { | ||
defer watcher.Close() //nolint:errcheck | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case event := <-watcher.Events: | ||
log.Println("fs event :", event.String()) | ||
case err := <-watcher.Errors: | ||
return err | ||
} | ||
} | ||
}) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return eg.Wait() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.