mirror of
https://github.com/komodorio/helm-dashboard.git
synced 2026-03-24 11:48:04 +00:00
fixbug: ChartAndVersion did not handle chart version correct in some case (#98)
* fixbug: ChartAndVersion did not handle chart version correct in some case https://github.com/komodorio/helm-dashboard/issues/95 * use regexp to get version * add test case
This commit is contained in:
@@ -3,13 +3,15 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var FailLogLevel = log.WarnLevel // allows to suppress error logging in some situations
|
var FailLogLevel = log.WarnLevel // allows to suppress error logging in some situations
|
||||||
@@ -17,12 +19,19 @@ var FailLogLevel = log.WarnLevel // allows to suppress error logging in some sit
|
|||||||
type ControlChan = chan struct{}
|
type ControlChan = chan struct{}
|
||||||
|
|
||||||
func ChartAndVersion(x string) (string, string, error) {
|
func ChartAndVersion(x string) (string, string, error) {
|
||||||
lastInd := strings.LastIndex(x, "-")
|
strs := strings.Split(x, "-")
|
||||||
if lastInd < 0 {
|
lens := len(strs)
|
||||||
|
if lens < 2 {
|
||||||
return "", "", errors.New("can't parse chart version string")
|
return "", "", errors.New("can't parse chart version string")
|
||||||
|
} else if lens == 2 {
|
||||||
|
return strs[0], strs[1], nil
|
||||||
|
} else {
|
||||||
|
// semver2 regex , add optional v prefix
|
||||||
|
re := regexp.MustCompile(`v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`)
|
||||||
|
match := re.FindString(x)
|
||||||
|
lastInd := strings.LastIndex(x, match)
|
||||||
|
return x[:lastInd-1], match, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return x[:lastInd], x[lastInd+1:], nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TempFile(txt string) (string, func(), error) {
|
func TempFile(txt string) (string, func(), error) {
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetQueryProps(t *testing.T) {
|
func TestGetQueryProps(t *testing.T) {
|
||||||
@@ -69,6 +70,27 @@ func TestChartAndVersion(t *testing.T) {
|
|||||||
wantVer: "1.0.0",
|
wantVer: "1.0.0",
|
||||||
wantError: false,
|
wantError: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Chart and version - successfully parsing chart and version",
|
||||||
|
params: "chart-v1.0.0",
|
||||||
|
wantChart: "chart",
|
||||||
|
wantVer: "v1.0.0",
|
||||||
|
wantError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Chart and version - successfully parsing chart and version",
|
||||||
|
params: "chart-v1.0.0-alpha",
|
||||||
|
wantChart: "chart",
|
||||||
|
wantVer: "v1.0.0-alpha",
|
||||||
|
wantError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Chart and version - successfully parsing chart and version",
|
||||||
|
params: "chart-1.0.0-alpha",
|
||||||
|
wantChart: "chart",
|
||||||
|
wantVer: "1.0.0-alpha",
|
||||||
|
wantError: false,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Chart and version - parsing chart without version",
|
name: "Chart and version - parsing chart without version",
|
||||||
params: "chart",
|
params: "chart",
|
||||||
|
|||||||
Reference in New Issue
Block a user