Skip to content

Commit

Permalink
Merge pull request #9 from netvolart/doc
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
netvolart authored Mar 21, 2024
2 parents cf000a8 + b8c9aca commit c6c7211
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 74 deletions.
17 changes: 17 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ Currently we are supporting GitLab and Community Registry as a terraform modules

## Installation

### MacOS via homebrew

```bash
brew tap netvolart/tap
brew install joven
```
### Linux and MacOS from binary

To install Joven, you can download the latest release from the [Releases](https://github.com/yourusername/joven/releases) page. Choose the binary that matches your operating system and architecture, download it, and place it in your system's PATH.

For example:

```sh
wget https://github.com/netvolart/joven/releases/download/v0.0.1/joven_0.0.1_linux_amd64.tar.gz
tar -xvzf joven_0.0.1_linux_amd64.tar.gz
chmod +x joven
sudo mv joven /usr/local/bin/
```

## Configuration

Joven uses a configuration file to store settings.
Expand Down
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "joven",

Use: "joven",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
4 changes: 1 addition & 3 deletions terraform/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"net/http"
"net/url"
"strings"

"github.com/netvolart/joven/config"
)

func getModuleVersionsFromRegistry(url string, localModule LocalModule) (module TerraformModule, Error error) {
Expand Down Expand Up @@ -41,7 +39,7 @@ func getModuleVersionsFromRegistry(url string, localModule LocalModule) (module

}

func CreateModuleGitlabUrl(c *config.Config, moduleName string) (string, error) {
func createModuleCommunityUrl(moduleName string) (string, error) {
if moduleName == "" {
return "", ErrorPageNumberEmpty
}
Expand Down
6 changes: 2 additions & 4 deletions terraform/community_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ func createRegistryMockServer(t *testing.T) *httptest.Server {

func TestCreateModuleCommunityUrl(t *testing.T) {
t.Run("Test First page", func(t *testing.T) {
config := generateMockConfig(t)
url, err := CreateModuleGitlabUrl(config, "registry.terraform.io/terraform-aws-modules/vpc/aws")
url, err := createModuleCommunityUrl("registry.terraform.io/terraform-aws-modules/vpc/aws")
if err != nil {
t.Errorf("Unable to generate URL %s", err)
}
Expand All @@ -89,8 +88,7 @@ func TestCreateModuleCommunityUrl(t *testing.T) {
}
})
t.Run("Test Empty page", func(t *testing.T) {
config := generateMockConfig(t)
_, err := createModuleGitlabUrl(config, "")
_, err := createModuleCommunityUrl("")

if err != ErrorPageNumberEmpty {
t.Errorf("got %s want %s given", ErrorPageNumberEmpty, err)
Expand Down
5 changes: 2 additions & 3 deletions terraform/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func findOutdated(modules []*TerraformModule) ([]*TerraformModule, error) {

func CompareGitLabModules(c *config.Config, localModulesData []byte) ([]*TerraformModule, error) {
// parse local modules
localModulesResult, err := GetLocalModules(localModulesData)
localModulesResult, err := getLocalModules(localModulesData)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -65,15 +65,14 @@ func CompareGitLabModules(c *config.Config, localModulesData []byte) ([]*Terrafo

} else if localModule.Type == "community" {

url, err := CreateModuleGitlabUrl(c, localModule.Source)
url, err := createModuleCommunityUrl(localModule.Source)
if err != nil {
return nil, err
}
communityModule, err := getModuleVersionsFromRegistry(url, localModule)
if err != nil {
return nil, err
}
// log.Println(communityModule)
resultModules = append(resultModules, &communityModule)
}

Expand Down
60 changes: 0 additions & 60 deletions terraform/gitlab_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package terraform
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -59,62 +58,3 @@ func makeGiLabModulesRequest(c *config.Config, url string) (modulesResp *[]Respo
}
return &responses, totalPages, nil
}

func downloadModulesMetadata(c *config.Config) ([]Response, error) {
url, err := createGitLabUrl(c, "1")
if err != nil {
return nil, err
}
responses, totalPages, err := makeGiLabModulesRequest(c, url)
if err != nil {
return nil, err
}

var fullResponses []Response

fullResponses = append(fullResponses, *responses...)
resultChannel := make(chan *[]Response)
for i := 2; i <= totalPages; i++ {

url, err := createGitLabUrl(c, strconv.Itoa(i))
if err != nil {
return nil, err
}
go func(c *config.Config, url string) {
responses, _, err := makeGiLabModulesRequest(c, url)
if err != nil {
log.Println(err)
return
}
resultChannel <- responses
}(c, url)

fullResponses = append(fullResponses, *responses...)
}
for i := 2; i <= totalPages; i++ {
r := <-resultChannel
fullResponses = append(fullResponses, *r...)
}
return fullResponses, nil
}

func GetModulesFromGitlab(c *config.Config) ([]*TerraformModule, error) {
responses, err := downloadModulesMetadata(c)
if err != nil {
log.Printf("Error getting modules from GitLab: %v", err)

}
var modules []*TerraformModule
for _, response := range responses {
link := "https://gitlab.com" + response.Links.WebPath
module := NewTerraformModule(response.Name, "", response.Version, link, false)
modules = append(modules, module)
}

cleared, err := clearOldVersions(modules)
if err != nil {
log.Printf("Unable to clean modules: %v", err)
}

return cleared, nil
}
2 changes: 1 addition & 1 deletion terraform/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type LocalModules struct {
Modules []LocalModule `json:"Modules"`
}

func GetLocalModules(data []byte) (*LocalModules, error) {
func getLocalModules(data []byte) (*LocalModules, error) {
var localModules LocalModules
err := json.Unmarshal(data, &localModules)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion terraform/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGetLocalModules(t *testing.T) {
]
}`

localModules, err := GetLocalModules([]byte(data))
localModules, err := getLocalModules([]byte(data))
if err != nil {
t.Errorf(err.Error())
}
Expand Down

0 comments on commit c6c7211

Please sign in to comment.