other

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AiCmd = &cobra.Command{
	Use:   "ai",
	Short: "Run AI-powered tasks",
	Long: `Run various AI-powered tasks, including general text processing or natural language
queries using OpenAI's API.`,
	Run: func(cmd *cobra.Command, args []string) {
		inputText, _ := cmd.Flags().GetString("input")
		isNaturalLanguage, _ := cmd.Flags().GetBool("natural")

		if inputText == "" {
			cmd.Help()
			return
		}

		result, err := runAIWithOpenAI(inputText, isNaturalLanguage)
		if err != nil {
			fmt.Printf("Error: %v\n", err)
			return
		}
		fmt.Println("AI Result:", result)
	},
}

AiCmd represents the ai command

View Source
var ApiResourcesCmd = &cobra.Command{
	Use:   "api-resources",
	Short: "Displays supported API resources",
	Run: func(cmd *cobra.Command, args []string) {

		home, err := os.UserHomeDir()
		if err != nil {
			log.Fatalf("Unable to find home directory: %v", err)
		}

		configFile := filepath.Join(home, ".cfctl", "config.yaml")
		viper.SetConfigFile(configFile)
		if err := viper.ReadInConfig(); err != nil {
			log.Fatalf("Error reading config file: %v", err)
		}

		currentEnv := viper.GetString("environment")
		if currentEnv == "" {
			log.Fatalf("No active environment specified in %s", configFile)
		}

		envConfig := viper.Sub(fmt.Sprintf("environments.%s", currentEnv))
		if envConfig == nil {
			log.Fatalf("No configuration found for environment: %s", currentEnv)
		}

		endpoint := envConfig.GetString("endpoint")
		proxy := envConfig.GetBool("proxy")

		if !proxy || !strings.Contains(endpoint, "identity") {
			log.Fatalf("Endpoint for environment '%s' is not valid for fetching resources.", currentEnv)
		}

		endpointsMap, err := fetchEndpointsMap(endpoint)
		if err != nil {
			log.Fatalf("Failed to fetch endpointsMap from '%s': %v", endpoint, err)
		}

		shortNamesFile := filepath.Join(home, ".cfctl", "short_names.yaml")
		shortNamesMap := make(map[string]string)
		if _, err := os.Stat(shortNamesFile); err == nil {
			file, err := os.Open(shortNamesFile)
			if err != nil {
				log.Fatalf("Failed to open short_names.yaml file: %v", err)
			}
			defer file.Close()

			err = yaml.NewDecoder(file).Decode(&shortNamesMap)
			if err != nil {
				log.Fatalf("Failed to decode short_names.yaml: %v", err)
			}
		}

		if endpoints != "" {
			selectedEndpoints := strings.Split(endpoints, ",")
			for i := range selectedEndpoints {
				selectedEndpoints[i] = strings.TrimSpace(selectedEndpoints[i])
			}
			var allData [][]string

			for _, endpointName := range selectedEndpoints {
				serviceEndpoint, ok := endpointsMap[endpointName]
				if !ok {
					log.Printf("No endpoint found for %s", endpointName)
					continue
				}

				result, err := fetchServiceResources(endpointName, serviceEndpoint, shortNamesMap)
				if err != nil {
					log.Printf("Error processing service %s: %v", endpointName, err)
					continue
				}

				allData = append(allData, result...)
			}

			sort.Slice(allData, func(i, j int) bool {
				return allData[i][0] < allData[j][0]
			})

			renderTable(allData)
			return
		}

		// If no specific endpoints are provided, list all services
		var wg sync.WaitGroup
		dataChan := make(chan [][]string, len(endpointsMap))
		errorChan := make(chan error, len(endpointsMap))

		for service, endpoint := range endpointsMap {
			wg.Add(1)
			go func(service, endpoint string) {
				defer wg.Done()
				result, err := fetchServiceResources(service, endpoint, shortNamesMap)
				if err != nil {
					errorChan <- fmt.Errorf("Error processing service %s: %v", service, err)
					return
				}
				dataChan <- result
			}(service, endpoint)
		}

		wg.Wait()
		close(dataChan)
		close(errorChan)

		if len(errorChan) > 0 {
			for err := range errorChan {
				log.Println(err)
			}
		}

		var allData [][]string
		for data := range dataChan {
			allData = append(allData, data...)
		}

		sort.Slice(allData, func(i, j int) bool {
			return allData[i][0] < allData[j][0]
		})

		renderTable(allData)
	},
}
View Source
var ConfigCmd = &cobra.Command{
	Use:   "config",
	Short: "Manage cfctl configuration files",
	Long: `Manage configuration files for cfctl. You can initialize,
switch environments, and display the current configuration.`,
}

ConfigCmd represents the config command

View Source
var ExecCmd = &cobra.Command{
	Use:   "exec [rpc] [service].[resource]",
	Short: "Execute a gRPC request to a specified service and message",
	Long: `Executes a gRPC command to a given service and message based on environment configuration.
	For example: cfctl exec list identity.User`,
	Args: cobra.ExactArgs(2),
	Run:  runExecCommand,
}
View Source
var LoginCmd = &cobra.Command{
	Use:   "login",
	Short: "Login to SpaceONE",
	Long: `A command that allows you to login to SpaceONE.
It will prompt you for your User ID, Password, and fetch the Domain ID automatically, then fetch the token.`,
	Run: executeLogin,
}

LoginCmd represents the login command

Functions

This section is empty.

Types

type Config

type Config struct {
	Environment  string                 `yaml:"environment"`
	Environments map[string]Environment `yaml:"environments"`
}

Config structure to parse environment files

type Environment

type Environment struct {
	Endpoint string `yaml:"endpoint"`
	Proxy    bool   `yaml:"proxy"`
	Token    string `yaml:"token"`
}

Jump to

Keyboard shortcuts

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