diff --git a/go.mod b/go.mod index 6018575..9b9b224 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,10 @@ module github.com/frontierdigital/ranger -go 1.20 +go 1.21 require ( - github.com/frontierdigital/utils v0.0.8 + github.com/frontierdigital/utils v0.0.8-1 github.com/google/uuid v1.1.2 - github.com/libgit2/git2go/v34 v34.0.0 github.com/otiai10/copy v1.9.0 github.com/segmentio/ksuid v1.0.4 github.com/spf13/cobra v1.6.1 @@ -32,7 +31,6 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.1 // indirect - golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect golang.org/x/sys v0.3.0 // indirect golang.org/x/text v0.4.0 // indirect diff --git a/go.sum b/go.sum index 13d2a55..8f74ce6 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,9 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= -github.com/frontierdigital/utils v0.0.8 h1:zIvqu8KLX8yfzEu0qEaAO9+/cu7Da5p0zZdhcG21Ze4= -github.com/frontierdigital/utils v0.0.8/go.mod h1:iz+lbB9iGA9ZsVbber/tPosKC9HFTgKTDP8/UC6XxBY= +github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= +github.com/frontierdigital/utils v0.0.8-1 h1:J4cYMjEWq/XMewPfSBDtGb7+DUnpj/gKvjd/yts0AtQ= +github.com/frontierdigital/utils v0.0.8-1/go.mod h1:iz+lbB9iGA9ZsVbber/tPosKC9HFTgKTDP8/UC6XxBY= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= @@ -130,6 +131,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -144,8 +146,6 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -177,11 +177,11 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/libgit2/git2go/v34 v34.0.0 h1:UKoUaKLmiCRbOCD3PtUi2hD6hESSXzME/9OUZrGcgu8= -github.com/libgit2/git2go/v34 v34.0.0/go.mod h1:blVco2jDAw6YTXkErMMqzHLcAjKkwF0aWIRHBqiJkZ0= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -230,6 +230,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c= @@ -266,6 +267,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs= github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -297,11 +299,8 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= -golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -405,7 +404,6 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190812172437-4e8604ab3aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -426,7 +424,6 @@ golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -438,8 +435,6 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/pkg/cmd/app/generate/docs.go b/pkg/cmd/app/generate/docs.go index 8a3a3d5..aad5d52 100644 --- a/pkg/cmd/app/generate/docs.go +++ b/pkg/cmd/app/generate/docs.go @@ -1,23 +1,26 @@ package generate import ( - _ "embed" + "embed" "errors" "fmt" + "io/fs" "os" + "path" "path/filepath" + "slices" + "strings" "text/template" "github.com/frontierdigital/ranger/pkg/core" - rfile "github.com/frontierdigital/ranger/pkg/util/file" rtime "github.com/frontierdigital/ranger/pkg/util/time" igit "github.com/frontierdigital/utils/git" git "github.com/frontierdigital/utils/git/external_git" "github.com/frontierdigital/utils/output" ) -//go:embed tpl/workload.tpl -var workloadTemplate string +//go:embed tpl/* +var wikiTemplates embed.FS type Workload struct { Name string @@ -70,56 +73,75 @@ func publish(ado *core.AzureDevOps, repoName string, repo interface{ igit.Git }) if err != nil { return err } + } else { + output.PrintlnfInfo("No changes for wiki") } return nil } -func createWorkLoadPages(workloads *[]core.Workload, localPath string) error { - orderPath := filepath.Join(localPath, "workloads", ".order") - - err := rfile.Clear(orderPath) - if err != nil { - return errors.New("could not reset order file") - } - - for _, w := range *workloads { - fullPath := filepath.Join(localPath, "workloads", fmt.Sprintf("%s.md", w.Name)) - - err := rfile.CreateOrUpdate(fullPath, w.Readme, false) - if err != nil { - return errors.New("could not create or update page") - } - - err = rfile.CreateOrUpdate(orderPath, fmt.Sprintln(w.Name), true) - if err != nil { - return errors.New("could not create or update orderfile") - } - } - - return nil +type WikiContent struct { + Sets []core.SetCollection + Workloads []core.Workload } -func createWorkloadIndex(workloads *[]core.Workload, localPath string) error { - wl := core.WorkloadIndex{ - Workloads: *workloads, - } - tmpl, err := template.New("workloadTemplate").Parse(workloadTemplate) +func processTemplateFile(src string, tgt string, localPath string, wikiContent interface{}) error { + tmpl, err := template.New(path.Base(src)).ParseFS(wikiTemplates, src) if err != nil { return err } - - file, err := os.Create(filepath.Join(localPath, "workloads.md")) + var f *os.File + err = os.MkdirAll(filepath.Dir(filepath.Join(localPath, tgt)), 0700) if err != nil { - return err + panic(err) + } + f, err = os.Create(filepath.Join(localPath, tgt)) + if err != nil { + panic(err) } - defer file.Close() + defer f.Close() - err = tmpl.Execute(file, wl) + err = tmpl.Execute(f, wikiContent) if err != nil { - return err + panic(err) } + return nil +} + +func writeWiki(wikiContent *WikiContent, localPath string) error { + loopDirs := []string{"tpl/sets", "tpl/workloads"} + fs.WalkDir(wikiTemplates, "tpl", func(src string, d fs.DirEntry, err error) error { + if slices.Contains(loopDirs, filepath.Dir(src)) { + if strings.Contains(src, "workloads") { + for _, w := range wikiContent.Workloads { + tgt := "workloads/" + w.Name + ".md" + terr := processTemplateFile(src, tgt, localPath, &w) + if terr != nil { + return nil + } + } + } + if strings.Contains(src, "sets") { + for _, s := range wikiContent.Sets { + tgt := "sets/" + s.Name + ".md" + terr := processTemplateFile(src, tgt, localPath, &s) + if terr != nil { + return nil + } + } + } + } + if !d.IsDir() && !slices.Contains(loopDirs, filepath.Dir(src)) { + tgt := strings.Replace(src, "tpl"+string(os.PathSeparator), "", 1) + tgt = strings.ReplaceAll(tgt, ".tpl", "") + terr := processTemplateFile(src, tgt, localPath, wikiContent) + if terr != nil { + return terr + } + } + return nil + }) return nil } @@ -130,42 +152,44 @@ func GenerateDocs(config *core.Config, projectName string, organisationName stri PAT: config.ADO.PAT, WorkloadFeedName: feedName, } - - err := ado.CreateWikiIfNotExists(wikiName, config.Git.UserName, config.Git.UserEmail) + sets, err := ado.GetSets() if err != nil { return err } + output.PrintlnfInfo("Fetched set info from project '%s/%s'", organisationName, projectName) - output.PrintlnfInfo("Created wiki '%s' (%s)", wikiName, ado.WikiRemoteUrl) - - wikiRepo, err := git.NewClonedGit(ado.WikiRepoRemoteUrl, "x-oauth-basic", config.ADO.PAT, config.Git.UserEmail, config.Git.UserName) + workloads, err := ado.GetWorkloadInfo() if err != nil { return err } - defer os.RemoveAll(wikiRepo.GetRepositoryPath()) + output.PrintlnfInfo("Fetched workload info from feed '%s' (https://dev.azure.com/%s/%s/_artifacts/feed/%s)", feedName, organisationName, projectName, feedName) - workloads, err := ado.GetWorkloadInfo() + wc := WikiContent{ + Workloads: *workloads, + Sets: *sets, + } + + err = ado.CreateWikiIfNotExists(wikiName, config.Git.UserName, config.Git.UserEmail) if err != nil { return err } - output.PrintlnfInfo("Fetched workload info from feed '%s' (https://dev.azure.com/%s/%s/_artifacts/feed/%s)", feedName, organisationName, projectName, feedName) + output.PrintlnfInfo("Created wiki '%s' (%s)", wikiName, ado.WikiRemoteUrl) - err = createWorkLoadPages(workloads, wikiRepo.GetRepositoryPath()) + wikiRepo, err := git.NewClonedGit(ado.WikiRepoRemoteUrl, "x-oauth-basic", config.ADO.PAT, config.Git.UserEmail, config.Git.UserName) if err != nil { return err } + defer os.RemoveAll(wikiRepo.GetRepositoryPath()) - err = createWorkloadIndex(workloads, wikiRepo.GetRepositoryPath()) + err = writeWiki(&wc, wikiRepo.GetRepositoryPath()) if err != nil { return err } - output.PrintlnInfo("Generated workload index and pages") - err = publish(ado, wikiName, wikiRepo) if err != nil { - return errors.New("could not create or automerge PR") + return errors.New("could not create or automerge pr") } output.PrintlnfInfo("Published Wiki '%s' (%s)", wikiName, ado.WikiRemoteUrl) diff --git a/pkg/cmd/app/generate/tpl/sets.md.tpl b/pkg/cmd/app/generate/tpl/sets.md.tpl new file mode 100644 index 0000000..0089c3b --- /dev/null +++ b/pkg/cmd/app/generate/tpl/sets.md.tpl @@ -0,0 +1,10 @@ +# Sets + +## Sets available in this project + +| Name | Instances | Latest version | Latest build | +|:-----------|:-----------|:-----------|:-----------| +{{- range .Sets }} +| {{ .Name }} | 0 | 0 | 0 | +{{- end }} + diff --git a/pkg/cmd/app/generate/tpl/sets/set.md.tpl b/pkg/cmd/app/generate/tpl/sets/set.md.tpl new file mode 100644 index 0000000..650cb62 --- /dev/null +++ b/pkg/cmd/app/generate/tpl/sets/set.md.tpl @@ -0,0 +1,2 @@ +# {{ .Name }} + diff --git a/pkg/cmd/app/generate/tpl/workload.tpl b/pkg/cmd/app/generate/tpl/workloads.md.tpl similarity index 100% rename from pkg/cmd/app/generate/tpl/workload.tpl rename to pkg/cmd/app/generate/tpl/workloads.md.tpl diff --git a/pkg/cmd/app/generate/tpl/workloads/.order.tpl b/pkg/cmd/app/generate/tpl/workloads/.order.tpl new file mode 100644 index 0000000..e87f1b1 --- /dev/null +++ b/pkg/cmd/app/generate/tpl/workloads/.order.tpl @@ -0,0 +1,3 @@ +{{- range .Workloads }} +{{ .Name }} +{{- end }} diff --git a/pkg/cmd/app/generate/tpl/workloads/workload.md.tpl b/pkg/cmd/app/generate/tpl/workloads/workload.md.tpl new file mode 100644 index 0000000..51a4c9b --- /dev/null +++ b/pkg/cmd/app/generate/tpl/workloads/workload.md.tpl @@ -0,0 +1,3 @@ +# {{ .Name }} + +{{ .Readme }} diff --git a/pkg/core/azuredevops.go b/pkg/core/azuredevops.go index dcbe1c1..b38ef60 100644 --- a/pkg/core/azuredevops.go +++ b/pkg/core/azuredevops.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "strings" "github.com/frontierdigital/utils/azuredevops" "github.com/google/uuid" @@ -59,9 +60,9 @@ func (ado *AzureDevOps) GetWorkloadInfo() (*[]Workload, error) { for _, p := range *packages { if len(*p.Versions) > 0 { - c, _ := azureDevOps.GetFileContent(ado.ProjectName, *p.Name, *(*p.Versions)[0].Version, "README.md") + c, _ := azureDevOps.GetFileContent(ado.ProjectName, *p.Name, *(*p.Versions)[0].Version, "README.md", "tag") workloads = append(workloads, Workload{ - Name: *p.Name, + Name: strings.ReplaceAll(*p.Name, "-workload", ""), Version: *(*p.Versions)[0].Version, Build: "N/A", Readme: *c.Content, @@ -72,3 +73,50 @@ func (ado *AzureDevOps) GetWorkloadInfo() (*[]Workload, error) { return &workloads, nil } + +func getManifestContent(azureDevOps *azuredevops.AzureDevOps, projectName *string, repoName *string) (*Manifest, error) { + m := "main" + c, err := azureDevOps.GetFileContent(*projectName, *repoName, m, "manifest.yml", "branch") + if err != nil { + return nil, err + } + + man, err := LoadManifestFromString(*c.Content) + if err != nil { + return nil, err + } + + return &man, nil +} + +func (ado *AzureDevOps) GetSets() (*[]SetCollection, error) { + azureDevOps := azuredevops.NewAzureDevOps(ado.OrganisationName, ado.PAT) + var sets []SetCollection + + repos, err := azureDevOps.GetRepositories(ado.ProjectName) + if err != nil { + return nil, err + } + + for _, r := range *repos { + if strings.HasSuffix(*r.Name, "-set") { + n := getSetNameFromRepoName(r.Name) + + m, err := getManifestContent(azureDevOps, &ado.ProjectName, r.Name) + if err != nil { + return nil, err + } + + sc := getSetCollectionByName(&sets, *n) + if sc == nil { + sets = newSetCollection(&sets, *n) + sc = getSetCollectionByName(&sets, *n) + } + sc.addSet(&Set{ + Name: *n, + Manifest: m, + }) + } + } + return &sets, nil +} diff --git a/pkg/core/manifest.go b/pkg/core/manifest.go index 204f68d..5b2aae8 100644 --- a/pkg/core/manifest.go +++ b/pkg/core/manifest.go @@ -69,3 +69,14 @@ func LoadManifest(filePath string) (Manifest, error) { return manifest, nil } + +func LoadManifestFromString(manifestContent string) (Manifest, error) { + manifest := Manifest{} + + err := yaml.Unmarshal([]byte(manifestContent), &manifest) + if err != nil { + return manifest, err + } + + return manifest, nil +} diff --git a/pkg/core/set.go b/pkg/core/set.go new file mode 100644 index 0000000..ce105ed --- /dev/null +++ b/pkg/core/set.go @@ -0,0 +1,44 @@ +package core + +import ( + "regexp" + "strings" +) + +func newSetCollection(sets *[]SetCollection, name string) []SetCollection { + s := append(*sets, SetCollection{Name: name}) + return s +} + +func getSetCollectionByName(sets *[]SetCollection, name string) *SetCollection { + for _, s := range *sets { + if s.Name == name { + return &s + } + } + return nil +} + +func (c *SetCollection) hasSet(s *Set) bool { + for _, v := range c.Sets { + if v.Name == s.Name && v.Environment == s.Environment { + return true + } + } + return false +} + +func (c *SetCollection) addSet(s *Set) { + if c.hasSet(s) { + return + } + c.Sets = append(c.Sets, s) +} + +func getSetNameFromRepoName(repoName *string) *string { + n := strings.ReplaceAll(*repoName, "-set", "") + re := regexp.MustCompile(`^.+?-`) + n = re.ReplaceAllString(n, "") + + return &n +} diff --git a/pkg/core/types.go b/pkg/core/types.go index c750957..1b481aa 100644 --- a/pkg/core/types.go +++ b/pkg/core/types.go @@ -40,6 +40,20 @@ type Manifest struct { Workloads []*WorkloadInstance `yaml:"workloads"` } +type Set struct { + Name string + Environment string + Next *Set + Previous *Set + Manifest *Manifest +} + +type SetCollection struct { + Name string + Entry *Set + Sets []*Set +} + type Workload struct { Name string Version string