search

package
v0.0.0-...-47d8c88 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cli.Command{
	Name:      "search",
	Usage:     "Search a VM in the config.",
	Flags:     flags,
	ArgsUsage: "<hostnames>",
	Action: func(cCtx *cli.Context) error {
		ctx := cCtx.Context
		if cCtx.NArg() < 1 {
			return errors.New("not enough arguments")
		}
		arg := cCtx.Args().Get(0)
		hostnamesRanges := generators.SplitCommaOutsideOfBrackets(arg)

		var hostnames []string
		for _, hostnamesRange := range hostnamesRanges {
			h := generators.ExpandBrackets(hostnamesRange)
			hostnames = append(hostnames, h...)
		}

		conf, err := config.ParseFile(cCtx.String("config.path"))
		if err != nil {
			return err
		}
		if err := conf.Validate(); err != nil {
			return err
		}

		logger.I.Info("Searching...", zap.Any("hostnames", hostnames))

		var wg sync.WaitGroup
		errChan := make(chan error)

		for _, hostname := range hostnames {
			wg.Add(1)
			go func(ctx context.Context, hostname string, wg *sync.WaitGroup, errChan chan<- error) {
				defer wg.Done()

				// Search host and cloud by hostname
				var host *config.Host
				var cl *config.Cloud
				var err error

				for _, suffix := range conf.SuffixSearch {
					host, cl, err = conf.SearchHostByHostName(hostname + suffix)
					if err != nil {
						errChan <- err
						return
					}
					if host != nil && cl != nil {
						break
					}
				}

				if host == nil && cl == nil {
					host, cl, err = conf.SearchHostByHostName(hostname)
					if err != nil {
						errChan <- err
						return
					}
				}

				if host == nil && cl == nil {
					errChan <- errors.New("hostname not found")
					return
				}

				logger.I.Info(
					"Search command successful.",
					zap.Any("host", host),
					zap.Any("cloud", cl),
				)
			}(
				ctx,
				hostname,
				&wg,
				errChan,
			)
		}

		go func() {
			wg.Wait()
			close(errChan)
		}()

		for e := range errChan {
			if e != nil {
				logger.I.Error("create thrown an error", zap.Error(e))
				err = e
			}
		}
		if err != nil {
			return err
		}

		logger.I.Info("Search command successful.")

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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