Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
143 lines (121 loc) · 3.25 KB
/
Copy pathexample_test.go
File metadata and controls
143 lines (121 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cli_test
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/dioad/cli"
)
// ExampleNewCommand demonstrates creating a type-safe command with configuration.
func ExampleNewCommand() {
type Config struct {
Name string `mapstructure:"name"`
Port int `mapstructure:"port"`
}
cfg := &Config{
Name: "example-service",
Port: 8080,
}
cmd := cli.NewCommand(
&cobra.Command{
Use: "serve",
Short: "Start the service",
},
func(ctx context.Context, c *Config) error {
fmt.Printf("Service %s listening on port %d\n", c.Name, c.Port)
return nil
},
cfg,
cli.WithConfigFlag("config.yaml"),
)
if cmd != nil {
fmt.Println("Command created successfully")
}
// Output: Command created successfully
}
// ExampleWithConfigFlag demonstrates adding a config file flag to a command.
func ExampleWithConfigFlag() {
cmd := &cobra.Command{Use: "serve"}
opt := cli.WithConfigFlag("~/.app/config.yaml")
opt(cmd)
configFlag := cmd.Flag("config")
if configFlag != nil {
fmt.Printf("Config flag default: %s\n", configFlag.DefValue)
}
// Output: Config flag default: ~/.app/config.yaml
}
// ExampleDefaultConfigPath demonstrates getting the default config path.
func ExampleDefaultConfigPath() {
path, err := cli.DefaultConfigPath("myorg", "myapp")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if path != "" {
fmt.Println("Config path obtained successfully")
}
// Output: Config path obtained successfully
}
// ExampleDefaultPersistencePath demonstrates getting the persistence path.
func ExampleDefaultPersistencePath() {
path, err := cli.DefaultPersistencePath("myorg", "myapp")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if path != "" {
fmt.Println("Persistence path obtained successfully")
}
// Output: Persistence path obtained successfully
}
// ExampleDefaultConfigFile demonstrates getting a default config file path.
func ExampleDefaultConfigFile() {
file, err := cli.DefaultConfigFile("myorg", "myapp", "config")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if file != "" {
fmt.Println("Config file path obtained successfully")
}
// Output: Config file path obtained successfully
}
// ExampleContext demonstrates creating a context with application metadata.
func ExampleContext() {
ctx := cli.Context(
context.Background(),
cli.SetOrgName("myorg"),
cli.SetAppName("myapp"),
)
if ctx != nil {
fmt.Println("Context created with metadata")
}
// Output: Context created with metadata
}
// ExampleSetOrgName demonstrates setting organization name in context.
func ExampleSetOrgName() {
opt := cli.SetOrgName("acme-corp")
ctx := opt(context.Background())
if ctx != nil {
fmt.Println("Organization name set in context")
}
// Output: Organization name set in context
}
// ExampleSetAppName demonstrates setting app name in context.
func ExampleSetAppName() {
opt := cli.SetAppName("myservice")
ctx := opt(context.Background())
if ctx != nil {
fmt.Println("App name set in context")
}
// Output: App name set in context
}
// ExampleIsDocker demonstrates Docker detection.
func ExampleIsDocker() {
inDocker := cli.IsDocker()
if inDocker {
fmt.Println("Running in Docker")
} else {
fmt.Println("Running on host machine")
}
// Output: Running on host machine
}