Config struct

Fields:

  • ApxPath (string) - json:"apxPath"
  • DistroboxPath (string) - json:"distroboxPath"
  • StorageDriver (string) - json:"storageDriver"
  • UserApxPath (string)
  • ApxStoragePath (string)
  • StacksPath (string)
  • UserStacksPath (string)
  • PkgManagersPath (string)
  • UserPkgManagersPath (string)

GetApxDefaultConfig function

Returns:

  • *Config
  • error
Show/Hide Function Body
{
	userHome, err := os.UserHomeDir()
	if err != nil {
		return nil, err
	}

	// dev paths
	viper.AddConfigPath("config/")

	// tests paths
	viper.AddConfigPath("../config/")

	// user paths
	viper.AddConfigPath(filepath.Join(userHome, ".config/apx/"))

	// prod paths
	viper.AddConfigPath("/etc/apx/")
	viper.AddConfigPath("/usr/share/apx/")

	viper.SetConfigName("apx")
	viper.SetConfigType("json")

	err = viper.ReadInConfig()
	if err != nil {
		fmt.Printf("Unable to read config file: \n\t%s\n", err)
		os.Exit(1)
	}

	// if viper.ConfigFileUsed() != "/etc/apx/apx.json" || viper.ConfigFileUsed() != "/usr/share/apx/apx.json" {
	// 	fmt.Printf("Using config file: %s\n\n", viper.ConfigFileUsed())
	// }

	distroboxPath := viper.GetString("distroboxPath")

	_, err = os.Stat(distroboxPath)
	if err != nil {
		if os.IsNotExist(err) {
			path, err := exec.LookPath("distrobox")
			if err != nil {
				fmt.Printf("Unable to find distrobox in PATH.\n")
			} else {
				distroboxPath = path
			}
		}
	}

	Cnf := NewApxConfig(
		viper.GetString("apxPath"),
		distroboxPath,
		viper.GetString("storageDriver"),
	)
	return Cnf, nil
}

NewApxConfig function

Parameters:

  • apxPath string
  • distroboxPath string
  • storageDriver string

Returns:

  • *Config
Show/Hide Function Body
{
	userHome, err := os.UserHomeDir()
	if err != nil {
		panic(err)
	}

	Cnf := &Config{
		// Common
		ApxPath:       apxPath,
		DistroboxPath: distroboxPath,
		StorageDriver: storageDriver,

		// Virtual
		ApxStoragePath:      "",
		UserApxPath:         "",
		StacksPath:          "",
		UserStacksPath:      "",
		PkgManagersPath:     "",
		UserPkgManagersPath: "",
	}

	Cnf.UserApxPath = filepath.Join(userHome, ".local/share/apx")
	Cnf.ApxStoragePath = filepath.Join(Cnf.UserApxPath, "storage")
	Cnf.StacksPath = filepath.Join(Cnf.ApxPath, "stacks")
	Cnf.UserStacksPath = filepath.Join(Cnf.UserApxPath, "stacks")
	Cnf.PkgManagersPath = filepath.Join(Cnf.ApxPath, "package-managers")
	Cnf.UserPkgManagersPath = filepath.Join(Cnf.UserApxPath, "package-managers")

	return Cnf
}

fmt import

Import example:

import "fmt"

os import

Import example:

import "os"

os/exec import

Import example:

import "os/exec"

path/filepath import

Import example:

import "path/filepath"

github.com/spf13/viper import

Import example:

import "github.com/spf13/viper"