other

package
v0.0.10 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

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")
		cacheConfigFile := filepath.Join(home, ".cfctl", "cache", "config.yaml")

		var currentEnv string
		var envConfig *viper.Viper

		mainV := viper.New()
		mainV.SetConfigFile(configFile)
		mainConfigErr := mainV.ReadInConfig()

		if mainConfigErr == nil {

			currentEnv = mainV.GetString("environment")
			if currentEnv != "" {
				envConfig = mainV.Sub(fmt.Sprintf("environments.%s", currentEnv))
			}
		}

		if mainConfigErr != nil || currentEnv == "" || envConfig == nil {
			cacheV := viper.New()
			cacheV.SetConfigFile(cacheConfigFile)
			if err := cacheV.ReadInConfig(); err != nil {
				log.Fatalf("Error reading both config files:\nMain config: %v\nCache config: %v", mainConfigErr, err)
			}

			if currentEnv == "" {
				currentEnv = cacheV.GetString("environment")
			}

			if currentEnv == "" {
				envs := cacheV.GetStringMap("environments")
				for env := range envs {
					if strings.HasSuffix(env, "-user") {
						currentEnv = env
						break
					}
				}
			}

			if envConfig == nil && currentEnv != "" {
				envConfig = cacheV.Sub(fmt.Sprintf("environments.%s", currentEnv))
			}
		}

		if envConfig == nil {

			mainV := viper.New()
			mainV.SetConfigFile(configFile)
			if err := mainV.ReadInConfig(); err == nil {
				envConfig = mainV.Sub(fmt.Sprintf("environments.%s", currentEnv))
			}

			if envConfig == nil {
				cacheV := viper.New()
				cacheV.SetConfigFile(cacheConfigFile)
				if err := cacheV.ReadInConfig(); err == nil {
					envConfig = cacheV.Sub(fmt.Sprintf("environments.%s", currentEnv))
				}
			}

			if envConfig == nil {
				log.Fatalf("No configuration found for environment '%s' in either config file", currentEnv)
			}
		}

		endpoint := envConfig.GetString("endpoint")

		if !strings.Contains(endpoint, "identity") {
			parts := strings.Split(endpoint, "://")
			if len(parts) == 2 {
				hostParts := strings.Split(parts[1], ".")
				if len(hostParts) >= 4 {
					env := hostParts[2]
					endpoint = fmt.Sprintf("grpc+ssl://identity.api.%s.spaceone.dev:443", env)
				}
			}
		}

		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 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

View Source
var SettingsCmd = &cobra.Command{
	Use:   "settings",
	Short: "Manage cfctl settings files",
	Long: `Manage settings files for cfctl. You can initialize,
switch environments, and display the current configuration.`,
}

SettingsCmd represents the settings command

Functions

func FetchEndpointsMap added in v0.0.8

func FetchEndpointsMap(endpoint string) (map[string]string, error)

func GetConfigDir added in v0.0.5

func GetConfigDir() string

GetConfigDir returns the directory where config files are stored

Types

type TokenInfo added in v0.0.7

type TokenInfo struct {
	Token string `yaml:"token"`
}

type UserCredentials added in v0.0.7

type UserCredentials struct {
	UserID   string `yaml:"userid"`
	Password string `yaml:"password"`
	Token    string `yaml:"token"`
}

Define a struct for user credentials

Jump to

Keyboard shortcuts

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