From 362f8c36196286eed525f2add549a09eb959b687 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Tue, 19 Mar 2024 11:06:04 -0700 Subject: [PATCH 1/6] update with flow package depdendenices --- internal/version/version.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/version/version.go b/internal/version/version.go index aab468f92..53fab4fa6 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -20,6 +20,9 @@ package version import ( "fmt" + "log" + "runtime/debug" + "strings" "github.com/spf13/cobra" @@ -35,7 +38,7 @@ var Cmd = &cobra.Command{ // Print version/commit strings if they are known if build.IsDefined(semver) { - fmt.Printf("Version: %s\n", semver) + fmt.Printf("Flow CLI Version: %s\n", semver) } if build.IsDefined(commit) { fmt.Printf("Commit: %s\n", commit) @@ -44,5 +47,19 @@ var Cmd = &cobra.Command{ if !build.IsDefined(semver) && !build.IsDefined(commit) { fmt.Printf("Version information unknown!\n") } + + // Print the Flow package dependencies from github.com/onflow + bi, ok := debug.ReadBuildInfo() + if !ok { + log.Printf("Failed to read build info") + return + } + + fmt.Printf("\nFlow Package Dependencies: \n") + for _, dep := range bi.Deps { + if strings.Contains(dep.Path, "github.com/onflow/") { + fmt.Printf("%s: %s\n", dep.Path, dep.Version) + } + } }, } From 77202785ab2fc7dbf7e9bbb9cdf864846e009c1e Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Tue, 19 Mar 2024 15:41:45 -0700 Subject: [PATCH 2/6] udpate --- internal/version/version.go | 87 +++++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/internal/version/version.go b/internal/version/version.go index 53fab4fa6..c1ee587ca 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -19,6 +19,7 @@ package version import ( + "encoding/json" "fmt" "log" "runtime/debug" @@ -27,8 +28,71 @@ import ( "github.com/spf13/cobra" "github.com/onflow/flow-cli/build" + "github.com/onflow/flow-cli/internal/command" ) +type versionCmd struct { + Version string + Commit string + Dependencies []debug.Module +} + +// Print prints the version information in the given format. +func (c versionCmd) Print(format string) error { + if format == "text" { + var txtBuilder strings.Builder + txtBuilder.WriteString(fmt.Sprintf("Flow CLI Version: %s %s\n", c.Version, c.Commit)) + txtBuilder.WriteString(fmt.Sprintf("\nFlow Package Dependencies \n")) + for _, dep := range c.Dependencies { + txtBuilder.WriteString(fmt.Sprintf("%s %s %s\n", dep.Path, dep.Version, dep.Sum)) + } + + log.Println(txtBuilder.String()) + + return nil + } + + if format == "json" { + jsonRes, err := c.MarshalJSON() + if err != nil { + return err + } + + log.Println(string(jsonRes)) + + return nil + } + + return fmt.Errorf("unsupported format: %s", format) +} + +// MarshalJSON returns the JSON encoding of the cmdPrint. +func (c *versionCmd) MarshalJSON() ([]byte, error) { + js := struct { + Version string `json:"version"` + Commit string `json:"commit"` + Dependencies []struct { + Package string `json:"package"` + Version string `json:"version"` + } `json:"dependencies"` + }{ + Version: c.Version, + Commit: c.Commit, + } + + for _, dep := range c.Dependencies { + js.Dependencies = append(js.Dependencies, struct { + Package string `json:"package"` + Version string `json:"version"` + }{ + Package: dep.Path, + Version: dep.Version, + }) + } + + return json.Marshal(js) +} + var Cmd = &cobra.Command{ Use: "version", Short: "View version and commit information", @@ -36,30 +100,27 @@ var Cmd = &cobra.Command{ semver := build.Semver() commit := build.Commit() - // Print version/commit strings if they are known - if build.IsDefined(semver) { - fmt.Printf("Flow CLI Version: %s\n", semver) - } - if build.IsDefined(commit) { - fmt.Printf("Commit: %s\n", commit) - } - // If no version info is known print a message to indicate this. - if !build.IsDefined(semver) && !build.IsDefined(commit) { - fmt.Printf("Version information unknown!\n") + ver := &versionCmd{ + Version: semver, + Commit: commit, } - // Print the Flow package dependencies from github.com/onflow bi, ok := debug.ReadBuildInfo() if !ok { log.Printf("Failed to read build info") return } - fmt.Printf("\nFlow Package Dependencies: \n") + // only add dependencies from github.com/onflow for _, dep := range bi.Deps { if strings.Contains(dep.Path, "github.com/onflow/") { - fmt.Printf("%s: %s\n", dep.Path, dep.Version) + ver.Dependencies = append(ver.Dependencies, *dep) } } + + if err := ver.Print(command.Flags.Format); err != nil { + log.Printf("Failed to print version information: %s", err) + return + } }, } From e1ab296dc1a428a23f95a2698c053890da0dbbcf Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 20 Mar 2024 09:04:37 -0400 Subject: [PATCH 3/6] remove sum --- internal/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/version/version.go b/internal/version/version.go index c1ee587ca..49329a441 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -44,7 +44,7 @@ func (c versionCmd) Print(format string) error { txtBuilder.WriteString(fmt.Sprintf("Flow CLI Version: %s %s\n", c.Version, c.Commit)) txtBuilder.WriteString(fmt.Sprintf("\nFlow Package Dependencies \n")) for _, dep := range c.Dependencies { - txtBuilder.WriteString(fmt.Sprintf("%s %s %s\n", dep.Path, dep.Version, dep.Sum)) + txtBuilder.WriteString(fmt.Sprintf("%s %s\n", dep.Path, dep.Version)) } log.Println(txtBuilder.String()) From 1b7e2421a5e0cece94c5a11583f173fc70df6bff Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 20 Mar 2024 09:08:31 -0400 Subject: [PATCH 4/6] update --- internal/command/command.go | 8 ++++---- internal/command/result.go | 6 +++--- internal/version/version.go | 18 ++++++++++-------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index 822639dc9..cebdd608f 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -74,9 +74,9 @@ type Command struct { } const ( - formatText = "text" - formatInline = "inline" - formatJSON = "json" + FormatText = "text" + FormatInline = "inline" + FormatJSON = "json" ) const ( @@ -238,7 +238,7 @@ func resolveHost(state *flowkit.State, hostFlag, networkKeyFlag, networkFlag str func createLogger(logFlag string, formatFlag string) output.Logger { // disable logging if we user want a specific format like JSON // (more common they will not want also to have logs) - if formatFlag != formatText { + if formatFlag != FormatText { logFlag = logLevelNone } diff --git a/internal/command/result.go b/internal/command/result.go index b6576f39a..d895c7247 100644 --- a/internal/command/result.go +++ b/internal/command/result.go @@ -76,10 +76,10 @@ func formatResult(result Result, filterFlag string, formatFlag string) (string, } switch strings.ToLower(formatFlag) { - case formatJSON: + case FormatJSON: jsonRes, _ := json.Marshal(result.JSON()) return string(jsonRes), nil - case formatInline: + case FormatInline: return result.Oneliner(), nil default: return result.String(), nil @@ -104,7 +104,7 @@ func outputResult(result string, saveFlag string, formatFlag string, filterFlag return af.WriteFile(saveFlag, []byte(result), 0644) } - if formatFlag == formatInline || filterFlag != "" { + if formatFlag == FormatInline || filterFlag != "" { _, _ = fmt.Fprintf(os.Stdout, "%s", result) } else { // default normal output _, _ = fmt.Fprintf(os.Stdout, "\n%s\n\n", result) diff --git a/internal/version/version.go b/internal/version/version.go index 49329a441..04e9aeed5 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -39,10 +39,12 @@ type versionCmd struct { // Print prints the version information in the given format. func (c versionCmd) Print(format string) error { - if format == "text" { + switch format { + case command.FormatInline, command.FormatText: var txtBuilder strings.Builder txtBuilder.WriteString(fmt.Sprintf("Flow CLI Version: %s %s\n", c.Version, c.Commit)) txtBuilder.WriteString(fmt.Sprintf("\nFlow Package Dependencies \n")) + for _, dep := range c.Dependencies { txtBuilder.WriteString(fmt.Sprintf("%s %s\n", dep.Path, dep.Version)) } @@ -50,9 +52,8 @@ func (c versionCmd) Print(format string) error { log.Println(txtBuilder.String()) return nil - } - if format == "json" { + case command.FormatJSON: jsonRes, err := c.MarshalJSON() if err != nil { return err @@ -61,9 +62,10 @@ func (c versionCmd) Print(format string) error { log.Println(string(jsonRes)) return nil - } - return fmt.Errorf("unsupported format: %s", format) + default: + return fmt.Errorf("unsupported format: %s", format) + } } // MarshalJSON returns the JSON encoding of the cmdPrint. @@ -100,7 +102,7 @@ var Cmd = &cobra.Command{ semver := build.Semver() commit := build.Commit() - ver := &versionCmd{ + v := &versionCmd{ Version: semver, Commit: commit, } @@ -114,11 +116,11 @@ var Cmd = &cobra.Command{ // only add dependencies from github.com/onflow for _, dep := range bi.Deps { if strings.Contains(dep.Path, "github.com/onflow/") { - ver.Dependencies = append(ver.Dependencies, *dep) + v.Dependencies = append(v.Dependencies, *dep) } } - if err := ver.Print(command.Flags.Format); err != nil { + if err := v.Print(command.Flags.Format); err != nil { log.Printf("Failed to print version information: %s", err) return } From b6b96b66f2f664889fe59af6dd1d772525287c42 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 20 Mar 2024 09:15:21 -0400 Subject: [PATCH 5/6] update --- internal/command/global_flags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/command/global_flags.go b/internal/command/global_flags.go index 0ce586709..a5a509ac9 100644 --- a/internal/command/global_flags.go +++ b/internal/command/global_flags.go @@ -33,7 +33,7 @@ import ( // Flags initialized to default values. var Flags = GlobalFlags{ Filter: "", - Format: formatText, + Format: FormatText, Save: "", Host: "", HostNetworkKey: "", From 4ffa22b300aaf72cf1b996450ac26a0237bc0ac8 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Thu, 21 Mar 2024 11:20:14 -0400 Subject: [PATCH 6/6] update --- internal/version/version.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/version/version.go b/internal/version/version.go index 04e9aeed5..a60fec245 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -42,9 +42,10 @@ func (c versionCmd) Print(format string) error { switch format { case command.FormatInline, command.FormatText: var txtBuilder strings.Builder - txtBuilder.WriteString(fmt.Sprintf("Flow CLI Version: %s %s\n", c.Version, c.Commit)) - txtBuilder.WriteString(fmt.Sprintf("\nFlow Package Dependencies \n")) + txtBuilder.WriteString(fmt.Sprintf("Version: %s\n", c.Version)) + txtBuilder.WriteString(fmt.Sprintf("Commit: %s\n", c.Commit)) + txtBuilder.WriteString(fmt.Sprintf("\nFlow Package Dependencies \n")) for _, dep := range c.Dependencies { txtBuilder.WriteString(fmt.Sprintf("%s %s\n", dep.Path, dep.Version)) }