Skip to content

Commit

Permalink
version Command (#135)
Browse files Browse the repository at this point in the history
* version Command

Signed-off-by: hfuss <[email protected]>

* missing single quote

Signed-off-by: hfuss <[email protected]>

* gofmt on cmd; json / yaml / short support w/ date and commit

Signed-off-by: hfuss <[email protected]>

* final tweaks

Signed-off-by: hfuss <[email protected]>

* including license in version info

Signed-off-by: hfuss <[email protected]>
  • Loading branch information
onelapahead authored Jan 31, 2022
1 parent 745b3cd commit 952932e
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2021 Kaleido, Inc.
# Copyright © 2022 Kaleido, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -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

Expand Down
124 changes: 62 additions & 62 deletions cmd/prompt.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
85 changes: 85 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 952932e

Please sign in to comment.