Skip to content

Commit

Permalink
add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Nov 1, 2023
1 parent c9f9f76 commit 3ec4b7e
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 12 deletions.
23 changes: 21 additions & 2 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ func New() error {
fmt.Println("Usage:\n dp [command]")
fmt.Println("\nCommands:")
fmt.Println(" strategic: generate domain strategic diagram")
fmt.Println(" tactic: generate domain tactic diagram")
fmt.Println(" normal: generate normal arch diagram")
fmt.Println(" tactic: generate domain tactic diagram")
fmt.Println(" normal: generate normal arch diagram")
fmt.Println(" open: open arch diagram")
fmt.Println(" version: show dddplayer command version")

fmt.Println("\nExample:")
fmt.Println(" dp normal -m ~/github/dddplayer/dp -p github.com/dddplayer/dp/internal/domain")
Expand All @@ -36,6 +38,23 @@ func New() error {
subCommand := topLevel.Args()[0]

switch subCommand {
case "version":
versionCmd, err := cmd.NewVersionCmd(topLevel)
if err != nil {
return err
}
if err := versionCmd.Run(); err != nil {
return err
}
case "open":
openCmd, err := cmd.NewOpenCmd(topLevel)
if err != nil {
return err
}
if err := openCmd.Run(); err != nil {
return err
}

case "normal":
normalCmd, err := cmd.NewNormalCmd(topLevel)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/interfaces/cmd/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

const officialWebsiteUrl = "https://dddplayer.com"

func open(raw string) {
func open(raw string) error {
encoded := encodeURIComponent(raw)
err := openBrowser(fmt.Sprintf("%s/#%s", officialWebsiteUrl, encoded))
if err != nil {
fmt.Println(err)
return
return err
}
return nil
}

func encodeURIComponent(str string) string {
Expand Down
16 changes: 12 additions & 4 deletions internal/interfaces/cmd/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ func normalCompositionGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}

if err = writeToDisk(dot, filename(domain, "composition"), mainPkg); err != nil {
return err
Expand All @@ -98,7 +100,9 @@ func normalDetailGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}

if err = writeToDisk(dot, filename(domain, "detail"), mainPkg); err != nil {
return err
Expand All @@ -116,7 +120,9 @@ func normalMessageFlowGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}

if err = writeToDisk(dot, filename(domain, "messageflow"), mainPkg); err != nil {
return err
Expand All @@ -134,7 +140,9 @@ func normalGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}

if err = writeToDisk(dot, filename(domain, ""), mainPkg); err != nil {
return err
Expand Down
54 changes: 54 additions & 0 deletions internal/interfaces/cmd/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"errors"
"flag"
"fmt"
"os"
)

type openCmd struct {
parent *flag.FlagSet
cmd *flag.FlagSet
archDiagramPath *string
}

func NewOpenCmd(parent *flag.FlagSet) (*openCmd, error) {
nCmd := &openCmd{
parent: parent,
}

nCmd.cmd = flag.NewFlagSet("normal", flag.ExitOnError)
nCmd.archDiagramPath = nCmd.cmd.String("p", "", fmt.Sprintf(
"[required] target arch diagram path \n(e.g. %s)", "dddplayer/arch.dot"))

err := nCmd.cmd.Parse(parent.Args()[1:])
if err != nil {
return nil, err
}

return nCmd, nil
}

func (oc *openCmd) Usage() {
oc.cmd.Usage()
}

func (oc *openCmd) Run() error {
if *oc.archDiagramPath == "" {
oc.cmd.Usage()
return errors.New("please specify a target arch diagram path")
}

_, err := os.Stat(*oc.archDiagramPath)
if os.IsNotExist(err) {
return fmt.Errorf("file %s does not exist", *oc.archDiagramPath)
}

dotStr, err := os.ReadFile(*oc.archDiagramPath)
if err != nil {
return err
}

return open(string(dotStr))
}
4 changes: 3 additions & 1 deletion internal/interfaces/cmd/strategic.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func strategicGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}
if err = writeToDisk(dot, strings.ReplaceAll(path.Join(domain, "detail"), "/", "."), mainPkg); err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions internal/interfaces/cmd/tactic.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func tacticGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}
if err = writeToDisk(dot, strings.ReplaceAll(domain, "/", "."), mainPkg); err != nil {
return err
}
Expand All @@ -85,7 +87,9 @@ func detailTacticGraph(mainPkg, domain string) error {
return err
}

open(dot)
if err = open(dot); err != nil {
return err
}
if err = writeToDisk(dot, strings.ReplaceAll(path.Join(domain, "detail"), "/", "."), mainPkg); err != nil {
return err
}
Expand Down
140 changes: 140 additions & 0 deletions internal/interfaces/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package cmd

import (
"flag"
"fmt"
"runtime/debug"
"sync"
)

type versionCmd struct {
parent *flag.FlagSet
cmd *flag.FlagSet
}

func NewVersionCmd(parent *flag.FlagSet) (*versionCmd, error) {
nCmd := &versionCmd{
parent: parent,
}

nCmd.cmd = flag.NewFlagSet("version", flag.ExitOnError)
err := nCmd.cmd.Parse(parent.Args()[1:])
if err != nil {
return nil, err
}

return nCmd, nil
}

func (oc *versionCmd) Usage() {
oc.cmd.Usage()
}

func (oc *versionCmd) Run() error {
fmt.Println(BuildVersionString())
return nil
}

func BuildVersionString() string {
// program := "Hugo Static Site Generator"
program := "dddplayer"

version := "v" + CurrentVersion.String()

bi := getBuildInfo()
if bi == nil {
return version
}
if bi.Revision != "" {
version += "-" + bi.Revision
}

osArch := bi.GoOS + "/" + bi.GoArch

date := bi.RevisionTime
if date == "" {
// Accept vendor-specified build date if .git/ is unavailable.
date = "unknown"
}

versionString := fmt.Sprintf("%s %s %s BuildDate=%s",
program, version, osArch, date)

return versionString
}

var (
bInfo *buildInfo
bInfoInit sync.Once
)

type buildInfo struct {
VersionControlSystem string
Revision string
RevisionTime string
Modified bool

GoOS string
GoArch string

*debug.BuildInfo
}

func getBuildInfo() *buildInfo {
bInfoInit.Do(func() {
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

bInfo = &buildInfo{BuildInfo: bi}

for _, s := range bInfo.Settings {
switch s.Key {
case "vcs":
bInfo.VersionControlSystem = s.Value
case "vcs.revision":
bInfo.Revision = s.Value
case "vcs.time":
bInfo.RevisionTime = s.Value
case "vcs.modified":
bInfo.Modified = s.Value == "true"
case "GOOS":
bInfo.GoOS = s.Value
case "GOARCH":
bInfo.GoArch = s.Value
}
}
})

return bInfo
}

var CurrentVersion = Version{
Major: 0,
Minor: 2,
PatchLevel: 0,
Suffix: "",
}

// Version represents the Hugo build version.
type Version struct {
Major int

Minor int

// Increment this for bug releases
PatchLevel int

// HugoVersionSuffix is the suffix used in the Hugo version string.
// It will be blank for release versions.
Suffix string
}

func (v Version) String() string {
return version(v.Major, v.Minor, v.PatchLevel, v.Suffix)
}

func version(major, minor, patch int, suffix string) string {
return fmt.Sprintf("%d.%d.%d%s", major, minor, patch, suffix)
}

0 comments on commit 3ec4b7e

Please sign in to comment.