Skip to content

Commit

Permalink
Merge pull request #1467 from onflow/ianthpun/version-update
Browse files Browse the repository at this point in the history
add flow package depdendenices into `version`
  • Loading branch information
ianthpun authored Mar 21, 2024
2 parents 86c91f4 + 0aa4fe3 commit 690ccd0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
8 changes: 4 additions & 4 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type Command struct {
}

const (
formatText = "text"
formatInline = "inline"
formatJSON = "json"
FormatText = "text"
FormatInline = "inline"
FormatJSON = "json"
)

const (
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/command/global_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// Flags initialized to default values.
var Flags = GlobalFlags{
Filter: "",
Format: formatText,
Format: FormatText,
Save: "",
Host: "",
HostNetworkKey: "",
Expand Down
6 changes: 3 additions & 3 deletions internal/command/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
97 changes: 89 additions & 8 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,111 @@
package version

import (
"encoding/json"
"fmt"
"log"
"runtime/debug"
"strings"

"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 {
switch format {
case command.FormatInline, command.FormatText:
var txtBuilder strings.Builder
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))
}

log.Println(txtBuilder.String())

return nil

case command.FormatJSON:
jsonRes, err := c.MarshalJSON()
if err != nil {
return err
}

log.Println(string(jsonRes))

return nil

default:
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",
Run: func(cmd *cobra.Command, args []string) {
semver := build.Semver()
commit := build.Commit()

// Print version/commit strings if they are known
if build.IsDefined(semver) {
fmt.Printf("Version: %s\n", semver)
v := &versionCmd{
Version: semver,
Commit: commit,
}
if build.IsDefined(commit) {
fmt.Printf("Commit: %s\n", commit)

bi, ok := debug.ReadBuildInfo()
if !ok {
log.Printf("Failed to read build info")
return
}
// 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")

// only add dependencies from github.com/onflow
for _, dep := range bi.Deps {
if strings.Contains(dep.Path, "github.com/onflow/") {
v.Dependencies = append(v.Dependencies, *dep)
}
}

if err := v.Print(command.Flags.Format); err != nil {
log.Printf("Failed to print version information: %s", err)
return
}
},
}

0 comments on commit 690ccd0

Please sign in to comment.