|  | 
|  | 1 | +package chainconfig | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"os" | 
|  | 6 | +	"path/filepath" | 
|  | 7 | +	"strings" | 
|  | 8 | + | 
|  | 9 | +	"github.com/ignite/cli/ignite/chainconfig/config" | 
|  | 10 | +	v0 "github.com/ignite/cli/ignite/chainconfig/v0" | 
|  | 11 | +	v1 "github.com/ignite/cli/ignite/chainconfig/v1" | 
|  | 12 | +	"github.com/ignite/cli/ignite/pkg/xfilepath" | 
|  | 13 | +) | 
|  | 14 | + | 
|  | 15 | +var ( | 
|  | 16 | +	// ConfigDirPath returns the path of configuration directory of Ignite. | 
|  | 17 | +	ConfigDirPath = xfilepath.JoinFromHome(xfilepath.Path(".ignite")) | 
|  | 18 | + | 
|  | 19 | +	// ConfigFileNames is a list of recognized names as for Ignite's config file. | 
|  | 20 | +	ConfigFileNames = []string{"config.yml", "config.yaml"} | 
|  | 21 | + | 
|  | 22 | +	// DefaultTSClientPath defines the default relative path to use when generating the TS client. | 
|  | 23 | +	// The path is relative to the app's directory. | 
|  | 24 | +	DefaultTSClientPath = "ts-client" | 
|  | 25 | + | 
|  | 26 | +	// LatestVersion defines the latest version of the config. | 
|  | 27 | +	LatestVersion config.Version = 1 | 
|  | 28 | + | 
|  | 29 | +	// Versions holds config types for the supported versions. | 
|  | 30 | +	Versions = map[config.Version]config.Converter{ | 
|  | 31 | +		0: &v0.Config{}, | 
|  | 32 | +		1: &v1.Config{}, | 
|  | 33 | +	} | 
|  | 34 | +) | 
|  | 35 | + | 
|  | 36 | +// Config defines the latest config. | 
|  | 37 | +type Config = v1.Config | 
|  | 38 | + | 
|  | 39 | +// DefaultConfig returns a config for the latest version initialized with default values. | 
|  | 40 | +func DefaultConfig() *Config { | 
|  | 41 | +	return v1.DefaultConfig() | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +// FaucetHost returns the faucet host to use. | 
|  | 45 | +func FaucetHost(cfg *Config) string { | 
|  | 46 | +	// We keep supporting Port option for backward compatibility | 
|  | 47 | +	// TODO: drop this option in the future | 
|  | 48 | +	host := cfg.Faucet.Host | 
|  | 49 | +	if cfg.Faucet.Port != 0 { | 
|  | 50 | +		host = fmt.Sprintf(":%d", cfg.Faucet.Port) | 
|  | 51 | +	} | 
|  | 52 | + | 
|  | 53 | +	return host | 
|  | 54 | +} | 
|  | 55 | + | 
|  | 56 | +// TSClientPath returns the relative path to the Typescript client directory. | 
|  | 57 | +// Path is relative to the app's directory. | 
|  | 58 | +func TSClientPath(conf *Config) string { | 
|  | 59 | +	if path := strings.TrimSpace(conf.Client.Typescript.Path); path != "" { | 
|  | 60 | +		return filepath.Clean(path) | 
|  | 61 | +	} | 
|  | 62 | + | 
|  | 63 | +	return DefaultTSClientPath | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | +// CreateConfigDir creates config directory if it is not created yet. | 
|  | 67 | +func CreateConfigDir() error { | 
|  | 68 | +	path, err := ConfigDirPath() | 
|  | 69 | +	if err != nil { | 
|  | 70 | +		return err | 
|  | 71 | +	} | 
|  | 72 | + | 
|  | 73 | +	return os.MkdirAll(path, 0o755) | 
|  | 74 | +} | 
|  | 75 | + | 
|  | 76 | +// LocateDefault locates the default path for the config file. | 
|  | 77 | +// Returns ErrConfigNotFound when no config file found. | 
|  | 78 | +func LocateDefault(root string) (path string, err error) { | 
|  | 79 | +	for _, name := range ConfigFileNames { | 
|  | 80 | +		path = filepath.Join(root, name) | 
|  | 81 | +		if _, err := os.Stat(path); err == nil { | 
|  | 82 | +			return path, nil | 
|  | 83 | +		} else if !os.IsNotExist(err) { | 
|  | 84 | +			return "", err | 
|  | 85 | +		} | 
|  | 86 | +	} | 
|  | 87 | + | 
|  | 88 | +	return "", ErrConfigNotFound | 
|  | 89 | +} | 
0 commit comments