initconfig

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConfigFile = ".tcasctl/config.json"
)

Variables

View Source
var Cmd = &cobra.Command{
	Use:                        "init",
	Short:                      "init config",
	Long:                       "generate the config file",
	SuggestionsMinimumDistance: 1,
	DisableSuggestions:         false,
	Run: func(cmd *cobra.Command, args []string) {
		url, _ := cmd.Flags().GetString("url")
		skipssl, _ := cmd.Flags().GetBool("skipssl")

		homedir, err := os.UserHomeDir()
		if err != nil {
			logrus.Errorf("home dir not find, error: %s", err)
			return
		}
		configPath := path.Join(homedir, ConfigFile)
		configs := new(TCASConfig)
		if file.IsFile(configPath) == false {
			logrus.Debugf("config file does not exist, generate new config file")
			err := os.MkdirAll(path.Dir(configPath), os.ModeDir)
			if err != nil {
				logrus.Errorf("creat config dir failed, error: %s", err)
				return
			}
			configs.TCASInfos = map[string]*manager.TCASInfo{url: {
				APIEndpoint: url,
			}}
		} else {
			configs, err = ReadTCASConfigs(configPath)
			if err != nil {
				logrus.Errorf("read tcas configs failed, error: %s", err)
				return
			}
			if _, ok := configs.TCASInfos[url]; !ok {
				configs.TCASInfos[url] = &manager.TCASInfo{
					APIEndpoint: url,
				}
			} else {
				configs = nil
			}
		}

		if configs != nil {
			configs.TCASInfos[url].SkipVerify = skipssl
			err = WriteTCASConfigToFile(configs, configPath)
			if err != nil {
				logrus.Errorf("write config faield, error: %s", err)
				return
			}
			fmt.Printf("init succuessful, config path: %s\n", configPath)
		} else {
			fmt.Println("config is already init")
		}
	},
}

Cmd represents the ca command

Functions

func ReadTCASInfoFromConfig

func ReadTCASInfoFromConfig(url string) (*manager.TCASInfo, error)

func WriteTCASConfigToFile

func WriteTCASConfigToFile(configs *TCASConfig, savePath string) error

Types

type TCASConfig

type TCASConfig struct {
	TCASInfos map[string]*manager.TCASInfo `json:"configs"`
}

func ReadTCASConfigs

func ReadTCASConfigs(configPath string) (*TCASConfig, error)

Jump to

Keyboard shortcuts

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