diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 00000000..f418946b --- /dev/null +++ b/cmd/root.go @@ -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 +} diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 00000000..0a17c9d9 --- /dev/null +++ b/cmd/root_test.go @@ -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) + } + +} diff --git a/cmd/version.txt b/cmd/version.txt new file mode 100644 index 00000000..bd52db81 --- /dev/null +++ b/cmd/version.txt @@ -0,0 +1 @@ +0.0.0 \ No newline at end of file diff --git a/go.mod b/go.mod index 7cc7267b..e012b67c 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index e69de29b..912390a7 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index ed1b5c43..2688aaf5 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,8 @@ package main -import "fmt" +import "github.com/googleapis/genai-toolbox/cmd" func main() { - fmt.Println("hello world") + cmd.Execute() }