github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/main.go (about)

     1  package main
     2  
     3  import (
     4  	"embed"
     5  	"fmt"
     6  	"github.com/ismailbayram/bigpicture/internal/browser"
     7  	"github.com/ismailbayram/bigpicture/internal/config"
     8  	"github.com/ismailbayram/bigpicture/internal/graph"
     9  	"github.com/ismailbayram/bigpicture/internal/server"
    10  	"github.com/ismailbayram/bigpicture/internal/validators"
    11  	"os"
    12  )
    13  
    14  //go:embed web/*
    15  var staticFiles embed.FS
    16  
    17  type BigPicture struct {
    18  	cfg  *config.Configuration
    19  	tree *graph.Tree
    20  }
    21  
    22  func (bp *BigPicture) Validate() error {
    23  	for _, validatorConf := range bp.cfg.Validators {
    24  		validator, err := validators.NewValidator(validatorConf.Type, validatorConf.Args, bp.tree)
    25  		if err != nil {
    26  			return err
    27  		}
    28  		if err := validator.Validate(); err != nil {
    29  			return err
    30  		}
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  func main() {
    37  	bp := BigPicture{
    38  		cfg:  config.Init(),
    39  		tree: graph.NewTree("root"),
    40  	}
    41  
    42  	lang := detectLanguage()
    43  	if bp.cfg.Lang != "" {
    44  		checkSupportedLang(bp.cfg.Lang)
    45  		lang = bp.cfg.Lang
    46  	}
    47  	if lang == "" && bp.cfg.Lang == "" {
    48  		fmt.Printf("Could not detect the project language. Please run the command in the root directory of the project"+
    49  			" or you can set `lang` parameter in .bigpicture.json file.\n"+
    50  			"%s\n", supportedLangs())
    51  		os.Exit(1)
    52  	}
    53  
    54  	brow := browser.NewBrowser(lang, bp.tree, bp.cfg.IgnoredPaths, bp.cfg.RootDir)
    55  	brow.Browse(".")
    56  	bp.tree.GenerateLinks()
    57  	bp.tree.CalculateInstability()
    58  
    59  	switch os.Args[1] {
    60  	case "server":
    61  		server.RunServer(staticFiles, bp.cfg.Port, bp.tree.ToJSON())
    62  	case "validate":
    63  		if err := bp.Validate(); err != nil {
    64  			fmt.Println("validation failed")
    65  			fmt.Println(err)
    66  			os.Exit(1)
    67  		}
    68  	case "help":
    69  		printHelp()
    70  	default:
    71  		fmt.Println("Invalid command. Use 'bigpicture help' to see available commands.")
    72  		os.Exit(1)
    73  	}
    74  }
    75  
    76  func printHelp() {
    77  	fmt.Println("Usage:")
    78  	fmt.Println("server")
    79  	fmt.Println("\tRuns the web server on port which is defined in .big.picture.json file. Default port is 44525.")
    80  
    81  	fmt.Println("\nvalidate")
    82  	fmt.Println("\tValidates the project structure. It checks if the project structure is valid according to the validators which are defined in .big.picture.json file.")
    83  
    84  	fmt.Println("\nhelp")
    85  	fmt.Println("\tPrints this help message.")
    86  }
    87  
    88  func detectLanguage() string {
    89  	if _, err := os.Stat("go.mod"); !os.IsNotExist(err) {
    90  		return browser.LangGo
    91  	}
    92  	if _, err := os.Stat("requirements.txt"); !os.IsNotExist(err) {
    93  		return browser.LangPy
    94  	}
    95  	if _, err := os.Stat("pom.xml"); !os.IsNotExist(err) {
    96  		return browser.LangJava
    97  	}
    98  	return ""
    99  }
   100  
   101  func checkSupportedLang(lang string) {
   102  	if lang != browser.LangGo && lang != browser.LangPy && lang != browser.LangJava {
   103  		fmt.Printf("Unsupported language: %s\n%s", lang, supportedLangs())
   104  		os.Exit(1)
   105  	}
   106  }
   107  
   108  func supportedLangs() string {
   109  	return fmt.Sprintf("Supported languages: %s, %s, %s\n", browser.LangGo, browser.LangPy, browser.LangJava)
   110  }