github.com/shravanasati/hydra@v1.0.1-0.20240122045627-1082d2ed50d2/src/hydra.go (about)

     1  /*
     2  Code responsible for the hydra CLI.
     3  
     4  Author: Shravan Asati
     5  Originally Written: 27 March 2021
     6  Last edited: 4 June 2021
     7  */
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"github.com/thatisuday/commando"
    14  	"regexp"
    15  	"strings"
    16  )
    17  
    18  const (
    19  	NAME    string = "hydra"
    20  	VERSION string = "2.2.0"
    21  )
    22  
    23  var (
    24  	supportedLangs    []string          = []string{"go", "python", "web", "flask", "ruby", "c", "c++"}
    25  	supportedLicenses map[string]string = map[string]string{
    26  		"APACHE": "Apache License",
    27  		"BSD":    "Berkeley Software Distribution 3-Clause",
    28  		"EPL":    "Eclipse Public License",
    29  		"GPL":    "GNU General Public License v3",
    30  		"MIT":    "Massachusetts Institute of Technology License",
    31  		"MPL":    "Mozilla Public License",
    32  		"UNI":    "The Unilicense"}
    33  )
    34  
    35  func stringInSlice(s string, slice []string) bool {
    36  	for _, v := range slice {
    37  		if v == s {
    38  			return true
    39  		}
    40  	}
    41  	return false
    42  }
    43  
    44  func wrongProjectName(projectName string) bool {
    45  	match, _ := regexp.MatchString(`\.|\?|\*|\:|\,|\'|\"|\||\|\|/<|>`, projectName)
    46  	return match
    47  }
    48  
    49  func main() {
    50  	fmt.Println(NAME, VERSION)
    51  
    52  	// * basic configuration
    53  	commando.
    54  		SetExecutableName(NAME).
    55  		SetVersion(VERSION).
    56  		SetDescription("hydra is command line utility used to generate language-specific project structure. \nFor more detailed information and documentation, visit https://github.com/shravanasati/hydra . \n")
    57  
    58  	commando.
    59  		Register(nil).
    60  		SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
    61  			fmt.Println("\nExecute `hydra -h` for help.")
    62  		})
    63  
    64  	// * the list command
    65  	commando.
    66  		Register("list").
    67  		SetShortDescription("Lists supported languages, licenses and user configurations.").
    68  		SetDescription("Lists supported languages, licenses and user configurations.").
    69  		AddArgument("item", "The item to list. Valid options are `langs`, `licenses` and `configs`.", "").
    70  		SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
    71  
    72  			if args["item"].Value == "langs" {
    73  				fmt.Println(list("langs"))
    74  			} else if args["item"].Value == "licenses" {
    75  				fmt.Println(list("licenses"))
    76  			} else if args["item"].Value == "configs" {
    77  				fmt.Println(list("configs"))
    78  			} else {
    79  				fmt.Println(list(args["item"].Value))
    80  			}
    81  		})
    82  
    83  	commando.
    84  		Register("config").
    85  		SetShortDescription("Alter or set the hydra user configuration.").
    86  		SetDescription("Alter or set the hydra user configuration.").
    87  		AddFlag("name", "The user's full name.", commando.String, "default").
    88  		AddFlag("github-username", "The user's GitHub username.", commando.String, "default").
    89  		AddFlag("default-lang", "The user's default language for project initialisation.", commando.String, "default").
    90  		AddFlag("default-license", "The user's default license for project initialisation.", commando.String, "default").
    91  		SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
    92  			config(
    93  				flags["name"].Value.(string), 
    94  				flags["github-username"].Value.(string), 
    95  				flags["default-lang"].Value.(string), 
    96  				strings.ToUpper(flags["default-license"].Value.(string)))
    97  		})
    98  
    99  	// * the init command
   100  	commando.
   101  		Register("init").
   102  		SetShortDescription("Intialises the project structure.").
   103  		SetDescription("Intialises the project structure.\n\nUsage: \n name : project name \n lang : programming language in which the project is being built.").
   104  		AddArgument("name", "Name of the project", "").
   105  		AddArgument("lang", "Language/framework of the project. To view valid options for the this parameter, execute `hydra list langs`.", "default").
   106  		AddFlag("license", "The license to initialise the project with.", commando.String, "default").
   107  		SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
   108  
   109  			// * checking if user has properly configured hydra (full name and github username)
   110  			if !checkForCorrectConfig() {
   111  				fmt.Println("Error: You've not set your hydra configuration. You cannot proceed without setting the necessary configuration.\nTo set configuration, execute `hydra config --name \"YOUR NAME\" --github-username \"YOUR GITHUB USERNAME\"` .\nFor further assistance regarding hydra configuration, type in `hydra config -h` .")
   112  				return
   113  			}
   114  
   115  			// * checking for correct license
   116  			license := strings.ToUpper(flags["license"].Value.(string))
   117  			if license == "DEFAULT" {
   118  				license = getConfig("defaultLicense")
   119  			}
   120  			if !stringInSlice(license, []string{"MIT", "BSD", "MPL", "EPL", "GPL", "APACHE", "UNI"}) {
   121  				fmt.Printf("Invalid value for flag license: '%v'.\n", license)
   122  				fmt.Println("You've either provided invalid license flag in the init command, or you've set wrong license in your hydra configuration.\nTo see your hydra configuration, execute `hydra list configs`.")
   123  				return
   124  			}
   125  
   126  			// * checking for correct project language
   127  			projectLang := strings.ToLower(args["lang"].Value)
   128  			if projectLang == "default" {
   129  				projectLang = getConfig("defaultLang")
   130  			}
   131  
   132  			projectName := args["name"].Value
   133  
   134  			// * checking the project name
   135  			if wrongProjectName(projectName) {
   136  				fmt.Printf(`Error: Invalid project name: '%v'. Characters like (, " | \ ? / : ; < >) are not allowed in filenames.`+"\n", projectName)
   137  				return
   138  			}
   139  
   140  			init := Initializer{
   141  				projectName: projectName,
   142  				license:     license,
   143  				lang:        projectLang,
   144  			}
   145  			switch projectLang {
   146  			case "python":
   147  				init.pythonInit()
   148  			case "go":
   149  				init.goInit()
   150  			case "web":
   151  				init.webInit()
   152  			case "flask":
   153  				init.flaskInit()
   154  			case "c":
   155  				init.cInit()
   156  			case "c++":
   157  				init.cppInit()
   158  			case "ruby":
   159  				init.rubyInit()
   160  			default:
   161  				fmt.Printf("Unsupported language type: '%v'. Cannot initiate the project. \nHint: You've either a typo at the language name, or the hydra default language configuration is wrong.", projectLang)
   162  			}
   163  		})
   164  
   165  	commando.
   166  		Register("update").
   167  		SetShortDescription("The update command updates hydra to the latest release.").
   168  		SetDescription("The update command downloads and installs the latest hydra release.").
   169  		SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
   170  			update()
   171  		})
   172  
   173  	commando.Parse(nil)
   174  	deletePreviousInstallation()
   175  }