mirror of
https://github.com/luxfi/lpm.git
synced 2026-10-30 16:20:57 +00:00
Lux Partners Limited is the Isle of Man company. Lux Industries Inc, a Delaware C corporation, is the company these sites belong to, so it is the one they name — in metadata, copyright, and the press items that record who signed. The fund site names Lux Assets Management LLC, which is its own company. Two sentences were rewritten rather than renamed, because a name was doing work a name cannot do. One claimed an Isle of Man domicile the naming entity does not have. The other said a company acts on behalf of another; with one entity left it said a company acts on behalf of itself. The Isle of Man stays everywhere it is a JURISDICTION the product supports — the regulatory package, the jurisdiction guides, the compliance tables. Only this company's own identity changed.
140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
"github.com/spf13/afero"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/luxfi/lpm/config"
|
|
"github.com/luxfi/lpm/constant"
|
|
"github.com/luxfi/lpm/lpm"
|
|
)
|
|
|
|
var (
|
|
homeDir = os.ExpandEnv("$HOME")
|
|
lpmHome = lpmHomeDir()
|
|
lpmDir = filepath.Join(lpmHome, fmt.Sprintf(".%s", constant.AppName))
|
|
)
|
|
|
|
// lpmHomeDir returns ~/.lpm — always. One plugin dir for all nodes that
|
|
// read from the same ~/.lpm/plugins/ (luxd, mlcd, etc.).
|
|
func lpmHomeDir() string {
|
|
return filepath.Join(homeDir, ".lpm")
|
|
}
|
|
|
|
const (
|
|
configFileKey = "config-file"
|
|
lpmPathKey = "lpm-path"
|
|
pluginPathKey = "plugin-path"
|
|
credentialsFileKey = "credentials-file"
|
|
adminAPIEndpointKey = "admin-api-endpoint"
|
|
)
|
|
|
|
func New(fs afero.Fs) (*cobra.Command, error) {
|
|
rootCmd := &cobra.Command{
|
|
Use: "lpm",
|
|
Short: "lpm is a plugin manager to help manage virtual machines and chains",
|
|
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
// we need to initialize our config here before each command starts,
|
|
// since Cobra doesn't actually parse any of the flags until
|
|
// cobra.Execute() is called.
|
|
return initializeConfig()
|
|
},
|
|
}
|
|
|
|
rootCmd.PersistentFlags().String(configFileKey, "", "path to configuration file for the lpm")
|
|
rootCmd.PersistentFlags().String(lpmPathKey, lpmDir, "path to the directory lpm creates its artifacts")
|
|
rootCmd.PersistentFlags().String(pluginPathKey, filepath.Join(homeDir, ".lpm", "plugins"), "path to plugin directory (~/.lpm/plugins)")
|
|
rootCmd.PersistentFlags().String(credentialsFileKey, "", "path to credentials file")
|
|
rootCmd.PersistentFlags().String(adminAPIEndpointKey, "127.0.0.1:9650/v1/admin", "endpoint for node admin api")
|
|
|
|
err := errors.Join(
|
|
viper.BindPFlag(configFileKey, rootCmd.PersistentFlags().Lookup(configFileKey)),
|
|
viper.BindPFlag(lpmPathKey, rootCmd.PersistentFlags().Lookup(lpmPathKey)),
|
|
viper.BindPFlag(pluginPathKey, rootCmd.PersistentFlags().Lookup(pluginPathKey)),
|
|
viper.BindPFlag(credentialsFileKey, rootCmd.PersistentFlags().Lookup(credentialsFileKey)),
|
|
viper.BindPFlag(adminAPIEndpointKey, rootCmd.PersistentFlags().Lookup(adminAPIEndpointKey)),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rootCmd.AddCommand(
|
|
install(fs),
|
|
installGithub(fs),
|
|
installGitlab(fs),
|
|
installURL(fs),
|
|
installSource(fs),
|
|
uninstall(fs),
|
|
update(fs),
|
|
upgrade(fs),
|
|
link(fs),
|
|
listRepositories(fs),
|
|
joinChain(fs),
|
|
addRepository(fs),
|
|
removeRepository(fs),
|
|
)
|
|
|
|
return rootCmd, nil
|
|
}
|
|
|
|
// initializes config from file, if available.
|
|
func initializeConfig() error {
|
|
if viper.IsSet(configFileKey) {
|
|
cfgFile := os.ExpandEnv(viper.GetString(configFileKey))
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
return viper.ReadInConfig()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// If we need to use custom git credentials (say for private repos).
|
|
// the zero value for credentials is safe to use.
|
|
func initCredentials() (http.BasicAuth, error) {
|
|
result := http.BasicAuth{}
|
|
|
|
if viper.IsSet(credentialsFileKey) {
|
|
credentials := &config.Credential{}
|
|
|
|
bytes, err := os.ReadFile(viper.GetString(credentialsFileKey))
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if err := yaml.Unmarshal(bytes, credentials); err != nil {
|
|
return result, err
|
|
}
|
|
|
|
result.Username = credentials.Username
|
|
result.Password = credentials.Password
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func initLPM(fs afero.Fs) (*lpm.LPM, error) {
|
|
credentials, err := initCredentials()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return lpm.New(lpm.Config{
|
|
Directory: viper.GetString(lpmPathKey),
|
|
Auth: credentials,
|
|
AdminAPIEndpoint: viper.GetString(adminAPIEndpointKey),
|
|
PluginDir: viper.GetString(pluginPathKey),
|
|
Fs: fs,
|
|
})
|
|
}
|