Skip to content

Commit 8a2cb90

Browse files
committed
example provider implementation
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent cc50ada commit 8a2cb90

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

docs/examples/provider.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"time"
23+
24+
"github.com/spf13/cobra"
25+
)
26+
27+
func main() {
28+
cmd := &cobra.Command{
29+
Short: "Compose Provider Example",
30+
Use: "demo",
31+
}
32+
cmd.AddCommand(composeCommand())
33+
err := cmd.Execute()
34+
if err != nil {
35+
fmt.Fprintln(os.Stderr, err)
36+
os.Exit(1)
37+
}
38+
}
39+
40+
func composeCommand() *cobra.Command {
41+
c := &cobra.Command{
42+
Use: "compose EVENT",
43+
TraverseChildren: true,
44+
}
45+
c.PersistentFlags().String("project-name", "", "compose project name") // unused
46+
c.AddCommand(&cobra.Command{
47+
Use: "up",
48+
Run: up,
49+
Args: cobra.ExactArgs(1),
50+
})
51+
c.AddCommand(&cobra.Command{
52+
Use: "down",
53+
Run: down,
54+
Args: cobra.ExactArgs(1),
55+
})
56+
return c
57+
}
58+
59+
const lineSeparator = "\n"
60+
61+
func up(_ *cobra.Command, args []string) {
62+
servicename := args[0]
63+
fmt.Printf(`{ "type": "debug", "message": "Starting %s" }%s`, servicename, lineSeparator)
64+
65+
for i := 0; i < 100; i += 10 {
66+
time.Sleep(1 * time.Second)
67+
fmt.Printf(`{ "type": "info", "message": "Processing ... %d%%" }%s`, i, lineSeparator)
68+
}
69+
fmt.Printf(`{ "type": "setenv", "message": "URL=https://magic.cloud/%s" }%s`, servicename, lineSeparator)
70+
}
71+
72+
func down(_ *cobra.Command, _ []string) {
73+
fmt.Printf(`{ "type": "error", "message": "Permission error" }%s`, lineSeparator)
74+
}

docs/extension.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ into its runtime environment.
104104
## Down lifecycle
105105

106106
`down` lifecycle is equivalent to `up` with the `<provider> compose --project-name <NAME> down <SERVICE>` command.
107-
The provider is responsible for releasing all resources associated with the service.
107+
The provider is responsible for releasing all resources associated with the service.
108+
109+
## Examples
110+
111+
See [example](examples/provider.go) for illustration on implementing this API in a command line

0 commit comments

Comments
 (0)