-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
745b3cd
commit 952932e
Showing
6 changed files
with
179 additions
and
65 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
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
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,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) | ||
} | ||
} | ||
} | ||
} |
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
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,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) | ||
} |
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,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" |