Skip to content

Commit

Permalink
To make it easier to use, the initial template files are fetched from…
Browse files Browse the repository at this point in the history
… a pre-defined server

	modified:   cmd/rfd/internal/config/initialise.go
	modified:   cmd/rfd/internal/config/utilities.go
	new file:   cmd/rfd/internal/config/utilities_test.go
	deleted:    template/0001/installation.md
  • Loading branch information
redazzo committed Jun 23, 2023
1 parent 252ebd2 commit b7fab26
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 85 deletions.
123 changes: 56 additions & 67 deletions cmd/rfd/internal/config/initialise.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,60 @@ func InitialiseRepo() {

}

func colateInitialConfiguration() {

// Collect information from user on where the rfd repo will be created.
repositoryRoot, templatesDirectory, keyType, userName, organisation := getConfigurationInfoFromUser()

// Write the configuration file
writeConfigFile(repositoryRoot, templatesDirectory, keyType, userName, organisation)

// Configure the repository
Configure()

// Write the template directory
_, err := WriteTemplates()
if err != nil {
log.Fatal(err)
}

PostConfigure()
}

func create0001Rfd() {

var fileExists = Exists(GetRFDDirectory("0001") + PATH_SEPARATOR + "readme.md")

if fileExists {

response := GetUserInput("File exists. Overwrite (y/N)?")
response = strings.ToUpper(response)

switch response {
case "N":
printCancelled()

case "NO":
printCancelled()

case "Y":
initReadme()

case "YES":
initReadme()

default:
printCancelled()

}

} else {

initReadme()

}
}

func pushToOrigin(err error, repository *git.Repository) {
Logger.TraceLog("Pushing to origin ...")
err = PushToOrigin(repository)
Expand Down Expand Up @@ -178,20 +232,6 @@ func stage() (*git.Repository, *git.Worktree, error) {
return repository, worktree, err
}

func colateInitialConfiguration() {

// Collect information from user on where the rfd repo will be created.
repositoryRoot, templatesDirectory, keyType, userName, organisation := getConfigurationInfoFromUser()

// Write the configuration file
writeConfigFile(repositoryRoot, templatesDirectory, keyType, userName, organisation)

// Configure the repository
Configure()
PostConfigure()

}

func getConfigurationInfoFromUser() (string, string, string, string, string) {
// Default to the current directory.

Expand All @@ -209,24 +249,7 @@ func getConfigurationInfoFromUser() (string, string, string, string, string) {

fmt.Println("Using repository root: " + repositoryRoot)

// Get template directory from user
templatesDirectory := GetUserInput("Enter the path to the directory where the rfd templates are located (default: <current directory>/template):")
if templatesDirectory == "" {
// if the repository root is empty, then use the working directory
templatesDirectory, _ = os.Getwd()
templatesDirectory = templatesDirectory + PATH_SEPARATOR + "template"
}

//templatesDirectory := repositoryRoot + PATH_SEPARATOR + "template"

//Logger.TraceLog("Fetching templates and copying to " + templatesDirectory)
//FetchTemplateDirectory()

// Check to see if the directory exists,and if not, exit.
if !Exists(templatesDirectory) {
Logger.TraceLog("The directory " + templatesDirectory + " does not exist.")
os.Exit(1)
}
templatesDirectory := repositoryRoot + PATH_SEPARATOR + "template"

fmt.Println("Using templates directory: " + templatesDirectory)

Expand Down Expand Up @@ -294,7 +317,7 @@ func writeConfigFile(repositoryRoot string, templatesDirectory string, keyType s
yamlData, err := yaml.Marshal(APP_CONFIG)

if err != nil {
log.Fatal("Error while Marshaling. %v", err)
log.Fatalf("Error while Marshaling. %v", err)
}

fileName := "config.yml"
Expand All @@ -304,40 +327,6 @@ func writeConfigFile(repositoryRoot string, templatesDirectory string, keyType s
}
}

func create0001Rfd() {

var fileExists = Exists(GetRFDDirectory("0001") + PATH_SEPARATOR + "readme.md")

if fileExists {

response := GetUserInput("File exists. Overwrite (y/N)?")
response = strings.ToUpper(response)

switch response {
case "N":
printCancelled()

case "NO":
printCancelled()

case "Y":
initReadme()

case "YES":
initReadme()

default:
printCancelled()

}

} else {

initReadme()

}
}

func initReadme() {

formattedRFDNumber := "0001"
Expand Down
79 changes: 62 additions & 17 deletions cmd/rfd/internal/config/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"bufio"
"fmt"
"github.com/go-git/go-git/v5"
"io"
"net/http"
"path/filepath"

//"github.com/redazzo/rfd/cmd/rfd/internal/config"
"io/ioutil"
"log"
Expand Down Expand Up @@ -117,29 +121,70 @@ func GetRFDDirectory(sRfdNumber string) string {
return APP_CONFIG.RootDirectory + "/" + sRfdNumber
}

func FetchTemplateDirectory() {
func fetchFileFromURL(url string) ([]byte, error) {

// Fetch the template directory from the remote repo
// and copy it to the local RFD root directory.
// Make an HTTP GET request
response, err := http.Get(url)
if err != nil {
fmt.Printf("Error while fetching the URL: %s", err)
return nil, err
}
defer response.Body.Close()

// Read the response body
body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Printf("Error while reading the response body: %s", err)
return nil, err
}

// This is a one-time operation, so we don't need to
// check if the directory already exists.
return body, nil

repoURL := "https://github.com/redazzo/rfd"
targetDirectory := APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template"
}

// Clone the repo to the target directory
_, err := git.PlainClone(targetDirectory, false, &git.CloneOptions{
URL: repoURL,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
})
func WriteTemplates() (string, error) {

if err != nil {
fmt.Printf("Failed to clone repository: %v\n", err)
return
type FileToWrite struct {
URL string
Destination string
}

targetDirectory := APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR

filesToWrite := []FileToWrite{
{
URL: "https://sea-turtle-app-ufxrk.ondigitalocean.app/readme.md",
Destination: targetDirectory + "readme.md",
},
{
URL: "https://sea-turtle-app-ufxrk.ondigitalocean.app/states.yml",
Destination: targetDirectory + "states.yml",
},
{
URL: "https://sea-turtle-app-ufxrk.ondigitalocean.app/0001/readme.md",
Destination: targetDirectory + "0001" + PATH_SEPARATOR + "readme.md",
},
}

for _, file := range filesToWrite {
fileContent, err := fetchFileFromURL(file.URL)
if err != nil {
return targetDirectory, err
}

err = os.MkdirAll(filepath.Dir(file.Destination), 0744)
if err != nil {
return targetDirectory, err
}

err = os.WriteFile(file.Destination, fileContent, 0744)
if err != nil {
return targetDirectory, err
}

log.Printf("Wrote %s template to %s ...\n", filepath.Base(file.URL), filepath.Dir(file.Destination))
}

// Repository cloned successfully
fmt.Println("Repository cloned successfully.")
return targetDirectory, nil

}
73 changes: 73 additions & 0 deletions cmd/rfd/internal/config/utilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package config

import (
"log"
"os"
"testing"
)

func TestWriteTemplates(t *testing.T) {

APP_CONFIG = &Configuration{}

APP_CONFIG.RootDirectory = "/tmp"

err := WriteTemplates()
if err != nil {
t.Errorf("Error writing templates: %s", err)
}

// Test for existence of readme.md
_, err = os.Stat(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "readme.md")
if err != nil {
t.Errorf("Error reading readme.md: %s", err)
}
log.Println("Readme.md template present in root directory ...")

// Test for existence of states.yml
_, err = os.Stat(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "states.yml")
if err != nil {
t.Errorf("Error reading states.yml: %s", err)
}
log.Println("States.yml template present in root directory ...")

// Test for existence of 0001 directory
_, err = os.Stat(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "0001")
if err != nil {
t.Errorf("Error reading 0001 directory: %s", err)
}
log.Println("0001 directory present in root directory ...")

// Test for existence of readme.md
_, err = os.Stat(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "0001" + PATH_SEPARATOR + "readme.md")
if err != nil {
t.Errorf("Error reading readme.md: %s", err)
}
log.Println("Readme.md template present in 0001 directory ...")

// Clean up
log.Println("Cleaning up ...")

// We only have rights to remove the files we created
if err := os.Remove(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "readme.md"); err != nil {
t.Errorf("Error removing readme.md: %s", err)
}
log.Println("Removed readme.md ...")

if err := os.Remove(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "states.yml"); err != nil {
t.Errorf("Error removing states.yml: %s", err)
}
log.Println("Removed states.yml ...")

if err := os.Remove(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "0001" + PATH_SEPARATOR + "readme.md"); err != nil {
t.Errorf("Error removing readme.md: %s", err)
}
log.Println("Removed readme.md from 0001 directory ...")

if err := os.RemoveAll(APP_CONFIG.RootDirectory + PATH_SEPARATOR + "template" + PATH_SEPARATOR + "0001"); err != nil {
t.Errorf("Error removing 0001 directory contents: %s", err)
}

log.Println("Cleaned up ...")

}
1 change: 0 additions & 1 deletion template/0001/installation.md

This file was deleted.

0 comments on commit b7fab26

Please sign in to comment.