diff --git a/docs/examples/provider.go b/docs/examples/provider.go new file mode 100644 index 000000000..6223a0f24 --- /dev/null +++ b/docs/examples/provider.go @@ -0,0 +1,74 @@ +/* + 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 main + +import ( + "fmt" + "os" + "time" + + "github.com/spf13/cobra" +) + +func main() { + cmd := &cobra.Command{ + Short: "Compose Provider Example", + Use: "demo", + } + cmd.AddCommand(composeCommand()) + err := cmd.Execute() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func composeCommand() *cobra.Command { + c := &cobra.Command{ + Use: "compose EVENT", + TraverseChildren: true, + } + c.PersistentFlags().String("project-name", "", "compose project name") // unused + c.AddCommand(&cobra.Command{ + Use: "up", + Run: up, + Args: cobra.ExactArgs(1), + }) + c.AddCommand(&cobra.Command{ + Use: "down", + Run: down, + Args: cobra.ExactArgs(1), + }) + return c +} + +const lineSeparator = "\n" + +func up(_ *cobra.Command, args []string) { + servicename := args[0] + fmt.Printf(`{ "type": "debug", "message": "Starting %s" }%s`, servicename, lineSeparator) + + for i := 0; i < 100; i += 10 { + time.Sleep(1 * time.Second) + fmt.Printf(`{ "type": "info", "message": "Processing ... %d%%" }%s`, i, lineSeparator) + } + fmt.Printf(`{ "type": "setenv", "message": "URL=https://magic.cloud/%s" }%s`, servicename, lineSeparator) +} + +func down(_ *cobra.Command, _ []string) { + fmt.Printf(`{ "type": "error", "message": "Permission error" }%s`, lineSeparator) +} diff --git a/docs/extension.md b/docs/extension.md index d01e6c6c7..f0d5cb9e9 100644 --- a/docs/extension.md +++ b/docs/extension.md @@ -104,4 +104,8 @@ into its runtime environment. ## Down lifecycle `down` lifecycle is equivalent to `up` with the ` compose --project-name down ` command. -The provider is responsible for releasing all resources associated with the service. \ No newline at end of file +The provider is responsible for releasing all resources associated with the service. + +## Examples + +See [example](examples/provider.go) for illustration on implementing this API in a command line \ No newline at end of file