-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
_ "embed" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// versionString indicates the version of this library. | ||
//go:embed version.txt | ||
versionString string | ||
// metadataString indicates additional build or distribution metadata. | ||
metadataString string | ||
) | ||
|
||
func init() { | ||
versionString = semanticVersion() | ||
} | ||
|
||
// semanticVersion returns the version of the CLI including a compile-time metadata. | ||
func semanticVersion() string { | ||
v := strings.TrimSpace(versionString) | ||
if metadataString != "" { | ||
v += "+" + metadataString | ||
} | ||
return v | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := NewCommand().Execute(); err != nil { | ||
exit := 1 | ||
os.Exit(exit) | ||
} | ||
} | ||
|
||
// Command represents an invocation of the CLI. | ||
type Command struct { | ||
*cobra.Command | ||
} | ||
|
||
// NewCommand returns a Command object representing an invocation of the CLI. | ||
func NewCommand() *Command { | ||
rootCmd := &cobra.Command{ | ||
Use: "toolbox", | ||
Version: versionString, | ||
RunE: root, | ||
} | ||
|
||
c := &Command{ | ||
Command: rootCmd, | ||
} | ||
|
||
return c | ||
} | ||
|
||
func root(cmd *cobra.Command, args []string) error { | ||
cmd.Printf("Toolbox running v%s!", versionString) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd_test | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/googleapis/genai-toolbox/cmd" | ||
) | ||
|
||
func TestVersion(t *testing.T) { | ||
data, err := os.ReadFile("version.txt") | ||
if err != nil { | ||
t.Fatalf("failed to read version.txt: %v", err) | ||
} | ||
want := strings.TrimSpace(string(data)) | ||
|
||
// run command with flag | ||
b := bytes.NewBufferString("") | ||
cmd := cmd.NewCommand() | ||
cmd.SetArgs([]string{"--version"}) | ||
cmd.SetOut(b) | ||
|
||
err = cmd.Execute() | ||
if err != nil { | ||
t.Fatalf("unable to execute command: %q", err) | ||
} | ||
|
||
out, err := io.ReadAll(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
got := string(out) | ||
|
||
if !strings.Contains(got, want) { | ||
t.Errorf("cli did not return correct version: want %q, got %q", want, got) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
module github.com/googleapis/genai-toolbox | ||
|
||
go 1.22.2 | ||
|
||
require github.com/spf13/cobra v1.8.1 | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= | ||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters