Skip to content

Commit

Permalink
no circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dazz committed Dec 10, 2023
1 parent 1eeee43 commit a42941a
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions cmd/s6-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -123,6 +124,11 @@ type Lint struct {


func compileDependencyTree(rootPath string, currentService string, services *[]Service, lints *[]Lint) bool {
// Check if the service is already in the services list
if containsService(*services, currentService) {
return true
}

// Check if the directory exists
currentServicePath := rootPath + "/" + currentService
if _, err := os.Stat(currentServicePath); os.IsNotExist(err) {
Expand All @@ -135,15 +141,33 @@ func compileDependencyTree(rootPath string, currentService string, services *[]S

// check the type file and content
typeFile := currentServicePath + "/type"
serviceTyp, err := ioutil.ReadFile(typeFile)
typeFileContent, err := ioutil.ReadFile(typeFile)
if err != nil {
*lints = append(*lints, Lint{
Service: currentService,
Message: fmt.Sprintf("type file for \"%s\" does not exist", currentService),
})
return false
}
serviceType := string(serviceTyp)
// Check if the file is empty
if len(typeFileContent) == 0 {
*lints = append(*lints, Lint{
Service: currentService,
Message: fmt.Sprintf("type file for \"%s\" is empty", currentService),
})
return false
}

// Check if the last character is a newline
lastChar := typeFileContent[len(typeFileContent)-1]
if lastChar != '\n' {
*lints = append(*lints, Lint{
Service: currentService,
Message: fmt.Sprintf("type file for \"%s\" does not end with a newline", currentService),
})
}

serviceType := strings.ReplaceAll(string(typeFileContent), "\n", "")

// check if the dependency directory exists
dependenciesDir := ""
Expand Down Expand Up @@ -171,8 +195,6 @@ func compileDependencyTree(rootPath string, currentService string, services *[]S
// we don't want to check the base directory
continue
}
// recursive call
compileDependencyTree(rootPath, f.Name(), services, lints)
}

// add the service to the services list
Expand All @@ -182,6 +204,14 @@ func compileDependencyTree(rootPath string, currentService string, services *[]S
Dependencies: dependencies,
})

for _, dependency := range dependencies {
if containsService(*services, dependency) || dependency == "base" {
continue
}
// recursive call
compileDependencyTree(rootPath, dependency, services, lints)
}

return true
}

Expand All @@ -195,4 +225,13 @@ func renderMermaidGraph(services []Service) string {
}
graph += "```\n"
return graph
}

func containsService (services []Service, service string) bool {
for _, s := range services {
if s.Name == service {
return true
}
}
return false
}

0 comments on commit a42941a

Please sign in to comment.