Skip to content

Commit

Permalink
change version via Makefile(incomplete)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaysomani07 committed Feb 23, 2023
1 parent 270358f commit 826b8f7
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 25 deletions.
18 changes: 13 additions & 5 deletions Makefile
@@ -1,14 +1,22 @@
setup:
GO_FLAGS ?=
NAME := cloudlens
OUTPUT_BIN ?= execs/${NAME}
PACKAGE := github.com/one2nc/$(NAME)
VERSION ?= v0.26.10

ßsetup:
docker-compose up -d

setup-down:
docker ps -a --format "{{.ID}} {{.Names}}" | grep cloudlens| awk '{print $$1}'| xargs docker stop | xargs docker rm -v

build:
go build -o cloudlens main.go

go build \
-ldflags "-w -s -X ${PACKAGE}/cmd.version=${VERSION}" \
-a -tags netgo -o ${OUTPUT_BIN} main.go

run: build
./cloudlens
./execs/cloudlens

populate: build
./cloudlens lspop
./execs/cloudlens lspop
19 changes: 11 additions & 8 deletions cmd/root.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/mattn/go-colorable"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/aws"
"github.com/one2nc/cloud-lens/internal/color"
Expand All @@ -16,19 +17,21 @@ import (
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: `cloudlens`,
Short: `cli for aws services`,
Long: `cli for aws services[s3, ec2, security-groups, iam]`,
Run: run,
}

var (
profile, region string
version = "v0.1.0"
version = "dev"
rootCmd = &cobra.Command{
Use: `cloudlens`,
Short: `cli for aws services`,
Long: `cli for aws services[s3, ec2, security-groups, iam]`,
Run: run,
}
out = colorable.NewColorableStdout()
)

func init() {
fmt.Println("version val is:", version)
rootCmd.AddCommand(versionCmd())
rootCmd.PersistentFlags().StringVarP(&profile, "profile", "p", "default", "Read aws profile")
rootCmd.PersistentFlags().StringVarP(&region, "region", "r", "", "Read aws region")
}
Expand Down
65 changes: 54 additions & 11 deletions cmd/version.go
@@ -1,24 +1,67 @@
package cmd

// import (
// "fmt"

// "github.com/spf13/cobra"
// )

// func versionCmd() *cobra.Command {
// var command = &cobra.Command{
// Use: "version",
// Short: "Print version/build info",
// Long: "Print version/build information",
// Run: func(cmd *cobra.Command, args []string) {
// printVersion()
// },
// }
// return command
// }

// func printVersion() {
// fmt.Println(version)
// }

import (
"fmt"

"github.com/one2nc/cloud-lens/internal/color"
"github.com/spf13/cobra"
)

var command = &cobra.Command{
Use: "version",
Short: "Print version/build info",
Long: "Print version/build information",
Run: func(cmd *cobra.Command, args []string) {
printVersion("version")
},
func versionCmd() *cobra.Command {
var short bool

command := cobra.Command{
Use: "version",
Short: "Print version/build info",
Long: "Print version/build information",
Run: func(cmd *cobra.Command, args []string) {
printVersion(short)
},
}

command.PersistentFlags().BoolVarP(&short, "short", "s", false, "Prints cloudlens version info in short format")

return &command
}

func init() {
rootCmd.AddCommand(command)
func printVersion(short bool) {
const fmat = "%-20s %s\n"
var outputColor color.Paint

if short {
outputColor = -1
}
printTuple(fmat, "Version", version, outputColor)
// printTuple(fmat, "Commit", commit, outputColor)
// printTuple(fmat, "Date", date, outputColor)
}

func printVersion(fmat string) {
fmt.Println(version)
func printTuple(fmat, section, value string, outputColor color.Paint) {
if outputColor != -1 {
fmt.Fprintf(out, fmat, color.Colorize(section+":", outputColor), value)
return
}
fmt.Fprintf(out, fmat, section, value)
}
56 changes: 56 additions & 0 deletions internal/model/semver.go
@@ -0,0 +1,56 @@
package model

import (
"fmt"
"regexp"
"strconv"
)

var versionRX = regexp.MustCompile(`\Av(\d+)\.(\d+)\.(\d+)\z`)

// SemVer represents a semantic version.
type SemVer struct {
Major, Minor, Patch int
}

// NewSemVer returns a new semantic version.
func NewSemVer(version string) *SemVer {
var v SemVer
v.Major, v.Minor, v.Patch = v.parse(NormalizeVersion(version))

return &v
}

// String returns version as a string.
func (v *SemVer) String() string {
return fmt.Sprintf("v%d.%d.%d", v.Major, v.Minor, v.Patch)
}

func (*SemVer) parse(version string) (major, minor, patch int) {
mm := versionRX.FindStringSubmatch(version)
if len(mm) < 4 {
return
}
major, _ = strconv.Atoi(mm[1])
minor, _ = strconv.Atoi(mm[2])
patch, _ = strconv.Atoi(mm[3])

return
}

// NormalizeVersion ensures the version starts with a v.
func NormalizeVersion(version string) string {
fmt.Println("inside normalise")
if version == "" {
return version
}
if version[0] == 'v' {
return version
}
return "v" + version
}

// IsCurrent asserts if at latest release.
func (v *SemVer) IsCurrent(latest *SemVer) bool {
return v.Major >= latest.Major && v.Minor >= latest.Minor && v.Patch >= latest.Patch
}
2 changes: 1 addition & 1 deletion internal/view/app.go
Expand Up @@ -44,7 +44,7 @@ func NewApp() *App {

// TODO keep context param at first place always
func (a *App) Init(ctx context.Context, profiles, regions []string, version string) error {
a.version = version
a.version = model.NormalizeVersion(version)
ctx = context.WithValue(ctx, internal.KeyActiveProfile, profiles[0])
ctx = context.WithValue(ctx, internal.KeyActiveRegion, regions[0])
ctx = context.WithValue(ctx, internal.KeyApp, a)
Expand Down

0 comments on commit 826b8f7

Please sign in to comment.