Commit b0dab6c1 authored by odatskov's avatar odatskov
Browse files

Get repository list

parent d11f2df9
Pipeline #458717 passed with stage
in 3 minutes and 7 seconds
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func getFileList(root string, args *[]string, files *[]string) {
func getRepoName(path string) string {
file_data, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("Could not open: %s", path)
} else {
file_lines := strings.Split(string(file_data), "\n")
for _, line := range file_lines {
if strings.Contains(line, "url:") {
repo_url := strings.Split(line, "url:")
return repo_url[1]
}
}
log.Printf("Repo url not found in: %s", path)
}
return "none"
}
func getRepoList(root string, args *[]string, repos *[]string) {
if len(*args) == 1 {
// Recusively walk through the root path, creating list of files with *.yaml
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(path, ".yaml") {
*files = append(*files, path)
*repos = append(*repos, getRepoName(path))
}
return nil
})
if len(*files) == 0 { log.Fatalf("No files found") }
if len(*repos) == 0 { log.Fatalf("No Repositories found") }
} else {
var err_files []string
// Check if files passed through arguments actually exist
for _, file := range *args[1:] {
for _, file := range (*args)[1:] {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
err_files = append(err_files, file)
}
} else { *files=append(*files,file) }
} else { *repos = append(*repos, getRepoName(file)) }
}
if len(err_files) > 0 { log.Fatalf("File(s) not found: %s", err_files) }
}
}
func main() {
var files []string
var repos []string
getRepoList(".", &os.Args, &repos)
log.Printf("Returned list of repositories: %s", repos)
getFileList(".", &os.Args,&files)
log.Printf("Returned list of files: %s",files)
// Check that the token still is valid
// For each file:
// Get the manifest and check for changes
......
......@@ -9,27 +9,38 @@ import (
//"math/rand"
)
func Test_getFileList(t *testing.T) {
func getFileList() []string {
cmd := strings.Split("find . -name *.yaml"," ")
test_cmd, _ := exec.Command(cmd[0],cmd[1:]...).Output()
test_str := strings.Split(string(test_cmd),"\n")
log.Printf("Test string: %s",test_str)
out_cmd, _ := exec.Command(cmd[0],cmd[1:]...).Output()
out_list := strings.Split(string(out_cmd),"\n")
return out_list[:len(out_list)-1]
}
for i:=1; i < len(test_str); i++ {
var out_test []string
getFileList(".", &test_str[:i],&out_test)
func Test_getRepoList(t *testing.T) {
t.Run(fmt.Sprintf("Size test for %d argument(s)",i), func(t *testing.T) {
if len(out_test) != len(test_str[:1]) {
t.Errorf("For %s expected size of %d, received %d", test_str[:i],len(test_str[:i]),len(out_test))
files := getFileList()
log.Printf("The repository contains %d files",len(files))
in_args := []string{"cmd_line"}
var repos []string
getRepoList(".", &in_args,&repos)
t.Run(fmt.Sprintf("Size test for no arguments"), func(t *testing.T) {
if len(files) != len(repos) {
t.Errorf("For all files expected size of %d, received %d",len(files),len(repos))
}
})
for _, path := range files {
in_args = append(in_args,path)
repos = nil
getRepoList(".", &in_args,&repos)
t.Run(fmt.Sprintf("Size test for %d argument(s)",len(in_args)), func(t *testing.T) {
if len(in_args)-1 != len(repos) {
t.Errorf("For %s expected size of %d, received %d with output: %s", in_args,len(in_args)-1,len(repos),repos)
}
})
}
t.Run(fmt.Sprintf("File path equivalence"), func(t *testing.T) {
var out_test []string
getFileList(".",&test_str,&out_test)
if out_test[0] != test_str[0] { t.Errorf("For %s, received %s", test_str[0], out_test[0]) }
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment