Skip to content

Commit a1d7416

Browse files
committed
example provider implementation
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 23fc76a commit a1d7416

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

docs/examples/provider.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
func up(_ *cobra.Command, args []string) {
60+
servicename := args[0]
61+
for i := 0; i < 100; i += 10 {
62+
time.Sleep(1 * time.Second)
63+
fmt.Printf(`{ "type": "info", "message": "Processing ... %d%%" }\n`, i)
64+
}
65+
fmt.Printf(`{ "type": "setenv", "message": "URL=https://magic.cloud/%s" }\n`, servicename)
66+
}
67+
68+
func down(_ *cobra.Command, _ []string) {
69+
fmt.Printf(`{ "type": "error", "message": "Permission error" }\n`)
70+
}

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)