Skip to content

Commit

Permalink
added validation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-hermann-sva committed Jan 6, 2024
1 parent 2f2ada2 commit 921e06f
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 33 deletions.
32 changes: 6 additions & 26 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/stuttgart-things/kaeffken/modules"

"github.com/spf13/cobra"
sthingsBase "github.com/stuttgart-things/sthingsBase"
sthingsCli "github.com/stuttgart-things/sthingsCli"
)

var (
Expand Down Expand Up @@ -49,38 +47,20 @@ var deployCmd = &cobra.Command{
}

// LOAD CLUSTERFILE - DEFAULT IS <ROOT>/<ENV>/<LAB>/clusters.yaml
repository, cloned := sthingsCli.CloneGitRepository(values["repository"], values["branch"], values["commitID"], nil)
fmt.Println(repository)
repository, cloned := modules.CloneGitRepository(values)

if !cloned {
log.Error("GIT REPOSITORY CAN NOT BE CLONED: ", values["repository"])
os.Exit(3)
}
// LOAD CLUSTERSFILE
fileList, directoryList := sthingsCli.GetFileListFromGitRepository("clusters/labul/", repository)
fmt.Println(fileList, directoryList)

if sthingsBase.CheckForStringInSlice(fileList, values["clustersfileName"]) {
clusterFile := sthingsCli.ReadFileContentFromGitRepo(repository, "clusters/labul/"+values["clustersfileName"])
fmt.Println(clusterFile)
} else {
log.Error("CLUSTERFILE DOES NOT EXIST IN REPOSITORY: ", gitRepository+":"+"clusters/labul/"+values["clustersfileName"])
os.Exit(3)
}
// LOAD CLUSTERSFILE
clustersFile := modules.LoadDataFromRepository(repository, values["clusterFilePath"])
fmt.Println(clustersFile)

// LOAD FLUX INFRA CATALOGUE
fileList, directoryList = sthingsCli.GetFileListFromGitRepository("clusters/labul/", repository)
fmt.Println(fileList, directoryList)

if sthingsBase.CheckForStringInSlice(fileList, values["clustersfileName"]) {
fmt.Println("LETS GOOO")
infraCatalog := sthingsCli.ReadFileContentFromGitRepo(repository, "clusters/config/"+"infraCatalog.json")
fmt.Println(infraCatalog)
} else {
log.Error("CLUSTERFILE DOES NOT EXIST IN REPOSITORY: ", gitRepository+":"+"clusters/config/"+"infraCatalog.json")
os.Exit(3)
}

infraCatalogue := modules.LoadDataFromRepository(repository, "clusters/config/infraCatalog.json")
fmt.Println(infraCatalogue)
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/stuttgart-things/kaeffken
go 1.21.1

require (
github.com/go-git/go-billy/v5 v5.4.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/stuttgart-things/sthingsBase v0.1.33
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/git-chglog/git-chglog v0.15.4 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-git/go-git/v5 v5.8.1 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright © 2024 PATRICK HERMANN PATRICK.HERMANN@SVA.DE
*/
package main

import "github.com/stuttgart-things/kaeffken/cmd"
import cmd "github.com/stuttgart-things/kaeffken/cmd"

func main() {
defCmd := "deploy"
Expand Down
22 changes: 22 additions & 0 deletions modules/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright © 2024 PATRICK HERMANN PATRICK.HERMANN@SVA.DE
*/
package modules

import (
billy "github.com/go-git/go-billy/v5"

sthingsCli "github.com/stuttgart-things/sthingsCli"
)

// LOAD CLUSTERFILE - DEFAULT IS <ROOT>/<ENV>/<LAB>/clusters.yaml
func CloneGitRepository(values map[string]string) (repository billy.Filesystem, repositoryCloned bool) {
repository, repositoryCloned = sthingsCli.CloneGitRepository(values["repository"], values["branch"], values["commitID"], nil)

if !repositoryCloned {
log.Error("GIT REPOSITORY CAN NOT BE CLONED: ", values["repository"])
repositoryCloned = false
}

return repository, repositoryCloned
}
32 changes: 32 additions & 0 deletions modules/clone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright © 2024 PATRICK HERMANN PATRICK.HERMANN@SVA.DE
*/
package modules

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCloneGitRepository(t *testing.T) {

assert := assert.New(t)

repoworks := make(map[string]string)
repoworks["repository"] = "https://github.com/stuttgart-things/kaeffken.git"
repoworks["branch"] = "main"
repoworks["gitCommitID"] = "09de9ff7b5c76aff8bb32f68cfb0bbe49cd5a7a8"

repoworksNot := make(map[string]string)
repoworksNot["repository"] = "https://github.com/stuttgart-things/kaeffken.git"
repoworksNot["branch"] = "test"
repoworksNot["gitCommitID"] = "09de9ff7b5c76aff8bb32f68cfb0bbe49cd5a7a8"

_, cloned := CloneGitRepository(repoworks)
assert.Equal(cloned, true)

_, cloned = CloneGitRepository(repoworksNot)
assert.Equal(cloned, false)

}
36 changes: 36 additions & 0 deletions modules/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright © 2024 PATRICK HERMANN PATRICK.HERMANN@SVA.DE
*/
package modules

import (
"fmt"
"path/filepath"

billy "github.com/go-git/go-billy/v5"
sthingsBase "github.com/stuttgart-things/sthingsBase"
sthingsCli "github.com/stuttgart-things/sthingsCli"
)

// LOAD CLUSTERFILE - DEFAULT IS <ROOT>/<ENV>/<LAB>/clusters.yaml
func LoadDataFromRepository(repository billy.Filesystem, filePath string) (loadedFile string) {

loadedFile = ""
file := filepath.Base(filePath)
folder := filepath.Dir(filePath)

fmt.Println(file)
fmt.Println(folder)

// GET FILELIST
fileList, directoryList := sthingsCli.GetFileListFromGitRepository(folder, repository)
fmt.Println(fileList, directoryList)

if sthingsBase.CheckForStringInSlice(fileList, file) {
loadedFile = sthingsCli.ReadFileContentFromGitRepo(repository, filePath)
return
} else {
log.Error("FILE DOES NOT EXIST IN REPOSITORY: ", filePath)
return
}
}
47 changes: 47 additions & 0 deletions modules/load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright © 2024 PATRICK HERMANN PATRICK.HERMANN@SVA.DE
*/
package modules

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoadDataFromRepository(t *testing.T) {

assert := assert.New(t)

type test struct {
filePath string
want bool
}

tests := []test{
{filePath: "cmd/deploy.go", want: true},
{filePath: "cmd/deploy.go1", want: false},
}

repoworks := make(map[string]string)
repoworks["repository"] = "https://github.com/stuttgart-things/kaeffken.git"
repoworks["branch"] = "main"
repoworks["gitCommitID"] = "2f2ada234ffee467195439dacfe4a5579be3f66a"

repository, cloned := CloneGitRepository(repoworks)

for _, tc := range tests {
var fileLoaded bool

if cloned {
loadedFile := LoadDataFromRepository(repository, tc.filePath)

if loadedFile != "" {
fileLoaded = true
}
}

assert.Equal(fileLoaded, tc.want)
}

}
1 change: 0 additions & 1 deletion modules/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func VerifyValues(values map[string]string, mandatoryFlags []string) (validValue
validValues = false
} else {
log.Warn(strings.ToUpper(key) + " is unset")

}
}
}
Expand Down
6 changes: 2 additions & 4 deletions modules/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ Copyright © 2024 PATRICK HERMANN PATRICK.HERMANN@SVA.DE
package modules

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestVerifyValues(t *testing.T) {

assert := assert.New(t)

type test struct {
mandatoryFlags []string
values map[string]string
Expand All @@ -27,11 +28,8 @@ func TestVerifyValues(t *testing.T) {
{mandatoryFlags: []string{"repository"}, values: values1, want: true},
}

assert := assert.New(t)

for _, tc := range tests {
validValues := VerifyValues(tc.values, tc.mandatoryFlags)
fmt.Println(validValues)
assert.Equal(validValues, tc.want)
}

Expand Down

0 comments on commit 921e06f

Please sign in to comment.