Skip to content

Commit a91470e

Browse files
Yuan325jeffreyrubi
authored andcommitted
feat: add options for command (googleapis#110)
Add Option to configure a Command. Currently we are only supporting `WithLogger` Option.
1 parent 0de3dfe commit a91470e

3 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎cmd/options.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"github.com/googleapis/genai-toolbox/internal/log"
19+
)
20+
21+
// Option is a function that configures a Command.
22+
type Option func(*Command)
23+
24+
// WithLogger overrides the default logger.
25+
func WithLogger(l log.Logger) Option {
26+
return func(c *Command) {
27+
c.logger = l
28+
}
29+
}

‎cmd/options_test.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"errors"
19+
"io"
20+
"testing"
21+
22+
"github.com/googleapis/genai-toolbox/internal/log"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
func TestCommandOptions(t *testing.T) {
27+
logger, err := log.NewStdLogger(io.Discard, io.Discard, "INFO")
28+
if err != nil {
29+
t.Errorf("fail to initialize logger: %v", err)
30+
}
31+
tcs := []struct {
32+
desc string
33+
isValid func(*Command) error
34+
option Option
35+
}{
36+
{
37+
desc: "with logger",
38+
isValid: func(c *Command) error {
39+
if c.logger != logger {
40+
return errors.New("loggers do not match")
41+
}
42+
return nil
43+
},
44+
option: WithLogger(logger),
45+
},
46+
}
47+
for _, tc := range tcs {
48+
t.Run(tc.desc, func(t *testing.T) {
49+
got, err := invokeProxyWithOption(tc.option)
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
if err := tc.isValid(got); err != nil {
54+
t.Errorf("option did not initialize command correctly: %v", err)
55+
}
56+
})
57+
}
58+
}
59+
60+
func invokeProxyWithOption(o Option) (*Command, error) {
61+
c := NewCommand(o)
62+
// Keep the test output quiet
63+
c.SilenceUsage = true
64+
c.SilenceErrors = true
65+
// Disable execute behavior
66+
c.RunE = func(*cobra.Command, []string) error {
67+
return nil
68+
}
69+
70+
err := c.Execute()
71+
return c, err
72+
}

‎cmd/root.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type Command struct {
6767
}
6868

6969
// NewCommand returns a Command object representing an invocation of the CLI.
70-
func NewCommand() *Command {
70+
func NewCommand(opts ...Option) *Command {
7171
cmd := &Command{
7272
Command: &cobra.Command{
7373
Use: "toolbox",
@@ -76,6 +76,10 @@ func NewCommand() *Command {
7676
},
7777
}
7878

79+
for _, o := range opts {
80+
o(cmd)
81+
}
82+
7983
flags := cmd.Flags()
8084
flags.StringVarP(&cmd.cfg.Address, "address", "a", "127.0.0.1", "Address of the interface the server will listen on.")
8185
flags.IntVarP(&cmd.cfg.Port, "port", "p", 5000, "Port the server will listen on.")

0 commit comments

Comments
 (0)