diff --git a/.goreleaser.yml b/.goreleaser.yml index 1f04f950..79f111d7 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -25,6 +25,8 @@ builds: - darwin main: ./ff binary: ff + ldflags: + - "-s -w -X 'github.com/hyperledger/firefly-cli/internal/version.Version={{.Version}}' -X 'github.com/hyperledger/firefly-cli/internal/version.Commit={{.Commit}}' -X 'github.com/hyperledger/firefly-cli/internal/version.Date={{.Date}}'" archives: - replacements: darwin: Darwin diff --git a/Makefile b/Makefile index f950899e..d2fb2240 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# Copyright © 2021 Kaleido, Inc. +# Copyright © 2022 Kaleido, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -16,11 +16,13 @@ VGO=go GOBIN := $(shell $(VGO) env GOPATH)/bin +GITREF := $(shell git rev-parse --short HEAD) +DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") LINT := $(GOBIN)/golangci-lint all: build build: ## Builds all go code - cd ff && go build + cd ff && go build -ldflags="-X 'github.com/hyperledger/firefly-cli/internal/version.Date=$(DATE)' -X 'github.com/hyperledger/firefly-cli/internal/version.Commit=$(GITREF)'" install: ## Installs the package cd ff && go install diff --git a/cmd/prompt.go b/cmd/prompt.go index de147a3e..da04305d 100644 --- a/cmd/prompt.go +++ b/cmd/prompt.go @@ -1,62 +1,62 @@ -// Copyright © 2021 Kaleido, Inc. -// -// SPDX-License-Identifier: Apache-2.0 -// -// 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 ( - "bufio" - "fmt" - "os" - "strings" -) - -func prompt(promptText string, validate func(string) error) (string, error) { - reader := bufio.NewReader(os.Stdin) - for { - fmt.Print(promptText) - if str, err := reader.ReadString('\n'); err != nil { - return "", err - } else { - str = strings.TrimSpace(str) - if err := validate(str); err != nil { - if fancyFeatures { - fmt.Printf("\u001b[31mError: %s\u001b[0m\n", err.Error()) - } else { - fmt.Printf("Error: %s\n", err.Error()) - } - } else { - return str, nil - } - } - } -} - -func confirm(promptText string) error { - reader := bufio.NewReader(os.Stdin) - for { - fmt.Printf("%s [y/N] ", promptText) - if str, err := reader.ReadString('\n'); err != nil { - return err - } else { - str = strings.ToLower(strings.TrimSpace(str)) - if str == "y" || str == "yes" { - return nil - } else { - return fmt.Errorf("confirmation declined with response: '%s'", str) - } - } - } -} +// Copyright © 2022 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// 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 ( + "bufio" + "fmt" + "os" + "strings" +) + +func prompt(promptText string, validate func(string) error) (string, error) { + reader := bufio.NewReader(os.Stdin) + for { + fmt.Print(promptText) + if str, err := reader.ReadString('\n'); err != nil { + return "", err + } else { + str = strings.TrimSpace(str) + if err := validate(str); err != nil { + if fancyFeatures { + fmt.Printf("\u001b[31mError: %s\u001b[0m\n", err.Error()) + } else { + fmt.Printf("Error: %s\n", err.Error()) + } + } else { + return str, nil + } + } + } +} + +func confirm(promptText string) error { + reader := bufio.NewReader(os.Stdin) + for { + fmt.Printf("%s [y/N] ", promptText) + if str, err := reader.ReadString('\n'); err != nil { + return err + } else { + str = strings.ToLower(strings.TrimSpace(str)) + if str == "y" || str == "yes" { + return nil + } else { + return fmt.Errorf("confirmation declined with response: '%s'", str) + } + } + } +} diff --git a/cmd/root.go b/cmd/root.go index 3f7c90e4..84a7bd66 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -80,7 +80,7 @@ To get started run: ff init // 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() { - rootCmd.PersistentFlags().StringVarP(&ansi, "ansi", "", "auto", "control when to print ANSI control characters (\"never\"|\"always\"|\"auto\") (default \"auto\")") + rootCmd.PersistentFlags().StringVarP(&ansi, "ansi", "", "auto", "control when to print ANSI control characters (\"never\"|\"always\"|\"auto\")") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose log output") cobra.CheckErr(rootCmd.Execute()) } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..2eefb1bb --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,85 @@ +// Copyright © 2022 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// 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 ( + "encoding/json" + "errors" + "fmt" + "github.com/hyperledger/firefly-cli/internal/version" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +var shortened = false +var output = "json" + +// Info creates a formattable struct for version output +type Info struct { + Version string `json:"Version,omitempty" yaml:"Version,omitempty"` + Commit string `json:"Commit,omitempty" yaml:"Commit,omitempty"` + Date string `json:"Date,omitempty" yaml:"Date,omitempty"` + License string `json:"License,omitempty" yaml:"License,omitempty"` +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Prints the version info", + Long: "Prints the version info of the CLI binary", + RunE: func(cmd *cobra.Command, args []string) error { + if shortened { + fmt.Println(version.Version) + } else { + info := &Info{ + Version: version.Version, + Commit: version.Commit, + Date: version.Date, + License: version.License, + } + + var ( + bytes []byte + err error + ) + + switch output { + case "json": + bytes, err = json.MarshalIndent(info, "", " ") + break + case "yaml": + bytes, err = yaml.Marshal(info) + break + default: + return errors.New(fmt.Sprintf("invalid output '%s'", output)) + } + + if err != nil { + return err + } + + fmt.Println(string(bytes)) + } + + return nil + }, +} + +func init() { + versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "print only the version") + versionCmd.Flags().StringVarP(&output, "output", "o", "json", "output format (\"yaml\"|\"json\")") + rootCmd.AddCommand(versionCmd) +} diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..515667e4 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,25 @@ +// Copyright © 2022 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// 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 version + +var ( + Version = "canary" + Commit = "ref" + Date = "1970-01-01T00:00:00Z" +) + +const License = "Apache-2.0"