Skip to content

Commit

Permalink
use debug info to get version
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 2, 2023
1 parent d16c3fc commit 445251d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 10 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"os"
"runtime/debug"

"github.com/JunNishimura/Goit/internal/file"
"github.com/JunNishimura/Goit/internal/store"
Expand Down Expand Up @@ -37,15 +38,22 @@ var rootCmd = &cobra.Command{
}

func Execute(version string) {
goitVersion = version
// set version
if version == "" {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
goitVersion = buildInfo.Main.Version
}
} else {
goitVersion = version
}

err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// get client
rootGoitPath, _ := file.FindGoitRoot(".") // ignore the error since the error is not important
config, err := store.NewConfig(rootGoitPath)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ Copyright © 2023 Jun Nishimura <[email protected]>
*/
package main

import (
"fmt"

"github.com/JunNishimura/Goit/cmd"
)
import "github.com/JunNishimura/Goit/cmd"

var version = ""

func main() {
fmt.Println(version)
cmd.Execute(version)
}

0 comments on commit 445251d

Please sign in to comment.