Skip to content

Commit

Permalink
Merge pull request #7 from syberalexis/comments
Browse files Browse the repository at this point in the history
Add methods and structures code comments
  • Loading branch information
syberalexis authored Nov 12, 2019
2 parents 024f325 + c42ad81 commit cd7377a
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.com/syberalexis/automirror.svg?branch=master)][travis]
[![Go Report Card](https://goreportcard.com/badge/github.com/syberalexis/automirror)](https://goreportcard.com/report/github.com/syberalexis/automirror)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/3394/badge)](https://bestpractices.coreinfrastructure.org/projects/3394/badge)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/3394/badge)](https://bestpractices.coreinfrastructure.org/projects/3394)

This Project is a POC to learn Golang. It's used to mirror different repositories
in local for a disconnected environment or embedded systems.
Expand Down
2 changes: 1 addition & 1 deletion automirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func buildEngine(config configs.EngineConfig) interface{} {
case "deb":
engine, err = pullers.NewDeb(config)
case "docker":
engine, err = pullers.NewDocker(config)
engine, err = both.NewDocker(config)
case "git":
engine, err = both.NewGit(config)
case "mvn":
Expand Down
28 changes: 25 additions & 3 deletions pullers/Docker.go → both/Docker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pullers
package both

import (
"encoding/json"
Expand All @@ -12,17 +12,20 @@ import (
"strings"
)

// Docker object to pull and push Docker images with docker unix command
type Docker struct {
Source string
Destination string
AuthUri string `toml:"auth_uri"`
AuthURI string `toml:"auth_uri"`
Images []Image `toml:"images"`
}

// Image structure from specific configuration file
type Image struct {
Name string `toml:"name"`
}

// NewDocker method to construct Docker
func NewDocker(config configs.EngineConfig) (interface{}, error) {
var docker Docker
err := configs.Parse(&docker, config.Config)
Expand All @@ -32,6 +35,9 @@ func NewDocker(config configs.EngineConfig) (interface{}, error) {
return docker, nil
}

// Pull Docker images
// Inherits public method to launch pulling process
// Return number of downloaded artifacts and error
func (d Docker) Pull() (int, error) {
err := utils.Mkdir(d.Destination)
if err != nil {
Expand Down Expand Up @@ -72,10 +78,19 @@ func (d Docker) Pull() (int, error) {

return after - before, nil
}

// Push Docker images
// Inherits public method to launch pushing process
// Return error
func (d Docker) Push() error {
return nil
}

// private method to authenticate with anonymous on Docker Registry
func (d Docker) authenticate(image string) (authentication, error) {
var authentication authentication

resp, err := http.Get(strings.Join([]string{d.AuthUri, fmt.Sprintf("scope=repository:library/%s:pull", image)}, "&"))
resp, err := http.Get(strings.Join([]string{d.AuthURI, fmt.Sprintf("scope=repository:library/%s:pull", image)}, "&"))
if err != nil {
return authentication, err
}
Expand All @@ -90,6 +105,7 @@ func (d Docker) authenticate(image string) (authentication, error) {
return authentication, err
}

// private method to get Tags from a Docker image
func (d Docker) getTags(name string) (image, error) {
var image image

Expand All @@ -112,25 +128,31 @@ func (d Docker) getTags(name string) (image, error) {

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return image, err
}

err = json.Unmarshal(body, &image)
return image, err
}

// private method to run docker pull unix command
func (d Docker) pull(name string, tag string) error {
cmd := exec.Command("docker", "pull", fmt.Sprintf("%s:%s", name, tag))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

// private method to run docker save unix command
func (d Docker) save(name string, tag string) error {
cmd := exec.Command("docker", "save", name, "--output", fmt.Sprintf("%s/%s/%s", d.Destination, name, tag))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

// Structure returned by authentication request
type authentication struct {
Token string `json:"token"`
AccessToken string `json:"access_token"`
Expand Down
8 changes: 8 additions & 0 deletions both/Git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"strings"
)

// Git object to pull and push with git unix command
type Git struct {
Source string
Destination string
Options string
}

// NewGit method to construct Git
func NewGit(config configs.EngineConfig) (interface{}, error) {
var git Git
err := configs.Parse(&git, config.Config)
Expand All @@ -23,6 +25,9 @@ func NewGit(config configs.EngineConfig) (interface{}, error) {
return git, nil
}

// Pull pull a git repo
// Inherits public method to launch pulling process
// Return number of downloaded artifacts and error
func (g Git) Pull() (int, error) {
err := utils.Mkdir(g.Destination)
if err != nil {
Expand All @@ -47,6 +52,9 @@ func (g Git) Pull() (int, error) {
return after - before, nil
}

// Push a git repo
// Inherits public method to launch pushing process
// Return error
func (g Git) Push() error {
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions both/Rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"strings"
)

// Rsync object to pull and push with rsync unix command
type Rsync struct {
Source string
Destination string
Options string
}

// NewRsync method to construct Rsync
func NewRsync(config configs.EngineConfig) (interface{}, error) {
var rsync Rsync
err := configs.Parse(&rsync, config.Config)
Expand All @@ -23,6 +25,9 @@ func NewRsync(config configs.EngineConfig) (interface{}, error) {
return rsync, nil
}

// Pull a remote folder to a local
// Inherits public method to launch pulling process
// Return number of downloaded artifacts and error
func (r Rsync) Pull() (int, error) {
err := utils.Mkdir(r.Destination)
if err != nil {
Expand All @@ -47,6 +52,9 @@ func (r Rsync) Pull() (int, error) {
return after - before, nil
}

// Push a local folder to a remote
// Inherits public method to launch pushing process
// Return error
func (r Rsync) Push() error {
return r.synchronize()
}
Expand Down
1 change: 1 addition & 0 deletions configs/ConfigManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
)

// Parse method to read TOML file and convert it into a struct
func Parse(config interface{}, configFile string) error {
tomlFile, err := ioutil.ReadFile(configFile)
if err != nil {
Expand Down
15 changes: 3 additions & 12 deletions configs/TomlConfig.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
package configs

// TomlConfig structure from configuration file
type TomlConfig struct {
LogFile string `toml:"log_file"`
LogFormat string `toml:"log_format"`
LogLevel string `toml:"log_level"`
Mirrors map[string]MirrorConfig `toml:"mirrors"`
}

// MirrorConfig structure from configuration file
type MirrorConfig struct {
Timer string
Puller EngineConfig `toml:"puller"`
Pusher EngineConfig `toml:"pusher"`
}

type PullerConfig struct {
Name string
Source string
Destination string
Config string
}

type PusherConfig struct {
Name string
Config string
}

// EngineConfig structure from configuration file
type EngineConfig struct {
Name string
Config string
Expand Down
3 changes: 3 additions & 0 deletions mirrors/Mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
)

// Mirror structure to pull and push packages
type Mirror struct {
Name string
Puller pullers.Puller
Expand All @@ -15,6 +16,7 @@ type Mirror struct {
IsRunning bool
}

// Start method to initialize scheduler
func (m Mirror) Start() {
timer, _ := time.ParseDuration(m.Timer)
for {
Expand All @@ -27,6 +29,7 @@ func (m Mirror) Start() {
log.Infof("%s is stopped !", m.Name)
}

// Run method to pull and push if not already running
func (m Mirror) Run() {
if !m.IsRunning {
log.Infof("%s is running !", m.Name)
Expand Down
5 changes: 5 additions & 0 deletions pullers/Deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
)

// Deb object to pull a Debian based mirror with debmirror unix command
type Deb struct {
Host string
Destination string
Expand All @@ -24,6 +25,7 @@ type Deb struct {
Options string
}

// NewDeb method to construct Deb
func NewDeb(config configs.EngineConfig) (interface{}, error) {
var deb Deb
err := configs.Parse(&deb, config.Config)
Expand All @@ -33,6 +35,9 @@ func NewDeb(config configs.EngineConfig) (interface{}, error) {
return deb, nil
}

// Pull a Debian based repo
// Inherits public method to launch pulling process
// Return number of downloaded artifacts and error
func (d Deb) Pull() (int, error) {
err := utils.Mkdir(d.Destination)
if err != nil {
Expand Down
38 changes: 22 additions & 16 deletions pullers/Maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
)

// Object data structure
// Maven object to pull Java artifacts from a Maven repo with mvn unix config
type Maven struct {
Source string
Destination string
Expand All @@ -24,12 +24,14 @@ type Maven struct {
Artifacts []Artifact `toml:"artifact"`
}

// Artifact to pull
type Artifact struct {
Group string
Id string
ID string
MinimumVersion string `toml:"minimum_version"`
}

// NewMaven method to construct Maven
func NewMaven(config configs.EngineConfig) (interface{}, error) {
var maven Maven
err := configs.Parse(&maven, config.Config)
Expand All @@ -39,6 +41,7 @@ func NewMaven(config configs.EngineConfig) (interface{}, error) {
return maven, nil
}

// Pull artifacts from Maven repo
// Inherits public method to launch pulling process
// Return number of downloaded artifacts
func (m Maven) Pull() (int, error) {
Expand All @@ -47,20 +50,23 @@ func (m Maven) Pull() (int, error) {

for _, artifact := range m.Artifacts {
group := replacer.Replace(artifact.Group)
artifactId := replacer.Replace(artifact.Id)
metadata, err := m.readMetadata(group, artifactId)
artifactID := replacer.Replace(artifact.ID)
metadata, err := m.readMetadata(group, artifactID)
if err != nil {
return counter, err
}

if len(metadata.Versioning.Versions) != 0 {
for _, version := range metadata.Versioning.Versions[0].Versions {
isExistInDB, err := utils.ExistsInDatabase(m.DatabaseFile, fmt.Sprintf("%s.%s:%s", artifact.Group, artifact.Id, version))
isExistInDB, err := utils.ExistsInDatabase(m.DatabaseFile, fmt.Sprintf("%s.%s:%s", artifact.Group, artifact.ID, version))
if err != nil {
return counter, err
}
if strings.Compare(version, artifact.MinimumVersion) >= 0 && !isExistInDB {
err = m.downloadWithDependencies(artifact.Group, artifact.Id, version)
err = m.downloadWithDependencies(artifact.Group, artifact.ID, version)
if err != nil {
return counter, err
}
counter++
}
}
Expand All @@ -74,15 +80,15 @@ func (m Maven) Pull() (int, error) {
func (m Maven) createPOM(group string, artifact string, version string) error {
project := project{
ModelVersion: "4.0.0",
GroupId: "automirror",
ArtifactId: "automirror",
GroupID: "automirror",
ArtifactID: "automirror",
Version: "0.0.0",
Dependencies: []dependencies{
{
Dependencies: []dependency{
{
GroupId: group,
ArtifactId: artifact,
GroupID: group,
ArtifactID: artifact,
Version: version,
},
},
Expand Down Expand Up @@ -155,8 +161,8 @@ func (m Maven) readMetadata(group string, artifact string) (metadata, error) {
// Metadata Structure
type metadata struct {
XMLName xml.Name `xml:"metadata"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Versioning versioning `xml:"versioning"`
}

Expand All @@ -177,8 +183,8 @@ type versions struct {
type project struct {
XMLName xml.Name `xml:"project"`
ModelVersion string `xml:"modelVersion"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
Dependencies []dependencies `xml:"dependencies"`
}
Expand All @@ -190,7 +196,7 @@ type dependencies struct {

type dependency struct {
XMLName xml.Name `xml:"dependency"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
}
1 change: 1 addition & 0 deletions pullers/Puller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pullers

// Puller interface to expose methods for pulling processes
type Puller interface {
Pull() (int, error)
}
Loading

0 comments on commit cd7377a

Please sign in to comment.