Commit fcb89e8e authored by odatskov's avatar odatskov
Browse files

Custom class/struct for repository list slices

parent b0dab6c1
Pipeline #459057 passed with stage
in 31 seconds
/*
sync provides synchronisatoin of docker images with cvmfs based on provided repository lists.
If no paths were specified, all yaml files (searched recursively) within current path will be used.
Usage: ./sync [file paths]
The repository description is expected to contain the url, description and maintainer fields.
For example:
url: docker.io/ubuntu:latest
description: Ubuntu image from DockerHub
maintainer: ubuntu@awesome.mail.com
*/
package main
import (
......@@ -6,37 +19,73 @@ import (
"os"
"path/filepath"
"strings"
//"net/http"
)
func getRepoName(path string) string {
//----------- Global constants ----------------//
const docker = "docker.registry.io"
const gitlab = "gitlab-registry.cern.ch"
type Repo struct {
url string // Clean url path without registry
registry string // Registry path for API calls
}
//--- Repo struct constructor -----------------//
func makeRepo(path string) Repo {
var url []string
var registry string
if strings.HasPrefix(path, "docker://") {
registry = docker
url = strings.Split(path, "//")
} else if strings.HasPrefix(path, "docker.io") {
registry = docker
url = strings.Split(path, ".io/")
} else if strings.HasPrefix(path, "gitlab-registry.cern.ch") {
registry = gitlab
url = strings.Split(path, ".cern.ch/")
} else {
log.Printf("Unknown repository specified: %s", path)
}
return Repo{url[1], registry}
}
//--- Retrieve file line containing a field ----//
func getField(field string, 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]
if strings.Contains(line, field) {
repo_url := strings.Split(line, field)
return strings.TrimSpace(repo_url[1])
}
}
log.Printf("Repo url not found in: %s", path)
log.Printf("Field %s not found in: %s", field, path)
}
return "none"
}
func getRepoList(root string, args *[]string, repos *[]string) {
//--- Return list of repositories --------//
func getRepoList(root string, args *[]string, repos *[]Repo) {
if len(*args) == 1 {
// Recusively walk through the root path, creating list of files with *.yaml
// Recursively walk through root/pwd 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") {
*repos = append(*repos, getRepoName(path))
*repos = append(*repos, makeRepo(getField("url:", path)))
}
return nil
})
if len(*repos) == 0 { log.Fatalf("No Repositories found") }
if len(*repos) == 0 {
log.Fatalf("No Repositories found")
}
} else {
var err_files []string
......@@ -47,21 +96,33 @@ func getRepoList(root string, args *[]string, repos *[]string) {
if os.IsNotExist(err) {
err_files = append(err_files, file)
}
} else { *repos = append(*repos, getRepoName(file)) }
} else {
*repos = append(*repos, makeRepo(getField("url:", file)))
}
}
if len(err_files) > 0 {
log.Fatalf("File(s) not found: %s", err_files)
}
if len(err_files) > 0 { log.Fatalf("File(s) not found: %s", err_files) }
}
}
//func getManifest(repo string) {
// log.Printf("Checking out repository: %s",repo)
//}
//----------------------------------------//
func main() {
var repos []string
var repos []Repo
getRepoList(".", &os.Args, &repos)
log.Printf("Returned list of repositories: %s", repos)
// Check that the token still is valid
// For each file:
// Get the manifest and check for changes
// Get the manifest and check for changes
//getManifest(repos[0])
// Fetch the layers
// Unpack the structure
// Adjust permissions on the files
......
......@@ -9,21 +9,36 @@ import (
//"math/rand"
)
//-Utility: Return list of all yaml files-//
func getFileList() []string {
//----------------------------------------//
cmd := strings.Split("find . -name *.yaml"," ")
out_cmd, _ := exec.Command(cmd[0],cmd[1:]...).Output()
out_list := strings.Split(string(out_cmd),"\n")
return out_list[:len(out_list)-1]
}
//----------------------------------------//
func Test_makeRepo(t *testing.T) {
//----------------------------------------//
log.Printf("One day tests shall be here")
}
//----------------------------------------//
func Test_getField(t *testing.T) {
//----------------------------------------//
log.Printf("One day tests shall be here")
}
//----------------------------------------//
func Test_getRepoList(t *testing.T) {
//----------------------------------------//
files := getFileList()
log.Printf("The repository contains %d files",len(files))
in_args := []string{"cmd_line"}
var repos []string
var repos []Repo
getRepoList(".", &in_args,&repos)
t.Run(fmt.Sprintf("Size test for no arguments"), func(t *testing.T) {
......@@ -32,12 +47,15 @@ func Test_getRepoList(t *testing.T) {
}
})
for _, path := range files {
// We limit number of tests run for maximum argument count of 3
var limit int ; if (len(files) > 3) { limit = 3 } else { limit = len(files) }
for _, path := range files[:limit] {
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) {
t.Run(fmt.Sprintf("Size test for %d argument(s)",len(in_args)-1), 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)
}
......
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