other

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: Apache-2.0 Imports: 40 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)
		}

		settingPath := filepath.Join(home, ".cfctl", "setting.yaml")

		mainV := viper.New()
		mainV.SetConfigFile(settingPath)
		mainV.SetConfigType("yaml")
		mainConfigErr := mainV.ReadInConfig()

		var currentEnv string
		var envConfig map[string]interface{}

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

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

		endpointsMap, err := loadEndpointsFromCache(currentEnv)
		if err != nil {

			endpoint, ok := envConfig["endpoint"].(string)
			if !ok || endpoint == "" {
				log.Fatalf("No endpoint found for environment '%s'", 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 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 SettingCmd = &cobra.Command{
	Use:   "setting",
	Short: "Manage cfctl setting file",
	Long: `Manage setting file for cfctl. 
You can initialize, switch environments, and display the current configuration.`,
}

SettingCmd represents the setting command

View Source
var ShortNameCmd = &cobra.Command{
	Use:   "short_name",
	Short: "Manage short names for commands",
	Long:  `Manage short names for frequently used commands.`,
}

ShortNameCmd represents the shortName command

Functions

func FetchEndpointsMap added in v0.0.8

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

func GetAPIEndpoint added in v1.0.11

func GetAPIEndpoint(endpoint string) (string, error)

GetAPIEndpoint fetches the actual API endpoint from the config endpoint

func GetIdentityEndpoint added in v1.0.11

func GetIdentityEndpoint(apiEndpoint string) (string, bool, error)

GetIdentityEndpoint fetches the identity service endpoint from the API endpoint

func GetSettingDir added in v1.0.0

func GetSettingDir() string

GetSettingDir returns the directory where setting file are stored

Types

type ServiceEndpoint added in v1.0.11

type ServiceEndpoint struct {
	Name     string `json:"name"`
	Service  string `json:"service"`
	Endpoint string `json:"endpoint"`
}

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