Skip to content

Commit

Permalink
fix: version style, rootCmd variable and others
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsm-dev committed Jun 16, 2022
1 parent 532507e commit 76152c7
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 65 deletions.
20 changes: 10 additions & 10 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
project_name: loli
env:
- GO111MODULE=on
before:
hooks:
# Cleaning local assets
- make clean
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
release:
github:
owner: lpmatos
Expand Down Expand Up @@ -49,17 +57,9 @@ archives:
- LICENSE
- README.md
snapshot:
name_template: "SNAPSHOT-{{ .Commit }}"
name_template: 'SNAPSHOT-{{ .Commit }}'
checksum:
name_template: "{{ .ProjectName }}_v{{ .Version }}_SHA256SUMS"
name_template: '{{ .ProjectName }}_v{{ .Version }}_SHA256SUMS'
algorithm: sha256
changelog:
skip: true
dist: dist
before:
hooks:
- make clean
- go generate ./...
- go mod download
github_urls:
download: https://github.com
4 changes: 1 addition & 3 deletions cmd/loli/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
cmd "github.com/lpmatos/loli/commands"
)
import cmd "github.com/lpmatos/loli/commands"

func main() {
cmd.Execute()
Expand Down
6 changes: 3 additions & 3 deletions commands/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io"

"github.com/common-nighthawk/go-figure"
//"github.com/common-nighthawk/go-figure"
"github.com/lpmatos/loli/internal/constants"
log "github.com/lpmatos/loli/internal/log"
"github.com/lpmatos/loli/internal/utils"
Expand All @@ -20,15 +20,15 @@ var (
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by loli main(). It only needs to happen once to the rootCmd.
func Execute() {
figure.NewColorFigure(constants.BinaryName, "smslant", "yellow", false).Print()
//figure.NewColorFigure(constants.BinaryName, "smslant", "yellow", false).Print()

outputRender, err := utils.RenderMarkdown(constants.Welcome)
if err != nil {
log.Fatal("Render glamour markdown")
}
fmt.Print(outputRender)

if err := RootCmd.Execute(); err != nil {
if err := rootCmd.Execute(); err != nil {
log.Fatal("Error while executing RootCmd")
}
}
22 changes: 16 additions & 6 deletions commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/spf13/cobra"
)

// excludeDesc description will not be added if true.
var excludeDesc = false

// completionCmd represents the completion command.
var completionCmd = &cobra.Command{
Use: "completion <shell>",
Expand All @@ -22,22 +25,29 @@ var completionCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
var shellType string = args[0]
rootCmd := cmd.Parent()
out, rootCmd := os.Stdout, cmd.Parent()
switch shellType {
case "bash":
return rootCmd.GenBashCompletion(os.Stdout)
return rootCmd.GenBashCompletionV2(out, !excludeDesc)
case "zsh":
return rootCmd.GenZshCompletion(os.Stdout)
if excludeDesc {
return rootCmd.GenZshCompletionNoDesc(out)
}
return rootCmd.GenZshCompletion(out)
case "powershell":
return rootCmd.GenPowerShellCompletion(os.Stdout)
if excludeDesc {
return rootCmd.GenPowerShellCompletion(out)
}
return rootCmd.GenPowerShellCompletionWithDesc(out)
case "fish":
return rootCmd.GenFishCompletion(os.Stdout, true)
return rootCmd.GenFishCompletion(out, !excludeDesc)
default:
return fmt.Errorf("unsupported shell type %q", shellType)
}
},
}

func init() {
RootCmd.AddCommand(completionCmd)
completionCmd.Flags().BoolVarP(&excludeDesc, "no-desc", "", false, "Do not include shell completion description")
rootCmd.AddCommand(completionCmd)
}
15 changes: 7 additions & 8 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

var config = log.Config{}

// RootCmd represents the base command when called without any subcommands.
var RootCmd = &cobra.Command{
var rootCmd = &cobra.Command{
Use: constants.BinaryName,
Short: "Find the anime scene by image using your terminal",
Long: `Description:
Expand Down Expand Up @@ -40,10 +39,10 @@ var RootCmd = &cobra.Command{
}

func init() {
RootCmd.PersistentFlags().StringVar(&config.Level, "log-level", "debug", "set the logging level. One of: debug|info|warn|error")
RootCmd.PersistentFlags().StringVar(&config.Format, "log-format", "color", "the formating of the logs. Available values: text|color|json|json-pretty")
RootCmd.PersistentFlags().StringVar(&config.Output, "log-output", "stdout", "default log output. Available values: stdout|stderr|file")
RootCmd.PersistentFlags().StringVar(&config.File, "log-file", helpers.CreateLogFile("/var/log/loli", "file"), "defaulting Loli CLI log file")
RootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "verbose output")
RootCmd.PersistentFlags().IntP("timeout", "t", 0, "override the default HTTP timeout (seconds)")
rootCmd.PersistentFlags().StringVar(&config.Level, "log-level", "debug", "set the logging level. One of: debug|info|warn|error")
rootCmd.PersistentFlags().StringVar(&config.Format, "log-format", "color", "the formating of the logs. Available values: text|color|json|json-pretty")
rootCmd.PersistentFlags().StringVar(&config.Output, "log-output", "stdout", "default log output. Available values: stdout|stderr|file")
rootCmd.PersistentFlags().StringVar(&config.File, "log-file", helpers.CreateLogFile("/var/log/loli", "file"), "defaulting Loli CLI log file")
rootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().IntP("timeout", "t", 0, "override the default HTTP timeout (seconds)")
}
2 changes: 1 addition & 1 deletion commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ var SearchCmd = &cobra.Command{

func init() {
SearchCmd.PersistentFlags().BoolVarP(&searchPretty, "pretty", "p", false, "Pretty output")
RootCmd.AddCommand(SearchCmd)
rootCmd.AddCommand(SearchCmd)
}
2 changes: 1 addition & 1 deletion commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ func updateCLI(c cli.Updater) error {
}

func init() {
RootCmd.AddCommand(UpgradeCmd)
rootCmd.AddCommand(UpgradeCmd)
}
16 changes: 6 additions & 10 deletions commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ import (
"github.com/spf13/cobra"
)

var (
short bool // Local flag - if true print just the version number of Gen CLI.
pretty bool // Local flag - if true show more details about the current version of Gen CLI.
)
var short, full bool

// VersionCmd sepresents the version command.
var VersionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "Version outputs the version of CLI",
Long: `Version outputs the version of the loli binary that is in use.`,
Long: `Version outputs the version of the loli binary that is in use`,
Run: func(cmd *cobra.Command, args []string) {
if short {
version.GetShortDetails()
} else {
version.ShowVersion(pretty)
version.ShowVersion(full)
}
},
}

func init() {
VersionCmd.PersistentFlags().BoolVarP(&short, "short", "s", false, "Print just the version number of loli CLI")
VersionCmd.PersistentFlags().BoolVarP(&pretty, "pretty", "p", false, "Show more details about the current version of loli CLI")
RootCmd.AddCommand(VersionCmd)
VersionCmd.PersistentFlags().BoolVarP(&short, "short", "s", false, "Show short details about the current version of loli CLI")
VersionCmd.PersistentFlags().BoolVarP(&full, "full", "f", false, "Show full details about the current version of loli CLI")
rootCmd.AddCommand(VersionCmd)
}
65 changes: 42 additions & 23 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package version

import (
"fmt"
"os"

"github.com/jedib0t/go-pretty/table"
Expand Down Expand Up @@ -29,45 +28,65 @@ func GetVersionFormatted() string {

// GetShortDetails function - create a pretty table and parse this table with current version details.
func GetShortDetails() {
pterm.DefaultSection.Println("Version short details")
pterm.DefaultTable.WithHasHeader().WithData(pterm.TableData{
{"Info", "Content"},
{"CLI Name", cliName},
{"CLI Version", cliVersion},
{"Project URL", projectURL},
{"Build Date", builtDate},
{"Commit Short", commitShort},
}).Render()
fmt.Println()
pterm.Println()
pterm.DefaultHeader.
WithMargin(5).
WithBackgroundStyle(pterm.NewStyle(pterm.BgBlack)).
Printf("✨ %s version details ✨", constants.BinaryName)
pterm.Println()
versionTable := table.NewWriter()
versionTable.SetOutputMirror(os.Stdout)
versionTable.AppendHeader(table.Row{"Info", "Content"})
versionTable.AppendRows([]table.Row{
{"➤ CLI Name", cliName},
{"➤ CLI Version", cliVersion},
{"➤ Project URL", projectURL},
{"➤ Build Date", builtDate},
{"➤ Commit Short", commitShort},
{"➤ Go Version", goVersion},
})
versionTable.SetStyle(table.StyleColoredBright)
versionTable.Render()
pterm.Println()
}

// GetPrettyDetails function - create a pretty table and parse this table with current version details.
func GetPrettyDetails() {
pterm.DefaultSection.Println("Version pretty details")
pterm.Println()
pterm.DefaultHeader.
WithMargin(5).
WithBackgroundStyle(pterm.NewStyle(pterm.BgBlack)).
Printf("✨ %s version details ✨", constants.BinaryName)
pterm.Println()
versionTable := table.NewWriter()
versionTable.SetOutputMirror(os.Stdout)
versionTable.AppendHeader(table.Row{"Info", "Content"})
versionTable.AppendRows([]table.Row{
{"CLI Name", cliName},
{"CLI Version", cliVersion},
{"Project URL", projectURL},
{"Build Date", builtDate},
{"Build by", builtBy},
{"Commit", commit},
{"Commit Short", commitShort},
{"Commit Branch", commitBranch},
{"Go Version", goVersion},
{"CLI Name", cliName},
{"CLI Version", cliVersion},
{"Project URL", projectURL},
{"Build Date", builtDate},
{"Build by", builtBy},
{"Commit", commit},
{"Commit Short", commitShort},
{"Commit Branch", commitBranch},
{"Go Version", goVersion},
})
versionTable.SetStyle(table.StyleColoredBright)
versionTable.Render()
fmt.Println()
pterm.Println()
}

// ShowVersion function - check detail flag and show the pretty details if enabled (`true`).
func ShowVersion(pretty bool) {
if pretty {
GetPrettyDetails()
} else {
fmt.Printf("loli version %s (%s)\n", cliVersion, builtDate)
pterm.Println()
pterm.DefaultHeader.
WithMargin(5).
WithBackgroundStyle(pterm.NewStyle(pterm.BgBlack)).
Printf("✨ %s version %s ✨\n\n (%s)", cliName, cliVersion, builtDate)
pterm.Println()
}
}

0 comments on commit 76152c7

Please sign in to comment.