-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automate README example generation #289
Merged
+419
−162
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2c3399c
add readme
korotkov-aerospike 989619f
add readme
korotkov-aerospike 8ebfe02
working example with cluster response
korotkov-aerospike b202cca
routines
korotkov-aerospike 6b310a3
storage
korotkov-aerospike def3b6b
backup list
korotkov-aerospike af5551b
restore request
korotkov-aerospike 7f3c353
restore timestamp request
korotkov-aerospike ca0af69
update test
korotkov-aerospike 526b4b5
linter warning
korotkov-aerospike dcb9947
Merge branch 'v2' into readme
korotkov-aerospike cdc4318
Merge branch 'v2' into readme
korotkov-aerospike c7eed49
regenerate
korotkov-aerospike 81709ce
add comment
korotkov-aerospike 9af21c8
add yaml
korotkov-aerospike 26319bc
extracr common structs
korotkov-aerospike f62ac9c
relocate script
korotkov-aerospike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/aerospike/aerospike-backup-service/v2/pkg/dto" | ||
"github.com/aerospike/aerospike-backup-service/v2/pkg/util" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var allStorageTypes = map[string]dto.Storage{ | ||
"local": { | ||
LocalStorage: &dto.LocalStorage{ | ||
Path: "backups", | ||
}, | ||
}, | ||
"aws-s3": { | ||
S3Storage: &dto.S3Storage{ | ||
Bucket: "as-backup-bucket", | ||
Path: "backups", | ||
S3Region: "eu-central-1", | ||
}, | ||
}, | ||
"gcp-gcs": { | ||
GcpStorage: &dto.GcpStorage{ | ||
Path: "backups", | ||
KeyFile: "key-file.json", | ||
BucketName: "gcp-backup-bucket", | ||
Endpoint: "http://127.0.0.1:9020", | ||
}, | ||
}, | ||
"azure-blob-storage": { | ||
AzureStorage: &dto.AzureStorage{ | ||
Path: "backups", | ||
Endpoint: "http://127.0.0.1:6000/devstoreaccount1", | ||
AccountName: "devstoreaccount1", | ||
ContainerName: "testcontainer", | ||
AccountKey: "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", | ||
}, | ||
}, | ||
} | ||
|
||
var cluster = dto.AerospikeCluster{ | ||
SeedNodes: []dto.SeedNode{{ | ||
HostName: "host.docker.internal", Port: 3000}, | ||
}, | ||
Credentials: &dto.Credentials{ | ||
User: util.Ptr("user"), | ||
Password: util.Ptr("password"), | ||
}, | ||
} | ||
|
||
var jsonExamples = map[string]any{ | ||
"ClustersResponse": []dto.AerospikeCluster{cluster}, | ||
"RoutinesResponse": map[string]dto.BackupRoutine{ | ||
"routine1": { | ||
BackupPolicy: "keepFilesPolicy", | ||
SourceCluster: "absDefaultCluster", | ||
Storage: "local", | ||
IntervalCron: "@yearly", | ||
Namespaces: []string{"test-namespace"}, | ||
}, | ||
"routine2": { | ||
BackupPolicy: "removeFilesPolicy", | ||
SourceCluster: "absDefaultCluster", | ||
Storage: "local", | ||
IntervalCron: "@monthly", | ||
IncrIntervalCron: "@daily", | ||
Namespaces: []string{"test-namespace"}, | ||
SetList: []string{"backupSet"}, | ||
BinList: []string{"backupBin"}, | ||
}, | ||
}, | ||
"StorageResponse": allStorageTypes, | ||
"FullBackupsResponse": map[string][]dto.BackupDetails{ | ||
"routine1": {{ | ||
BackupMetadata: dto.BackupMetadata{ | ||
Created: time.Date(2024, 01, 01, 12, 0, 0, 0, time.UTC), | ||
Namespace: "source-ns1", | ||
ByteCount: 480_000, | ||
FileCount: 1, | ||
UDFCount: 1, | ||
RecordCount: 42, | ||
SecondaryIndexCount: 5, | ||
}, | ||
Key: "routine1/backup/1704110400000/source-ns1", | ||
Storage: &dto.Storage{ | ||
S3Storage: &dto.S3Storage{ | ||
Bucket: "as-backup-bucket", | ||
Path: "backups", | ||
S3Region: "eu-central-1", | ||
}, | ||
}}, | ||
}, | ||
"routine2": {{ | ||
BackupMetadata: dto.BackupMetadata{ | ||
Created: time.Date(2024, 01, 01, 12, 0, 0, 0, time.UTC), | ||
Namespace: "source-ns2", | ||
ByteCount: 1_234_567_890, | ||
RecordCount: 1890, | ||
FileCount: 4, | ||
}, | ||
Key: "routine2/backup/1704110400000/source-ns2", | ||
Storage: &dto.Storage{ | ||
S3Storage: &dto.S3Storage{ | ||
Bucket: "as-backup-bucket", | ||
Path: "backups", | ||
S3Region: "eu-central-1", | ||
}, | ||
}, | ||
}}, | ||
}, | ||
"RestoreFullRequest": dto.RestoreRequest{ | ||
DestinationCluster: &cluster, | ||
Policy: &dto.RestorePolicy{ | ||
NoGeneration: util.Ptr(true), | ||
}, | ||
SourceStorage: &dto.Storage{ | ||
S3Storage: &dto.S3Storage{ | ||
Bucket: "as-backup-bucket", | ||
Path: "backups", | ||
S3Region: "eu-central-1", | ||
}, | ||
}, | ||
BackupDataPath: "routine1/backup/1704110400000/source-ns1", | ||
}, | ||
"RestoreTimestampRequest": dto.RestoreTimestampRequest{ | ||
DestinationCluster: &cluster, | ||
Policy: &dto.RestorePolicy{ | ||
NoGeneration: util.Ptr(true), | ||
}, | ||
Time: 1704110400000, | ||
Routine: "routine1", | ||
}, | ||
} | ||
|
||
var yamlExamples = map[string]any{ | ||
"Storage": allStorageTypes, | ||
} | ||
|
||
func main() { | ||
_ = dto.AerospikeCluster{} | ||
readme, err := os.ReadFile("README.md") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// comment containing an example name (e.g.,key from jsonExamples) | ||
// followed by ```json/```yaml and the example code block. | ||
re := regexp.MustCompile("<!--\\s*(\\w+)\\s*-->\\s*```(json|yaml)[\\s\\S]*?```") | ||
|
||
updatedReadme := re.ReplaceAllFunc(readme, func(match []byte) []byte { | ||
submatches := re.FindSubmatch(match) | ||
if len(submatches) < 3 { | ||
return match | ||
} | ||
|
||
name := string(submatches[1]) | ||
format := string(submatches[2]) | ||
|
||
var example any | ||
var exists bool | ||
var formattedExample []byte | ||
|
||
if format == "json" { | ||
example, exists = jsonExamples[name] | ||
if exists { | ||
formattedExample, err = json.MarshalIndent(example, "", " ") | ||
} | ||
} else if format == "yaml" { | ||
example, exists = yamlExamples[name] | ||
if exists { | ||
formattedExample, err = yaml.Marshal(example) | ||
} | ||
} | ||
|
||
if !exists || err != nil { | ||
return match | ||
} | ||
|
||
var buffer bytes.Buffer | ||
buffer.WriteString(fmt.Sprintf("<!-- %s -->\n```%s\n", name, format)) | ||
buffer.Write(formattedExample) | ||
buffer.WriteString("\n```") | ||
return buffer.Bytes() | ||
}) | ||
err = os.WriteFile("README.md", updatedReadme, 0600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relocate the script under
build/readme
.