Skip to content

Commit

Permalink
improve version printing; color logo
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Oct 16, 2023
1 parent 3490f11 commit 9bf90b0
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 23 deletions.
9 changes: 8 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ builds:
binary: pkup
main: ./main.go
ldflags:
- -s -w -X main.version=v{{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
- -s
- -w
- -X main.version=v{{.Version}}
- -X main.commit={{.Commit}}
- -X main.date={{.CommitDate}}
- -X main.buildOs={{.Os}}
- -X main.buildArch={{.Arch}}
- -X main.builtBy=goreleaser
goos:
- linux
- darwin
Expand Down
5 changes: 5 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/pPrecel/PKUP/internal/logo"
"github.com/pPrecel/PKUP/internal/view"
"github.com/pPrecel/PKUP/pkg/artifacts"
"github.com/pPrecel/PKUP/pkg/github"
Expand All @@ -30,6 +31,10 @@ func NewGenCommand(opts *Options) *cli.Command {
Aliases: []string{"g", "generate", "get"},
Flags: getGenFlags(actionsOpts),
Action: func(ctx *cli.Context) error {
if !actionsOpts.ci {
// print logo before any action
fmt.Printf("%s\n\n", logo.Build())
}
if err := actionsOpts.setDefaults(); err != nil {
return err
}
Expand Down
15 changes: 13 additions & 2 deletions cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
)

type Options struct {
Version string
Log *pterm.Logger
BuildVersion string
BuildCommit string
BuildDate string
BuildOs string
BuildArch string
Log *pterm.Logger
}

type genActionOpts struct {
Expand All @@ -24,6 +28,13 @@ type genActionOpts struct {
ci bool
}

type versionActionOpts struct {
*Options
v bool
vv bool
ci bool
}

func (opts *genActionOpts) setDefaults() error {
if opts.dir == "" {
pwd, err := os.Getwd()
Expand Down
66 changes: 64 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,81 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
)

func NewVersionCommand(opts *Options) *cli.Command {
actionOpts := &versionActionOpts{
Options: opts,
}
return &cli.Command{
Name: "version",
Aliases: []string{"v"},
Usage: "Shows tool version",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "ci",
Usage: "print output using standard log and JSON format",
Aliases: []string{"c"},
Action: func(_ *cli.Context, b bool) error {
actionOpts.ci = b
opts.Log = opts.Log.WithFormatter(pterm.LogFormatterJSON)
return nil
},
},
&cli.BoolFlag{
Name: "v",
Usage: "show more info",
Destination: &actionOpts.v,
DisableDefaultText: true,
},
&cli.BoolFlag{
Name: "vv",
Usage: "show much more info",
Destination: &actionOpts.vv,
DisableDefaultText: true,
},
},
Action: func(_ *cli.Context) error {
fmt.Printf("pkup-gen version: %s\n", opts.Version)
return nil
text, err := buildVersionOutput(actionOpts)
fmt.Print(text)
return err
},
}
}

func buildVersionOutput(opts *versionActionOpts) (string, error) {
data := getVersionData(opts)

if opts.ci {
byteData, err := json.Marshal(data)
return string(byteData), err
}

text := ""
for key, value := range data {
text += fmt.Sprintf("%s: %s\n", key, value)
}

return text, nil
}

func getVersionData(opts *versionActionOpts) map[string]string {
data := map[string]string{}
data["version"] = opts.BuildVersion
if opts.v {
data["build-commit"] = opts.BuildCommit
data["build-data"] = opts.BuildDate
} else if opts.vv {
data["build-commit"] = opts.BuildCommit
data["build-data"] = opts.BuildDate
data["build-arch"] = opts.BuildArch
data["build-os"] = opts.BuildOs
}

return data
}
40 changes: 40 additions & 0 deletions internal/logo/logo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package logo

import (
"fmt"

"github.com/pterm/pterm"
)

var (
pkupLayers = []string{
`.______ __ ___ __ __ .______`,
`| _ \ | |/ / | | | | | _ \`,
`| |_) | | ' / | | | | | |_) |`,
`| ___/ | < | | | | | ___/`,
`| | | . \ | '--' | | | `,
`| _| |__|\__\ \______/ | _| `,
` `,
}
genLayers = []string{
``,
``,
`__ _ ___ _ __`,
`/ _' |/ _ \ '_ \`,
`| (_| | __/ | | |`,
`\__, |\___|_| |_|`,
`|___/`,
}
)

func Build() string {
return fmt.Sprint(
pkupLayers[0], genLayers[0], "\n",
pkupLayers[1], genLayers[1], "\n",
pkupLayers[2], pterm.Bold.Sprint(pterm.LightRed(genLayers[2])), "\n",
pkupLayers[3], pterm.Bold.Sprint(pterm.LightRed(genLayers[3])), "\n",
pkupLayers[4], pterm.Bold.Sprint(pterm.LightRed(genLayers[4])), "\n",
pkupLayers[5], pterm.Bold.Sprint(pterm.LightRed(genLayers[5])), "\n",
pkupLayers[6], pterm.Bold.Sprint(pterm.LightRed(genLayers[6])),
)
}
29 changes: 11 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package main

import (
"fmt"
"os"

"github.com/pPrecel/PKUP/cmd"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
)

const (
logo = `
.______ __ ___ __ __ .______
| _ \ | |/ / | | | | | _ \
| |_) | | ' / | | | | | |_) |__ _ ___ _ __
| ___/ | < | | | | | ___// _' |/ _ \ '_ \
| | | . \ | '--' | | | | (_| | __/ | | |
| _| |__|\__\ \______/ | _| \__, |\___|_| |_|
|___/`
)

var (
version = "local"
version = "local"
commit = "local"
date = "local"
buildOs = "local"
buildArch = "local"
)

func main() {
// print logo before any action
fmt.Printf("%s\n\n", logo)

log := pterm.DefaultLogger.
WithTime(false)
opts := &cmd.Options{
Version: version,
Log: log,
BuildVersion: version,
BuildCommit: commit,
BuildDate: date,
BuildOs: buildOs,
BuildArch: buildArch,
Log: log,
}

app := &cli.App{
Expand Down

0 comments on commit 9bf90b0

Please sign in to comment.