Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func YAML ¶
func YAML(key string, sources ...altsrc.Sourcer) cli.ValueSourceChain
YAML is a helper function to encapsulate a number of yamlValueSource together as a cli.ValueSourceChain
Example ¶
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
altsrc "github.com/urfave/cli-altsrc/v3"
yaml "github.com/urfave/cli-altsrc/yaml"
"github.com/urfave/cli/v3"
)
var (
testdataDir = func() string {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return altsrc.MustTestdataDir(ctx)
}()
)
func main() {
configFiles := []altsrc.Sourcer{
altsrc.StringSourcer(filepath.Join(testdataDir, "config.yaml")),
altsrc.StringSourcer(filepath.Join(testdataDir, "alt-config.yaml")),
}
app := &cli.Command{
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Sources: yaml.YAML("greet.name", configFiles...),
},
&cli.IntFlag{
Name: "enthusiasm",
Aliases: []string{"!"},
Sources: yaml.YAML("greet.enthusiasm", configFiles...),
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
punct := ""
if cmd.Int("enthusiasm") > 9000 {
punct = "!"
}
fmt.Fprintf(os.Stdout, "Hello, %[1]v%[2]v\n", cmd.String("name"), punct)
return nil
},
}
// Simulating os.Args
os.Args = []string{"greet"}
if err := app.Run(context.Background(), os.Args); err != nil {
fmt.Fprintf(os.Stdout, "OH NO: %[1]v\n", err)
}
}
Output: Hello, Berry!
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.