Skip to content

cli: all-markers

Back to gallery

Exhibit path: docs/examples/codegen/cli/all-markers/

Every +glacier: marker valid on a command struct, in one place. Use this as a reference when checking which markers are supported and how they translate to cli.With* calls in the generated file.

Input (in.go)

go
//go:build glacier_codegen_fixture

// Package main is the entry point for the myapp CLI.
package main

import (
	"context"
	"errors"
)

// MyApp is the root command.
//
// +glacier:command name=myapp
// +glacier:root
type MyApp struct{}

// Run is a no-op; subcommands do the work.
func (MyApp) Run(_ context.Context) error { return nil }

// DeployCmd deploys the application. It demonstrates every supported marker.
//
// +glacier:command name=deploy parent=myapp
type DeployCmd struct {
	// Env is the target environment.
	//
	// +glacier:required
	// +glacier:choices staging|production
	// +glacier:short e
	// +glacier:env DEPLOY_ENV
	Env string

	// Parallel controls the number of concurrent deployments.
	//
	// +glacier:default 2
	Parallel int

	// DryRun previews changes without applying them.
	//
	// +glacier:default false
	DryRun bool

	// Strategy is the rollout strategy.
	//
	// +glacier:choices rolling|blue-green|canary
	// +glacier:default rolling
	// +glacier:validate validateStrategy
	Strategy string

	// OldFlag is kept for backward compatibility.
	//
	// +glacier:deprecated Use --strategy instead.
	OldFlag string

	// Target is the positional deployment target (e.g. a cluster name).
	//
	// +glacier:positional
	Target string
}

// Run deploys the application.
func (c *DeployCmd) Run(_ context.Context) error {
	// implementation goes here
	return nil
}

// validateStrategy rejects unknown strategy strings.
func validateStrategy(s string) error {
	switch s {
	case "rolling", "blue-green", "canary":
		return nil
	}
	return errors.New("unknown strategy: " + s)
}

Output (out.go)

go
//go:build glacier_codegen_fixture

// Code generated by glaciergen. DO NOT EDIT.
// Source: in.go
package main

import (
	"github.com/nathanbrophy/glacier/cli"
)

func init() {
	cli.Default.Register(
		cli.WithName("myapp"),
		cli.WithRoot(),
		cli.WithRunner(func() cli.Runner { return &MyApp{} }),
	)
	cli.Default.Register(
		cli.WithName("deploy"),
		cli.WithParent("myapp"),
		cli.WithRunner(func() cli.Runner { return &DeployCmd{} }),
		cli.WithFlagRequired("Env"),
		cli.WithFlagChoices("Env", []string{"staging", "production"}),
		cli.WithFlagShort("Env", 'e'),
		cli.WithFlagEnv("Env", "DEPLOY_ENV"),
		cli.WithFlagDefault("Parallel", 2),
		cli.WithFlagDefault("DryRun", false),
		cli.WithFlagChoices("Strategy", []string{"rolling", "blue-green", "canary"}),
		cli.WithFlagDefault("Strategy", "rolling"),
		cli.WithFlagValidate("Strategy", validateStrategy),
		cli.WithFlagDeprecated("OldFlag", "Use --strategy instead."),
		cli.WithPositional("Target"),
	)
}

What the generator did

Markers demonstrated and their generated output:

MarkerGenerated call
+glacier:command name=<verb> parent=<parent>cli.WithName("<verb>"), parent linkage
+glacier:default <value>cli.WithFlagDefault("<Field>", <value>)
+glacier:short <char>cli.WithFlagShort("<Field>", '<char>')
+glacier:env <KEY>cli.WithFlagEnv("<Field>", "<KEY>")
+glacier:requiredcli.WithFlagRequired("<Field>")
+glacier:choices <a>|<b>cli.WithFlagChoices("<Field>", []string{...})
+glacier:deprecated <msg>cli.WithFlagDeprecated("<Field>", "<msg>")
+glacier:validate <funcName>cli.WithFlagValidate("<Field>", <funcName>)
+glacier:positionalcli.WithPositional("<Field>")

simple nested glacier explain

Apache-2.0