cmd

package
v0.0.0-...-d10ae67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "ps2info",
	Short: "ps2info is an helper to gather all informations about a prestashop deployement.",
	Long:  `ps2info is an helper to gather all informations about a prestashop deployement.`,
}

RootCmd is the root command for ovh-qa

View Source
var ScanCmd = &cobra.Command{
	Use:     "scan",
	Aliases: []string{"s"},
	Short:   "scan information about a prestashop deployment.",
	Long:    "scan information about a prestashop deployment (version, modules, shops...) into a yaml report.",
	Run: func(cmd *cobra.Command, args []string) {

		parametersFilePath := filepath.Join(psDir, parametersFile)

		if _, err := os.Stat(parametersFilePath); err == nil {

			log.Infof("Found '%s' config file, reading content from it...\n", parametersFilePath)
			dat, err := ioutil.ReadFile(parametersFilePath)
			checkErr("ReadFile.error", err)

			databaseHost := reDatabaseHost.FindSubmatch(dat)
			if len(databaseHost) > 0 {
				dbHost = string(databaseHost[1])
			}

			if !inSlice(dbHost, defaultDbHosts, false) {
				dbHost = defaultDbHost
			}

			databasePort := reDatabasePort.FindSubmatch(dat)
			if len(databasePort) > 0 {
				dbPort = string(databasePort[1])
			}
			if dbPort == "" {
				dbPort = defaultDbPort
			}

			databaseName := reDatabaseName.FindSubmatch(dat)
			if len(databaseName) > 0 {
				dbName = string(databaseName[1])
			}

			databaseUser := reDatabaseUser.FindSubmatch(dat)
			if len(databaseUser) > 0 {
				dbUser = string(databaseUser[1])
			}

			databasePass := reDatabasePass.FindSubmatch(dat)
			if len(databasePass) > 0 {
				dbPass = string(databasePass[1])
			}

			databasePref := reDatabasePref.FindSubmatch(dat)
			if len(databasePref) > 0 {
				dbTablePrefix = string(databasePref[1])
			}

		} else {
			log.Warnf("Could not find '%s' config file, using command cli arguments...\n", parametersFilePath)
		}

		if !dryRun {
			var err error

			dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)
			db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
				NamingStrategy: schema.NamingStrategy{
					TablePrefix:   dbTablePrefix,
					SingularTable: true,
					NameReplacer:  strings.NewReplacer("ID", "Id"),
				},
			})
			if err != nil {
				log.Fatal(err)
			}

			err = db.Where("active = ?", 1).Find(&psLangs).Error
			pp.Println("psLangs:", psLangs)

			err = db.Where("active = ?", 1).Find(&psShops).Error
			if errors.Is(err, gorm.ErrRecordNotFound) {
				log.Fatal("shops not found")
			}
			pp.Println("psShops:", psShops)

			err = db.Find(&psGroups).Error
			if errors.Is(err, gorm.ErrRecordNotFound) {
				log.Fatal("groups not found")
			}
			pp.Println("psGroups:", psGroups)

			psModules := make([]*Module, 0)
			err = db.Find(&psModules).Error
			if errors.Is(err, gorm.ErrRecordNotFound) {
				log.Fatal("modules not found")
			}

			modulesPath := filepath.Join(psDir, "modules")
			files, err := ioutil.ReadDir(modulesPath)
			if err != nil {
				log.Fatal(err)
			}

			for _, f := range files {
				if (f.Name() != "." || f.Name() != "..") && f.IsDir() {

					modulePath := filepath.Join(modulesPath, f.Name())
					xmlConfigs, _ := filepath.Glob(fmt.Sprintf("%s/config*.xml", modulePath))
					for _, xmlConfig := range xmlConfigs {
						log.Infoln("module xml config found: ", xmlConfig)

						doc := etree.NewDocument()
						if err := doc.ReadFromFile(xmlConfig); err != nil {
							panic(err)
						}

						root := doc.SelectElement("module")
						if root != nil {
							fmt.Println("ROOT element:", root.Tag)
						} else {
							log.Warnln("could not parse root element from: ", xmlConfig)
						}

						var nameStr string
						name := root.SelectElement("name")
						if name != nil {
							nameStr = name.Text()
							fmt.Printf("  NAME: %s \n", name.Text())
						}

						var displayNameStr string
						displayName := root.SelectElement("displayName")
						if displayName != nil {
							displayNameStr = displayName.Text()
							fmt.Printf("  DISPLAY_NAME: %s \n", displayName.Text())
						}

						var descriptionStr string
						description := root.SelectElement("description")
						if description != nil {
							descriptionStr = description.Text()
							fmt.Printf("  DESCRIPTION: %s \n", description.Text())
						}

						var authorStr string
						author := root.SelectElement("author")
						if author != nil {
							authorStr = author.Text()
							fmt.Printf("  AUTHOR: %s \n", author.Text())
						}

						var tabStr string
						tab := root.SelectElement("tab")
						if tab != nil {
							tabStr = tab.Text()
							fmt.Printf("  TAB: %s \n", tab.Text())
						}

						var isConfigurableBool bool
						isConfigurable := root.SelectElement("is_configurable")
						if isConfigurable != nil {
							var err error
							isConfigurableBool, err = strconv.ParseBool(isConfigurable.Text())
							checkErr("ParseBool.Error", err)
							fmt.Printf("  IS_CONFIGURABLE: %s \n", isConfigurable.Text())
						}

						var needInstanceBool bool
						needInstance := root.SelectElement("need_instance")
						if needInstance != nil {
							var err error
							needInstanceBool, err = strconv.ParseBool(needInstance.Text())
							checkErr("ParseBool.Error", err)
							fmt.Printf("  NEED_INSTANCE: %s \n", needInstance.Text())
						}

						var limitedCountriesStr string
						limitedCountries := root.SelectElement("limited_countries")
						if limitedCountries != nil {
							limitedCountriesStr = limitedCountries.Text()
							fmt.Printf("  LIMITED_COUNTRIES: %s \n", limitedCountries.Text())
						}

						for i, psModule := range psModules {
							if psModule.Module.Name == f.Name() && nameStr == f.Name() {
								psModules[i].DisplayName = displayNameStr
								psModules[i].Description = descriptionStr
								psModules[i].Author = authorStr
								psModules[i].Tab = tabStr
								psModules[i].IsConfigurable = isConfigurableBool
								psModules[i].NeedInstance = needInstanceBool
								psModules[i].LimitedCountries = limitedCountriesStr
							}
						}
					}

					d, err := yaml.Marshal(&psModules)
					if err != nil {
						log.Fatalf("error: %v", err)
					}
					fmt.Printf("--- t dump:\n%s\n\n", string(d))

				}
			}
		}
	},
}

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately.

Types

type Module

type Module struct {
	*psm.Module
	DisplayName      string
	Description      string
	Author           string
	Tab              string
	IsConfigurable   bool
	NeedInstance     bool
	LimitedCountries string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL