Skip to content

Commit 12e5597

Browse files
authored
Add a new flag to add custom labels (#128)
* Rename git module * Add custom labels * Update README
1 parent 87eaeec commit 12e5597

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ $ builder-playground cook l1 --latest-fork --output ~/my-builder-testnet --genes
6565
- `--watchdog` (bool): Enable the watchdog service to monitor the specific chain
6666
- `--dry-run` (bool): Generates the artifacts and manifest but does not deploy anything (also enabled with the `--mise-en-place` flag)
6767
- `--log-level` (string): Log level to use (debug, info, warn, error, fatal). Defaults to `info`.
68+
- `--labels` (key=val): Custom labels to apply to your deployment.
6869

6970
To stop the playground, press `Ctrl+C`.
7071

internal/local_runner.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ type LocalRunner struct {
7474

7575
// networkName is the name of the network to use for the services
7676
networkName string
77+
78+
// labels is the list of labels to apply to each resource being created
79+
labels map[string]string
7780
}
7881

7982
type task struct {
@@ -104,7 +107,7 @@ func newDockerClient() (*client.Client, error) {
104107
}
105108

106109
// TODO: add a runner config struct
107-
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string) (*LocalRunner, error) {
110+
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string) (*LocalRunner, error) {
108111
client, err := newDockerClient()
109112
if err != nil {
110113
return nil, fmt.Errorf("failed to create docker client: %w", err)
@@ -209,6 +212,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
209212
sessionID: uuid.New().String(),
210213
networkName: networkName,
211214
instances: instances,
215+
labels: labels,
212216
}
213217

214218
if interactive {
@@ -594,6 +598,11 @@ func (d *LocalRunner) toDockerComposeService(s *Service) (map[string]interface{}
594598
"service": s.Name,
595599
}
596600

601+
// apply the user defined labels
602+
for k, v := range d.labels {
603+
labels[k] = v
604+
}
605+
597606
// add the local ports exposed by the service as labels
598607
// we have to do this for now since we do not store the manifest in JSON yet.
599608
// Otherwise, we could use that directly

internal/utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package internal
33
import (
44
"fmt"
55
"strconv"
6+
"strings"
67
)
78

89
type nullableUint64Value struct {
@@ -37,3 +38,33 @@ func (n nullableUint64Value) Type() string {
3738
func (n nullableUint64Value) GetNoOptDefVal() string {
3839
return "0"
3940
}
41+
42+
type MapStringFlag map[string]string
43+
44+
func (n *MapStringFlag) String() string {
45+
parts := []string{}
46+
for k, v := range *n {
47+
parts = append(parts, k+"="+v)
48+
}
49+
return "(" + strings.Join(parts, ",") + ")"
50+
}
51+
52+
func (n *MapStringFlag) Type() string {
53+
return "map(string, string)"
54+
}
55+
56+
func (n *MapStringFlag) Set(s string) error {
57+
parts := strings.Split(s, "=")
58+
if len(parts) != 2 {
59+
return fmt.Errorf("expected k=v for flag")
60+
}
61+
62+
k := parts[0]
63+
v := parts[1]
64+
65+
if *n == nil {
66+
(*n) = map[string]string{}
67+
}
68+
(*n)[k] = v
69+
return nil
70+
}

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var logLevelFlag string
2727
var bindExternal bool
2828
var withPrometheus bool
2929
var networkName string
30+
var labels internal.MapStringFlag
3031

3132
var rootCmd = &cobra.Command{
3233
Use: "playground",
@@ -173,7 +174,7 @@ func main() {
173174
recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface")
174175
recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics")
175176
recipeCmd.Flags().StringVar(&networkName, "network", "", "network name")
176-
177+
recipeCmd.Flags().Var(&labels, "labels", "list of labels to apply to the resources")
177178
cookCmd.AddCommand(recipeCmd)
178179
}
179180

@@ -251,7 +252,7 @@ func runIt(recipe internal.Recipe) error {
251252
}
252253
}
253254

254-
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName)
255+
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels)
255256
if err != nil {
256257
return fmt.Errorf("failed to create docker runner: %w", err)
257258
}

0 commit comments

Comments
 (0)