Skip to content

Commit

Permalink
preparations
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalpress committed Dec 5, 2023
1 parent 55fa571 commit 0979e2d
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 134 deletions.
11 changes: 6 additions & 5 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Copyright © 2023 Sirrend
package cmd

import (
"os"
"path"
"path/filepath"

"github.com/enescakir/emoji"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/terraform_utils"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/utils/terraform"
"github.com/sirrend/terrap-cli/internal/workspace"
"os"
"path"
"path/filepath"

"github.com/spf13/cobra"
)
Expand All @@ -35,7 +36,7 @@ func deleteInitData(dir string) {
}

if ws.IsTempProvider {
err = terraform_utils.RemoveTempTerraformExecutor(ws.ExecPath)
err = terraform.RemoveTempTerraformExecutor(ws.ExecPath)
if err != nil {
_, _ = commons.RED.Println(err)
os.Exit(1)
Expand Down
15 changes: 8 additions & 7 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ package cmd

import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/enescakir/emoji"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/terraform_utils"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/utils/terraform"
"github.com/spf13/cobra"
"os"
"path"
"path/filepath"
)

// terraformInit
Expand All @@ -31,17 +32,17 @@ func terraformInit(dir string) {
if err != nil {
_, _ = commons.YELLOW.Println(emoji.Rocket, "Initializing directory...")
mainWorkspace.ExecPath, mainWorkspace.IsTempProvider,
mainWorkspace.TerraformVersion, err = terraform_utils.TerraformInit(dir) // initiate new terraform tool in context
mainWorkspace.TerraformVersion, err = terraform.TerraformInit(dir) // initiate new terraform tool in context

if err != nil {
fmt.Println()
terraform_utils.TerraformErrorPrettyPrint(err)
terraform.TerraformErrorPrettyPrint(err)
os.Exit(1)

}

_, _ = commons.YELLOW.Print(emoji.Toolbox, " Looking for providers...")
terraform_utils.FindTfProviders(dir, &mainWorkspace) //find all providers and assign to mainWorkspace
terraform.FindTfProviders(dir, &mainWorkspace) //find all providers and assign to mainWorkspace
_, _ = commons.GREEN.Println(" Done!")

_, _ = commons.YELLOW.Print(emoji.WavingHand, " Saving workspace...")
Expand Down
9 changes: 5 additions & 4 deletions cmd/open_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ Copyright © 2023 Sirrend
package cmd

import (
"os"

"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/github_issue"
"github.com/sirrend/terrap-cli/internal/github"
"github.com/spf13/cobra"
"os"
)

// createIssue
Expand All @@ -22,9 +23,9 @@ import (
isFeature - bool - is a feature request
*/
func createIssue(token, title, description string, isFeature bool) {
issue, err := github_issue.OpenIssue(token, title, description, isFeature)
issue, err := github.OpenIssue(token, title, description, isFeature)
if err != nil {
if _, ok := err.(*github_issue.RateError); ok {
if _, ok := err.(*github.RateError); ok {
_, _ = commons.YELLOW.Println(err.Error())
os.Exit(0)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"strings"

"github.com/enescakir/emoji"
"github.com/sirrend/terrap-cli/internal/cli_utils"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/receiver"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/utils/cli"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
)
Expand All @@ -24,7 +24,7 @@ var getContextCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
var workspace workspace.Workspace
var tableData [][]string
table := cli_utils.GetTable([]string{"Provider", "Version"}) // initialize new table
table := cli.GetTable([]string{"Provider", "Version"}) // initialize new table

if utils.IsInitialized(".") {
err := state.Load("./.terrap.json", &workspace)
Expand Down Expand Up @@ -71,7 +71,7 @@ var getSupportedProvidersCmd = &cobra.Command{
tableData [][]string
dataPrinted = false
)
table := cli_utils.GetTable([]string{"Provider", "Min Version", "Max Version"}) // initialize new table
table := cli.GetTable([]string{"Provider", "Min Version", "Max Version"}) // initialize new table
providers, _ := receiver.GetSupportedProviders()

// go over providers retrieved from API
Expand Down
8 changes: 4 additions & 4 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"github.com/enescakir/emoji"
"github.com/fatih/color"
"github.com/sirrend/terrap-cli/internal/annotate"
"github.com/sirrend/terrap-cli/internal/cli_utils"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/parser"
"github.com/sirrend/terrap-cli/internal/receiver"
"github.com/sirrend/terrap-cli/internal/scanning"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/utils/cli"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -60,7 +60,7 @@ var scanCmd = &cobra.Command{
_, _ = commons.RED.Println(err)
}
} else {
workspace = cli_utils.GetFixedProvidersFlag(*cmd)
workspace = cli.GetFixedProvidersFlag(*cmd)
}

// go over every provider in user's folder / user's declaration
Expand All @@ -78,14 +78,14 @@ var scanCmd = &cobra.Command{
continue
}

flags := cli_utils.ChangedComponentsFlags(*cmd)
flags := cli.ChangedComponentsFlags(*cmd)
if !cmd.Flag("annotate").Changed {
for file, fileResources := range files {
if len(fileResources) == 0 {
continue
}

for _, resource := range scanning.GetUniqResources(fileResources) {
for _, resource := range scanning.GetUniqueResources(fileResources) {
if utils.IsItemInSlice(resource.Type, flags) {
ruleset, err := resource.GetRuleset(rulebook, nil)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/whats_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

"github.com/enescakir/emoji"
"github.com/fatih/color"
"github.com/sirrend/terrap-cli/internal/cli_utils"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/receiver"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/utils/cli"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
)
Expand All @@ -37,7 +37,7 @@ var whatsNewCmd = &cobra.Command{
_, _ = commons.RED.Println(err)
}
} else {
workspace = cli_utils.GetFixedProvidersFlag(*cmd)
workspace = cli.GetFixedProvidersFlag(*cmd)
}

for provider, version := range workspace.Providers { // go over every provider in user's folder
Expand All @@ -58,7 +58,7 @@ var whatsNewCmd = &cobra.Command{
os.Exit(1)
}

flags := cli_utils.ChangedComponentsFlags(*cmd) // get resources filtering
flags := cli.ChangedComponentsFlags(*cmd) // get resources filtering
for resourcesType, resources := range ruleSets {
if utils.IsItemInSlice(resourcesType, flags) {
for resourceName := range resources.(map[string]interface{}) { // go over all ruleSets
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.19

require (
github.com/Jeffail/gabs v1.4.0
github.com/Masterminds/semver/v3 v3.2.0
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/enescakir/emoji v1.0.0
github.com/fatih/color v1.15.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig/v3 v3.2.1/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions internal/commons/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commons

import (
"github.com/fatih/color"
"os"
)

var (
Expand All @@ -17,8 +16,3 @@ var (
SIRREND = color.New(color.FgHiMagenta)
HighMagenta = color.New(color.FgMagenta)
)

func getUserHomeDir() string {
u, _ := os.UserHomeDir()
return u
}
3 changes: 2 additions & 1 deletion internal/github_issue/main.go → internal/github/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package github_issue
package github

import (
"context"
"errors"
"fmt"

"github.com/google/go-github/github"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/spf13/cast"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package github_issue
package github

import "fmt"

Expand All @@ -16,7 +16,7 @@ type RateError struct {
error - the new error
*/
func createRateError() *RateError {
message := "Rate limit exceeded, please try again in an hour or open an issue manually here: https://placeholder.com"
message := "rate limit exceeded, please try again in an hour or open an issue manually here: https://placeholder.com"
return &RateError{message: message, error: fmt.Errorf(message)}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/parser/component_rulebook.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package parser

type ComponentRulebook struct {
Versions map[string]Version `json:"versions"`
}
// type Rulebook struct {
// Versions map[string]Version `json:"versions"`
// }
16 changes: 14 additions & 2 deletions internal/receiver/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package receiver

import "github.com/sirrend/terrap-cli/internal/files_handler"
import (
"net/http"

"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/parser"
)

type RulesAPIRequest struct {
Provider string `json:"provider"`
Expand All @@ -15,9 +20,16 @@ func CreateRulesRequest() *RulesAPIRequest {

func (r *RulesAPIRequest) fillUsedResources(files map[string][]files_handler.Resource) {
// 1. extract all used components by iterating over the files argument
// 2. insert resources to the UsedResource map
// 2. insert resources to the UsedResources map
}

func (r *RulesAPIRequest) fetchRules() {
// 1. perform request using the internal/requests library
}

func loadRulebook(response *http.Response) *parser.Rulebook {
// 1. Convert the response object body to bytes representation.
// 2. Marshal it to a parser.Rulebook struct.

return &parser.Rulebook{}
}
15 changes: 1 addition & 14 deletions internal/scanning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,9 @@ package scanning

import (
"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/utils"
)

func WhereDoesResourceAppear(resources []files_handler.Resource) map[string][]string {
appearances := make(map[string][]string)

for _, resource := range resources {
if !utils.IsItemInSlice(utils.GetAbsPath(resource.Pos.Filename), appearances[resource.Name]) {
appearances[resource.Name] = append(appearances[resource.Name], utils.GetAbsPath(resource.Pos.Filename))
}
}

return appearances
}

func GetUniqResources(resources []files_handler.Resource) []files_handler.Resource {
func GetUniqueResources(resources []files_handler.Resource) []files_handler.Resource {
var tempResourcesSlice []files_handler.Resource
tempResourcesMap := map[string]files_handler.Resource{}

Expand Down
11 changes: 11 additions & 0 deletions internal/ui/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ui

import "github.com/sirrend/terrap-cli/internal/parser"

func PrintChangelog(rulebook *parser.Rulebook) {

}

func PrintChangelogAsJson(rulebook *parser.Rulebook) {

}
7 changes: 4 additions & 3 deletions internal/cli_utils/main.go → internal/utils/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cli_utils
package cli

import (
"fmt"
"os"
"strings"

"github.com/common-nighthawk/go-figure"
"github.com/enescakir/emoji"
"github.com/hashicorp/go-version"
Expand All @@ -10,8 +13,6 @@ import (
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
validate "golang.org/x/mod/semver"
"os"
"strings"
)

// SirrendLogoPrint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package terraform_utils
package terraform

import (
"fmt"
"strings"

"github.com/enescakir/emoji"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/utils"
"strings"
)

type TerraformError struct {
Expand Down
Loading

0 comments on commit 0979e2d

Please sign in to comment.