Skip to content

Commit

Permalink
Makes custom manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaize Kaye committed Aug 25, 2024
1 parent fc4c3d1 commit 2470c60
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
21 changes: 17 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ import (
)

var targetDirectory string
var localManifest string
var scaffold string
var noInteraction bool

func getScaffoldsKeys() []string {
scaffolds, _ := internal.GetScaffolds(localManifest)
var ret []string
for k := range internal.GetScaffolds() {
for k := range scaffolds {
ret = append(ret, k)
}
sort.Strings(ret)
return ret
}

func selectScaffold(scaffold *string) error {
scaffolds, err := internal.GetScaffolds(localManifest)
if err != nil {
return err
}
prompt := survey.Select{
Message: "Select a scaffold to run",
Options: getScaffoldsKeys(),
Description: func(value string, index int) string {
return internal.GetScaffolds()[value].ShortDescription
return scaffolds[value].ShortDescription
},
}

Expand All @@ -53,7 +59,12 @@ var RootCmd = &cobra.Command{
Long: `Lagoon scaffold will pull a new site and fill in the details`,
RunE: func(cmd *cobra.Command, args []string) error {

scaffolds := internal.GetScaffolds()
scaffolds, err := internal.GetScaffolds(localManifest)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

if scaffold == "" && noInteraction {
return errors.New("Please select a scaffold")
Expand Down Expand Up @@ -225,8 +236,9 @@ var listCmd = &cobra.Command{
Long: "Lists all currently supported Lagoon scaffolds",
Example: "lagoon-init-prot list",
Run: func(cmd *cobra.Command, args []string) {
scaffolds, _ := internal.GetScaffolds(localManifest)
fmt.Println("We currently support the following:")
for pagage := range internal.GetScaffolds() {
for pagage := range scaffolds {
fmt.Println(pagage)
}
},
Expand All @@ -237,6 +249,7 @@ func init() {
RootCmd.PersistentFlags().StringVar(&scaffold, "scaffold", "", "Which scaffold to pull into directory")
RootCmd.Flags().BoolVar(&noInteraction, "no-interaction", false, "Don't interactively fill in any values for the scaffold - use defaults")
RootCmd.Flags().StringVar(&targetDirectory, "targetdir", "./", "Directory to check out project into - defaults to current directory")
RootCmd.Flags().StringVar(&localManifest, "manifest", "", "Custom local manifest file for scaffold list - defaults to an empty string")
}

func Execute() {
Expand Down
22 changes: 20 additions & 2 deletions internal/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"os"
)

const manifestUrl = "https://raw.githubusercontent.com/uselagoon/lagoon-scaffold/main/internal/assets/scaffolds.yml"
Expand Down Expand Up @@ -76,8 +77,25 @@ func remapScaffoldLoader(loader *ScaffoldLoader) map[string]ScaffoldRepo {
return remapped
}

func GetScaffolds() map[string]ScaffoldRepo {
return resolveScaffolds(manifestUrl)
func GetScaffolds(localmanifest string) (map[string]ScaffoldRepo, error) {

if localmanifest != "" { // we try load up a manifest given locally
// try open and read the file

loader := &ScaffoldLoader{
Scaffolds: make([]ScaffoldRepo, 0),
}
dat, err := os.ReadFile(localmanifest)
err = yaml.Unmarshal(dat, loader)
if err != nil {
return map[string]ScaffoldRepo{}, err
}

return remapScaffoldLoader(loader), nil

}

return resolveScaffolds(manifestUrl), nil
}

type ScaffoldRepo struct {
Expand Down
40 changes: 40 additions & 0 deletions internal/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,43 @@ func Test_resolveScaffolds(t *testing.T) {
})
}
}

func TestGetScaffolds(t *testing.T) {
type args struct {
localmanifest string
}
tests := []struct {
name string
args args
want map[string]ScaffoldRepo
wantErr bool
}{
{
name: "Test 1 - Passing manifest file manually",
args: args{
localmanifest: "./testassets/manifest_test_1.yml",
},
want: map[string]ScaffoldRepo{
"test1": {
Name: "test1",
GitRepo: "https://github.com/lagoon-examples/test1.git",
Branch: "test1_branch",
Description: "test1_description",
ShortDescription: "test1_shortDescription",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetScaffolds(tt.args.localmanifest)
if (err != nil) != tt.wantErr {
t.Errorf("GetScaffolds() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetScaffolds() got = %v, want %v", got, tt.want)
}
})
}
}
6 changes: 6 additions & 0 deletions internal/testassets/manifest_test_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scaffolds:
- name: test1
git_repo: https://github.com/lagoon-examples/test1.git
branch: test1_branch
description: test1_description
shortDescription: test1_shortDescription

0 comments on commit 2470c60

Please sign in to comment.