Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
automated readme generation
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickLaabs committed Feb 25, 2024
1 parent 65a4795 commit 3c6ca90
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 63 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/readme-workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: update

on:
push:
branches:
- main
schedule:
- cron: '0 11 * * *'

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: 🍽️ Get working copy
uses: actions/checkout@main
with:
fetch-depth: 1
- name: 🍳 Shake & bake README
run:
go run docs/docsgenerator.go
- name: 🚀 Deploy
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git add .
git commit -am "Update dynamic content"
git push --all -f https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git
22 changes: 11 additions & 11 deletions cmd/frigg/bootstrap/capd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {

func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {

// Get GITHUB_TOKEN environment var
// Will exit, if the Token is not set, since we need this Token for further configurations
// Like deploying the token as a kubernetes secret on your clusters, creating repositories.
// docs:Get GITHUB_TOKEN environment var
// docs:Will exit, if the Token is not set, since we need this Token for further configurations
// docs:Like deploying the token as a kubernetes secret on your clusters, creating repositories.
if os.Getenv("GITHUB_TOKEN") == "" {
fmt.Println("Missing Github Token, please set it. Exiting now.")
os.Exit(1)
Expand All @@ -108,7 +108,7 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {

// Create working directory named .frigg inside the users homedirectory.
workdir.CreateDir()

provider := cluster.NewProvider(
cluster.ProviderWithLogger(logger),
runtime.GetDefault(logger),
Expand All @@ -134,7 +134,7 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {
return errors.Wrap(err, "failed to create cluster")
}

// Installs capi components on the bootstrap cluster.
// Installs capi components on the bootstrap cluster.
// clustername is bootstrapcluster
wait.Wait(10 * time.Second)
clusterapi.ClusterAPI()
Expand Down Expand Up @@ -164,7 +164,7 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {
kubeconfig.RetrieveMgmtKubeconfig()

// Modifes the kubeconfig, to let us interact with the newly created kubernetes cluster.
// On MacOS, there is an issue, where you need to replace ip and the port address, in order to
// On MacOS, there is an issue, where you need to replace ip and the port address, in order to
// successfully connect to the cluster.
// ToDo:
// We shall check, if the user is running on macOS, Linux and/or Windows.
Expand All @@ -175,14 +175,14 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {
return err
}

// Applies the Github Token and the default ArgoCD Login Credentials as a
// Applies the Github Token and the default ArgoCD Login Credentials as a
// kubernetes secret on the argo namespace.
// This is needed to let us interact with github, to clone, refactor and push the needed
// gitops repositories.
//
// On the deployment, of new workload kubernetes clusters - which will be attached to the management
// cluster - we run ArgoCD Workflows, which will create a pod, which runs a script.
// This script logs in to the argocd instance, and adds the new kubernetes cluster to it, and
// cluster - we run ArgoCD Workflows, which will create a pod, which runs a script.
// This script logs in to the argocd instance, and adds the new kubernetes cluster to it, and
// also adds a label to the cluster, with which we can proceed the automation steps.
wait.Wait(5 * time.Second)
// Github Token Secret deployment
Expand Down Expand Up @@ -211,11 +211,11 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole) error {
// Modifies the manifest of the workload cluster, to add the helmchart labels to it
// ToDo
// This is still in development process, hence the fact that modification on YAMLs are not that easy..

// Modifies the generated manifest with the needed helmchartproxy labels
// This step can we "on hold", since we directly write a yaml file from the templates directory, to the
// .frigg working directory.

// Applies the workload cluster manifest to the frigg-mgmt-cluster
wait.Wait(5 * time.Second)
clusterapi.KubectlApplyWorkload()
Expand Down
17 changes: 16 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# ArgoHub
# Frigg documentation

## Content

- ./frigg
- ./providers
- ./capd
- ./capv
- ./capz
- ./harvester

## How we do documentation

The documentation is mostly done by writing comments in the code.
The 'docsgenerator.go' program tries to read them, and writes it to the desired places
inside the Markdown-files.
9 changes: 0 additions & 9 deletions docs/capd/capd_docs.go

This file was deleted.

52 changes: 43 additions & 9 deletions docs/docsgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ package main

import (
"fmt"
"github.com/PatrickLaabs/frigg/docs/capd"
"github.com/PatrickLaabs/frigg/docs/capv"
"github.com/PatrickLaabs/frigg/docs/capz"
"github.com/PatrickLaabs/frigg/docs/harvester"
"github.com/PatrickLaabs/frigg/docs/providers/capd"
"os"
"strings"
)

func CreateDocs() {
fmt.Println("Scraping Go Files and creating docs out of it..")
capd.Docsgenerator()
capv.Docsgenerator()
capz.Docsgenerator()
harvester.Docsgenerator()
capd.MakeReadme("docs/providers/capd/README.md")

//fmt.Println("Scraping Go Files and creating docs out of it..")
//_, err := capd.Docsgenerator("../cmd/frigg/bootstrap/capd/cluster/cluster.go")
//if err != nil {
// return
//}

//capv.Docsgenerator()
//capz.Docsgenerator()
//harvester.Docsgenerator()
}

func main() {
Expand All @@ -24,3 +29,32 @@ func main() {
func TestFunc() {
fmt.Println("A Testfile with no meaning, beside to feed the docsgenerator")
}

func ReadComments(filePath string) ([]string, error) {
// Use the os.ReadFile function to read the file contents
fileBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

// Convert file contents to string
fileContent := string(fileBytes)

// Initialize empty slice to store comments
var comments []string

// Iterate over each line in the file content
for _, line := range strings.Split(fileContent, "\n") {
// Check for single-line comments starting with "//"
if strings.HasPrefix(line, "//") {
comments = append(comments, strings.TrimSpace(line[2:]))
}

// Check for multi-line comments starting with "/*` and ending with `*/"
if strings.HasPrefix(line, "/*") && strings.HasSuffix(line, "*/") {
comments = append(comments, strings.TrimSpace(strings.TrimPrefix(strings.TrimSuffix(line, "*/"), "/*")))
}
}

return comments, nil
}
36 changes: 3 additions & 33 deletions docs/docsgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,16 @@ package main

import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)

// TestReadComments tries to test the readability of the comments inside the code.
// It tries to call the function 'ReadComments' from the docsgenerator.go file.
func TestReadComments(t *testing.T) {
// Replace with your actual Go file path
filePath := "docsgenerator.go"
comments, err := ReadComments(filePath)
if err != nil {
t.Errorf("Error reading comments: %v", err)
t.Errorf("Error reading comments: %q", err)
}
assert.Contains(t, comments, "TestFunc func prints something to stdout. It has no meaning, and is only used for testing the docsgenerator package.")
}

func ReadComments(filePath string) ([]string, error) {
// Use the os.ReadFile function to read the file contents
fileBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

// Convert file contents to string
fileContent := string(fileBytes)

// Initialize empty slice to store comments
comments := []string{}

// Iterate over each line in the file content
for _, line := range strings.Split(fileContent, "\n") {
// Check for single-line comments starting with "//"
if strings.HasPrefix(line, "//") {
comments = append(comments, strings.TrimSpace(line[2:]))
}

// Check for multi-line comments starting with "/*` and ending with `*/"
if strings.HasPrefix(line, "/*") && strings.HasSuffix(line, "*/") {
comments = append(comments, strings.TrimSpace(strings.TrimPrefix(strings.TrimSuffix(line, "*/"), "/*")))
}
}

return comments, nil
}
File renamed without changes.
Empty file removed docs/harvester/README.md
Empty file.
6 changes: 6 additions & 0 deletions docs/providers/capd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CAPD (ClusterAPI Provider for Docker) Documentation


Placeholder

25 Feb 2024
88 changes: 88 additions & 0 deletions docs/providers/capd/capd_docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package capd

import (
"fmt"
"io"
"os"
"strings"
"time"
)

func MakeReadme(filename string) {
date := time.Now().Format("2 Jan 2006")

header := "# CAPD (ClusterAPI Provider for Docker) Documentation"
body := "## Placeholder"
footer := date
data := fmt.Sprintf("%s\n\n\n%s\n\n%s", header, body, footer)

file, err := os.Create(filename)
if err != nil {
fmt.Printf("Error on creating file: %v\n", err)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Printf("Error on closing file: %v\n", err)
}
}(file)

_, err = io.WriteString(file, data)
if err != nil {
fmt.Printf("Error writing README file: %v\n", err)
}
}

// Docsgenerator for the capd provider usage
func Docsgenerator(filepath string) ([]string, error) {
//filepath = "cmd/frigg/bootstrap/capd/cluster/cluster.go"
fileBytes, err := os.ReadFile(filepath)
if err != nil {
fmt.Printf("Error reading file from given path: %v", err)
}

fileContent := string(fileBytes)

var comments []string

//regex := regexp.MustCompile(`// docs:`)
//
//for _, line := range strings.Split(fileContent, "\n") {
// if match := regex.FindStringSubmatch(line); match != nil {
// // Extract and trim the comment text after "// docs:"
// comment := strings.TrimSpace(match[1])
// comments = append(comments, comment)
// }
//}

for _, line := range strings.Split(fileContent, "\n") {
line = strings.TrimLeft(line, " \t")
if strings.HasPrefix(line, "//") || (strings.HasPrefix(line, "/*") && strings.HasSuffix(line, "*/")) {
comments = append(comments, strings.TrimSpace(line[2:]))
}
}

//for _, line := range strings.Split(fileContent, "\n") {
// if strings.HasPrefix(line, "//") {
// comments = append(comments, strings.TrimSpace(line[2:]))
// }
//
// if strings.HasPrefix(line, "/*") && strings.HasSuffix(line, "*/") {
// comments = append(comments, strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(line, "*/"), "/*")))
// }
//}

var combinedComments string
for _, comment := range comments {
combinedComments += comment + "\n"
}

err = os.WriteFile("docs/providers/capd/README.md", []byte(combinedComments), 0755)
if err != nil {
return nil, err
}

fmt.Println(combinedComments)

return comments, nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/pborman/uuid v1.2.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/sys v0.16.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 3c6ca90

Please sign in to comment.